javascript - True Sentence Case(处理常见句子)
问题描述
真句案例
这不应与“标题案例”相混淆。句格只大写句子的第一个单词;这句话的其他词保持不变。
Stackoverflow 上的各种文章和答案都没有解决下面解释的问题:最相关的(虽然 6 岁)是在 javascript 中将字符串转换为句子大小写,但不起作用。
一个段落(或文章)可能包括几个由句号“.”分隔的句子。
我正在寻找至少应该处理下面描述的情况的“SentenceCase”代码。
目的是仅将第一个单词(如果适用)大写,而不是尝试修改或尝试更正句子文本的其余部分。
该功能应解决以下标准情况:
// 1. Simple sentence (no full-stop):
// ----------------------------------
// Original: this is a sentence
// Becomes : This is a sentence
// 2. Two or more sentences:
// -------------------------
// Original: first sentence. second sentence . third sentence.
// Becomes : First sentence. Second sentence . Third sentence.
// 3. Sentences starting with a single char:
// -----------------------------------------
// Original: a world of fun. a world of fun.
// Becomes : A world of fun. A world of fun.
// 4. Sentences starting with a word that has an in-between cap:
// -------------------------------------------------------------
// Original: kHz is a unit of measure. kPascal too.
// Becomes : kHz is a unit of measure. kPascal too.
// Original: iPads are sold by Apple. iTones are too.
// Becomes : iPads are sold by Apple. iTones are too.
// 5. Sentences starting with a special chars/words:
// -------------------------------------------------
// Original: https//: is the web address. www.stack.com is another website.
// Becomes : https//: is the web address. www.stack.com is another website.
// Original: .Net is a programming language. foo_bar is a variable it uses.
// Becomes : .Net is a programming language. foo_bar is a variable it uses.
// Original: string_length is a snake case type variable.
// Becomes : string_length is a snake case type variable.
// Original: abc@domain.net is my email. defgh@dmain.net is his.
// Becomes : abc@domain.net is my email. defgh@dmain.net is his.
// Original: concrete5 is an open-source content management system.
// Becomes : concrete5 is an open-source content management system.
// 6. Text inside quotes:
// ----------------------
// Original: "we should stay home" said the governor. 'we will' we answered.
// Becomes : "we should stay home" said the governor. 'we will' we answered.
// Original: "this ". was text in quotes
// Becomes : "this ". Was text in quotes
// 7. Sentences with Abbreviations:
// --------------------------------
// Original: reporting time is 09:00 a.m. and leaving at 05:00 p.m. every day.
// Becomes : Reporting time is 09:00 a.m. and leaving at 05:00 p.m. every day.
// 8. Short sentences with single words:
// -------------------------------------
// Original: organization
// Becomes : Organization
// Original: development.
// Becomes : Development.
解决方案
推荐阅读
- c# - 我如何使用 Bootstrap Carousel 作为数据库记录的列表?
- silverstripe - Silverstripe 3:如何在设定的日期取消发布页面?
- c - 快速排序未正确排序长值数组
- c++ - 测试子类是否覆盖基类中的虚函数
- r - R通过取消分组日期在顶层汇总
- python - 使用 SSH 或 FTP 压缩远程目录的 Pythonic 方法
- visual-studio - 我可以将代码从 Visual Studio Code 集成或导入到 Visual Studio 吗?
- gcc - 如何将主干补丁应用到 mingw-w64 8.1
- javascript - 这个纯 JavaScript 代码中的 $$ 函数是如何使用的?
- python - 即使语句为 True,我的函数也没有返回 True