4
\$\begingroup\$

(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!
\$\endgroup\$
  • \$\begingroup\$ "what about this?", "(or this.)"? (I'm guessing "what about this? ". and (or this. )., which is a shame, but golf be golf...) \$\endgroup\$ – Jonathan Allan May 17 '17 at 18:52
  • \$\begingroup\$ What are all the possible characters that can appear in the input? \$\endgroup\$ – Kritixi Lithos May 17 '17 at 18:53
  • \$\begingroup\$ Your last test case seems wrong, since nothing in the post says we need to remove repeated punctuation with whitespace inbetween. \$\endgroup\$ – DJMcMayhem May 17 '17 at 19:57
  • \$\begingroup\$ would rules number 5 and 6 clash and end up having a trailing output of <space>.<space>.<space>.<space>.<space>. \$\endgroup\$ – tuskiomi May 17 '17 at 20:26
  • \$\begingroup\$ 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. \$\endgroup\$ – ElPedro May 17 '17 at 20:42
2
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$ – Business Cat May 17 '17 at 18:55
  • 1
    \$\begingroup\$ There should be a space after punctuation like ; \$\endgroup\$ – Kritixi Lithos May 17 '17 at 18:55
  • 1
    \$\begingroup\$ Also ([^.!?])(?m:$) $1. -> [^.!?]$ $&.. \$\endgroup\$ – Business Cat May 17 '17 at 18:58
  • \$\begingroup\$ @KritixiLithos Thanks, fixed. \$\endgroup\$ – eush77 May 17 '17 at 19:06
  • 1
    \$\begingroup\$ Ah. 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. \$\endgroup\$ – Business Cat May 17 '17 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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