You've been hired to write some code for a dictation-taking app, which takes voice input from a spoken source, parses it as words, and writes it down on a screen.
The management doesn't really trust you with all that much powerin the project—you're known to sit around and golf code all day instead of doing your work, unfortunately—so they just give you a really simple task to perform: turn a Sentence with interspersed Punctuation into a properly formatted sentence, where 'properly formatted' is defined below.
The Sentence is the string of input. A Word is a group of continguous non-space characters. A Punctuation is a Word whose first character is
A Word is capitalized if the first letter of the Word is not a lowercase letter (capitalized words match the regex
The first Word of the Sentence must be capitalized.
^COMMAis the comma character
,and has a space following but not preceding.
aaa ^COMMA bbbbecomes
^COLONis a comma that looks like
^SEMICOLONis a comma that looks like
^PERIODis a comma that looks like
.. The word following a
^PERIODmust be capitalized.
^BANGis a period that looks like
^DASHis the dash character
-and has a space both preceding and following.
^HYPHENis also the dash character
-but has no space following or preceding.
^EMDASHis a hyphen (not a dash!) that is spelled
^OPENQUOTEis a quote character
"that has a space preceding but not following. The word following an
^OPENQUOTEmust be capitalized. If an
^OPENQUOTEis preceded by a Word that is not Punctuation, add a
^COMMAbetween that word and the
^OPENQUOTE. If an
^OPENQUOTEis preceded by a Punctuation that makes the next word capitalized, this skips over the
^OPENQUOTEto the next word.
^CLOSEQUOTEis the digraph
,"that has a space following but not preceding. If a
^CLOSEQUOTEis preceded by a
^BANG, that Punctuation disappears and the
!"respectively. If the disappearing Punctuation specified a capitalization, that capitalization must still occur on the next available word.
Initial or trailing spaces in the full final result must be removed, and any string of two or more spaces in a row must be all collapsed into a single space character.
Any case not covered above (e.g.
^UNDEFINEDPUNCTUATION) will not occur in well-formed input and is thus undefined behaviour.
The development team informs you of the following:
The project is being written in the language [your language here], and should be as short as possible so that it takes up as little space as possible when it's an app for Android/iPhone. You try to explain that that's not how app development works, but they don't listen. But hey, what a coincidence! You're an amazing golfer in [your language here]!
The app will not have any web access permissions, and there won't be any libraries installed that do this formatting for you. You can probably convince the team lead to allow you a regex library if one exists for your language, though, if you think you need one.
Support for nested quotations that use double/single quotes properly is planned for a later version of the app, but not the version that you're working on now, so don't worry about it.
The management is a huge fan of test-driven development, and so the dev team has already had some hapless keyboard monkey write up some tests for your portion of the program: (newlines added for readability, treat them as spaces)
hello ^COMMA world ^BANG
once upon a time ^COMMA there was a horse ^PERIOD that horse cost me $50 ^PERIOD ^OPENQUOTE eat your stupid oats ^COMMA already ^BANG ^CLOSEQUOTE I told the horse ^PERIOD the horse neighed back ^OPENQUOTE no ^CLOSEQUOTE and died ^PERIOD THE END
Once upon a time, there was a horse. That horse cost me $50. "Eat your stupid oats, already!" I told the horse. The horse neighed back, "No," and died. THE END
begin a ^PERIOD b ^COMMA c ^COLON d ^SEMICOLON e ^BANG f ^HYPHEN g ^DASH h ^EMDASH i ^OPENQUOTE j ^PERIOD ^OPENQUOTE k ^SEMICOLON ^OPENQUOTE l ^CLOSEQUOTE m ^BANG ^CLOSEQUOTE n ^PERIOD 0x6C6F6C end
Begin a. B, c: d; e! F-g - h--i, "j. "K; "l," m!" N. 0x6C6F6C end
This is a code golf: the lowest score wins. You may write a function of one string argument, or a program reading from STDIN and writing to STDOUT.