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
/[^a-z].*/
).The first Word of the Sentence must be capitalized.
A
^COMMA
is the comma character,
and has a space following but not preceding.aaa ^COMMA bbb
becomesaaa, bbb
.A
^COLON
is a comma that looks like:
.A
^SEMICOLON
is a comma that looks like;
.A
^PERIOD
is a comma that looks like.
. The word following a^PERIOD
must be capitalized.A
^BANG
is a period that looks like!
.A
^DASH
is the dash character-
and has a space both preceding and following.A
^HYPHEN
is also the dash character-
but has no space following or preceding.An
^EMDASH
is a hyphen (not a dash!) that is spelled--
.An
^OPENQUOTE
is a quote character"
that has a space preceding but not following. The word following an^OPENQUOTE
must be capitalized. If an^OPENQUOTE
is preceded by a Word that is not Punctuation, add a^COMMA
between that word and the^OPENQUOTE
. If an^OPENQUOTE
is preceded by a Punctuation that makes the next word capitalized, this skips over the^OPENQUOTE
to the next word.A
^CLOSEQUOTE
is the digraph,"
that has a space following but not preceding. If a^CLOSEQUOTE
is preceded by a^COMMA
,^PERIOD
, or^BANG
, that Punctuation disappears and the^CLOSEQUOTE
is spelled,"
,."
, or!"
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.
^COMMA ^COMMA
or^SEMICOLON ^CLOSEQUOTE
or^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)
Input:
hello ^COMMA world ^BANG
Output:
Hello, world!
Input:
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
Output:
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
Input:
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
Output:
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.
prompt()
? \$\endgroup\$