0

I am wondering, in the context of a computer processing text, which characters or combination of characters can end an English sentence. A question mark, exclamation mark and period are the most obvious, but are there any others? What if the final character is a parenthesis?

Background

I am working on a computer program that produces a document, and in the document I am displaying some user-entered text in the form of a sentence. This user text ends the sentence, so I need to ensure there's a full stop before the next sentence begins.

I could simply add a period to the end of the text, which would work most of the time:

This is an exampleThis is an example.

But of course if the user entered text that includes an end-of-sentence marker, this simple solution won't work:

Example!Example!.

So clearly if the last character is an exclamation mark, question mark or period, then no extra period should be appended.

However it doesn't seem so simple. What if the last character is a closing parenthesis? You could simply add a period to (Example) and it would be fine, but if it's (Example.) then you should not add a period as (Example.). is not correct.

So in this case it seems you need to check the last two characters to see if the sentence has ended.

But then what if the sentence ends with a quotation mark?

Please refer to the song "Sweet Dreams (Are Made of This)"

So it seems you have to check the final three characters to see whether the sentence has ended, and whether you should append a period or not.

Question

Is there any rule (suitable for implementation in a computer program) that would cover at least the majority of cases? I am thinking something like:

  1. Check the last character
  2. If it is ?, ! or . then the sentence has ended
  3. If it is " or ) then drop the character from the end and go back to 1
  4. Otherwise the sentence has not ended, and a period should be appended to the original text.

Would this work, or does it miss other combinations that might occur?

Out of curiosity, what other character combinations might there be that could potentially end a sentence, but require closer scrutiny rather than blindly appending a full stop?

closed as too broad by Edwin Ashworth, Chris H, David, Davo, RaceYouAnytime Aug 23 '17 at 18:57

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Hello, Malvineous. There are doubtless many exceptions to your suggested rules, many complicating factors. I'll mention just three, but then I'll have to close-vote as the question is too broad to give a definitive answer to. (a) a sentential quote within a sentence and (b) double punctuation (not considered unacceptable in at least the UK) << Did the notice read 'Do not enter', 'Do not enter.' or 'Do not enter!'? >> (c) parenthetical exclamation mark << Then, John (!) and Jill each sang a song. >> And how a computer can tell when a period is missing is something I'd hate to have to address. – Edwin Ashworth Aug 17 '17 at 8:28
  • We need more counter examples!!!! I say "Are you sure?!". ***YES*** (obviously these are informal and slightly contrived but your users will come up with worse, don't you think?) – Chris H Aug 17 '17 at 11:50
  • I'll note that if you're talking about text used on computer screens as title, etc, that is effectively off-topic. When and where to use terminating punctuation is a matter of style on computer screens. – Hot Licks Aug 17 '17 at 12:04
  • "If it is ?, ! or . then the sentence has ended" I'm sure you'll be getting a lot of "sentences" ending in Mr., Mrs., Dr., etc. where the next "sentence" starts with a last name. – Laurel Aug 17 '17 at 17:00
  • @Laurel: We're only looking at the end of the text though (which could be more than one sentence) to work out whether it needs ending punctuation, so if you had the text Dr. Smith the period after Dr is unrelated to whether or not a period should be added after Smith. In this case the final letter is h which means a period could be appended to end the sentence. – Malvineous Aug 17 '17 at 23:06
2

A few more I can think of:

  • Ellipsis: This is often typed as three dots, which your other rule would catch, but there is also a single character for it. Note that an ellipsis could also occur in the middle of a sentence.
  • Period with square bracket. [Like this for example.]
  • Right quotation marks: and (not just the vertical ones you listed: " and '.)

You should learn regular expressions, by the way, if you don't already know them. They will be very useful to you.

  • I think in this case using regexes would be an example of the classic "now you have two problems" given that the initial problem is unlikely to be solvable – Chris H Aug 17 '17 at 11:53
  • @ChrisH, huh? You will have to explain your logic. He just wants recognize some simple patterns. "Now you have two problems" is when people try to use regexes for a problem that would require a parser. This isn't such a case. – dangph Aug 17 '17 at 13:05
  • the comments under the question indicate that the initial problem can't be reliably defined. If we accept that (obviously I do as I wrote one of the comments) we can't write a regex. If we try (maybe we start with [\.)"\]”!?]+$) we then have to bolt on more and more edge cases. Actually taking that character class I posted and adding some more might be good enough. Until a user comes along and ends their post with :-( – Chris H Aug 17 '17 at 14:25
  • @ChrisH, he wanted a "rule (suitable for implementation in a computer program) that would cover at least the majority of cases". That seems doable to me. – dangph Aug 17 '17 at 14:32
  • 1
    You could probably come up with something not too cumbersome to cover the majority of cases in formal written English. But I've just skimmed the comments in my facebook feed as a different style of writing. Ignoring emoji and foreign languages, I can see plenty that would be very hard to deal with, That's despite mainly having friends who write decent English on facebook. So in other words I can't agree with "majority of cases", at least not without some restrictions we don't have – Chris H Aug 17 '17 at 14:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.