Correct peoples grammar

4

0

(yes, that title was intentional)

Your task is to correct common grammatical mistakes.

You must:

  • Capitalise the first letter of each sentence, ie the first non-whitespace letter after any of .!? and, additionally, capitalise the first letter of the input
  • Remove repeating punctuation, such as !!! -> !. Ellipses (...) are not special, and will collapse into ..
  • Remove repeating whitespace <space><space><space> -> <space>
  • Remove whitespace directly before any of .,;!? as in: Hello , World ! -> Hello, World!
  • If there is no space directly after any of .,;!?, add it.
  • Add a . at the end of the input if there is not any of .!? at the end.
  • Not use Mathematica's builtins

Examples:

This is a  sentence .Unfortunately, it's not very grammatically correct.can you fix it ???
This is a sentence. Unfortunately, it's not very grammatically correct. Can you fix it?

buffalo, buffalo;buffalo   buffalo? Buffalo.buffalo Buffalo buffalo
Buffalo, buffalo; buffalo buffalo? Buffalo. Buffalo Buffalo buffalo.

i'm not sure what to put for this test case...
I'm not sure what to put for this test case.

!this is a test. .Can ...you . . . 'ear me Ground-Control? ?! 'ere as in what you do with you're [sic] ears !
! This is a test. Can. You. 'ear me Ground-Control?! 'ere as in what you do with you're [sic] ears!

Okx

Posted 2017-05-17T18:30:47.040

Reputation: 15 025

"what about this?", "(or this.)"? (I'm guessing "what about this? ". and (or this. )., which is a shame, but golf be golf...) – Jonathan Allan – 2017-05-17T18:52:54.800

What are all the possible characters that can appear in the input? – user41805 – 2017-05-17T18:53:20.867

Your last test case seems wrong, since nothing in the post says we need to remove repeated punctuation with whitespace inbetween. – James – 2017-05-17T19:57:31.153

would rules number 5 and 6 clash and end up having a trailing output of <space>.<space>.<space>.<space>.<space>. – tuskiomi – 2017-05-17T20:26:49.847

I like this challenge but I guess a better title might be "Correct peoples grammar according to the given rules". The rules are specific enough about what is required for me. If I am not misunderstanding, as long as the test cases are matched then the answer is valid. If my comment is grammatically incorrect then please feel free to use it as a further test case. – ElPedro – 2017-05-17T20:42:21.047

Do leading whitespace have to be removed? – user41805 – 2017-05-17T20:45:16.093

@KritixiLithos Not unless there is any of .,;!? after the whitespace at the start. – Okx – 2017-05-18T07:37:19.217

Answers

2

Retina, 80 74 73 71 74 64 65 62 78 72 bytes

 *([.,;!? ])(\1| )*
$1
T`l`L`[.!?].|^.
([.,;!?])(\w|')
$1 $2
[^.!?]$
$&.

Try it online!

This is my first Retina answer.

-12 bytes thanks to @BusinessCat!

-3 bytes thanks to @Neil.

eush77

Posted 2017-05-17T18:30:47.040

Reputation: 1 280

1I think you can replace \s with a literal space. OP said there won't ever be newlines and I'm assuming there won't be tabs either. – Business Cat – 2017-05-17T18:55:10.783

1There should be a space after punctuation like ; – user41805 – 2017-05-17T18:55:56.037

1Also ([^.!?])(?m:$) $1. -> [^.!?]$ $&.. – Business Cat – 2017-05-17T18:58:57.473

@KritixiLithos Thanks, fixed. – eush77 – 2017-05-17T19:06:16.993

@BusinessCat Thanks, but $ still seems to need a multiline modifier. – eush77 – 2017-05-17T19:07:29.720

1Ah. When you run all test cases at once it will. If you do one at a time it would be fine. You could also precede the line with `%`` (which executes the stage on each line separately) instead of using the modifier. – Business Cat – 2017-05-17T19:14:53.650

You don't need to count code added solely to run multiple test cases at once. On TIO, normal practice is to put %(G` in the Header area instead. – Neil – 2017-05-17T19:59:50.863

@Neil Did just that, thanks. – eush77 – 2017-05-17T20:09:09.763