Dear Rodrigo,
absolutely briliant post. I have just added a line to use the hyphenation rules. Of course that could be improved, but it shows that the dictionary lookup in Mathematica is fast enough.
centerWord[str_String, maxString_] :=
Module[{centerLetter, formatedWord, midlePoint, paddedWord},
centerLetter =
Which[WordData[str, "Hyphenation"] === Missing["NotAvailable"],
Ceiling[StringLength[str]/2, 1],
Length[WordData[str, "Hyphenation"]] == 1,
Ceiling[(Characters[WordData[str, "Hyphenation"][[1]]] // Length)/
2], True,
Length[Characters[WordData[str, "Hyphenation"][[1]]]] + 1];
midlePoint = Ceiling[maxString/2, 1];
formatedWord =
Table[" ", {i, 1, midlePoint - centerLetter}]~Join~
MapAt[Style[#, Bold, Red] &,
Style[#, Bold] & /@ (Characters[str]), centerLetter];
Grid[{formatedWord}, Spacings -> 0.1 .5]]
spritzIt[text_String, wordsPerMinute_] :=
DynamicModule[{maxString, textList, stopWords, n},
maxString = StringLength /@ StringSplit[text] // Max;
textList = StringSplit[text];
textList =
Flatten@MapAt[{#, " ", " "} &, textList,
Position[textList, n_ /; StringQ[n] && StringMatchQ[n, "*."]]];
textList = centerWord[#, maxString] & /@ textList;
Grid[{{Style["Simple Spritz on Wolfram Language",
FontSize -> 20]}, {Dynamic[textList[[n]]~Magnify~2]}, {Animator[
Dynamic[n], {1, Length@textList, 1},
wordsPerMinute/60]}, {Style[
Row[{"Words Per Minute: ", wordsPerMinute}], FontSize -> 20]}},
Alignment -> Left, ItemSize -> {Automatic, 3}, FrameStyle -> Gray,
Frame -> All]]
Also, I wanted it to read something more book-like.
words = Import["http://www.gutenberg.org/cache/epub/2701/pg2701.txt"];
Evaluate[StringTake[words, {23029 ;; 40000}][[1]]];
spritzIt[Evaluate[StringTake[words, {23029 ;; 40000}][[1]]], 400]
That loads Moby Dick and starts reading from the first chapter. It turns out that for many words the "hyphenation-lookup" fails, that is why I need the missing data bit. I decided to highlight the first letter of the second syllable, if known. For short words I just choose the middle letter.
Thanks a lot for your interesting post,
M.