0

True Sentence Case

This is not to be confused with "Title Case". Sentence case is capitalizing only the first word of the sentence; other words of the sentence remain unchanged.

The various articles and answers on Stackoverflow do not address the issues explained below: The most relevant (although 6 years old) is Convert string to sentence case in javascript but does not work.

A paragraph (or article) may include several sentences separated by full-stops ".".

I am looking for a "SentenceCase" code that should at least handle the cases described below.

The intent is to ONLY capitalize the first word (if applicable) and not to try to modify or try to correct the rest of the sentence text.

The function should address the following standard cases:

// 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.

Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42
  • 1
    Recognizing sentences is a complex natural language processing problem. You have to handle abbreviations and other unusual uses of punctuation. – Barmar Apr 21 '20 at 19:48
  • It sounds like you're asking someone to write the code for you or to refer you to a library that does sentence casing. This site is more for users that have tried some code and are stuck getting it correct. If you have attempted some code that implements sentence case, post that and explain why you're having problems with it. – Jacob Apr 21 '20 at 19:50
  • Only need a function to capitalize the first letter of a sentence "if valid"; that's it, ignore the rest. – Mohsen Alyafei Apr 21 '20 at 19:51
  • Some examples in 5 are shaky and 6 is just wrong unless it's not real English? There are some examples which are considered typos -- ex. in 2 there's a space ***before*** a period. – zer00ne Apr 21 '20 at 19:52
  • @zer00ne item 6 is only example for leaving text in quotes unchanged. It could be any text. – Mohsen Alyafei Apr 21 '20 at 19:54
  • @MohsenAlyafei Quoted phrases are sentences and the first letter of any sentence is capitalized. The first set of quotes could be correct if it was part of another sentence. But the second set of quotes is definitely a sentence that needs to be capitalized. Moreover, there are no commas. – zer00ne Apr 21 '20 at 20:01

0 Answers0