Goal is to create a haiku by inserting replacements into a template, obeying the 5/7/5 syllable rule, and constraining the replacements to be a single word with the required number of syllables.
The source haiku is: Over the wintry Forest, winds howl in rage With no leaves to blow.
As a template it becomes: Over the _(adjective)__ Forest, winds howl in __(noun)__ With no leaves to __(verb)__.
Download this notebook to evaluate!
{elapsedTime,validWordsBySyllables}=(*Cache,foreachreplacementslot,alistofallcommonwordsthathavetheexactnumberofsyllablesforitsspotinthehaiku*)Timing[Module[{wordSyllables=ResourceFunction["WordSyllables"],replacementSyllables=<|"adj"->2,"noun"->2,"verb"->1|>,(*byinspectionofthehaiku*)replacementPartsOfSpeech=<|"adj"->"Adjective","noun"->"Noun","verb"->"Verb"|>,posWords,(*pos==abbrevfor"part of speech";assnholdingforeachposall"CommonWords"ofthatpos*)posWordsWithIndicatedSyllables},posWords=AssociationMap[#[[1]]->WordList[{"CommonWords",#[[2]]}]&,replacementPartsOfSpeech];posWordsWithIndicatedSyllables[pos_String]:=(*forgivenpos,getall"CommonWords"withcorrect#syllables*)Select[posWords[[pos]],Length[wordSyllables[#]]==replacementSyllables[[pos]]&];AssociationMap[posWordsWithIndicatedSyllables[#]&,Keys[replacementPartsOfSpeech]]]];Print["Number of words for each replacement: ",Map[Length,validWordsBySyllables]," (",elapsedTime," seconds)"];generateHaiku[]:=(*generatearandombutvalid(accordingtotherulesofthischallenge)haikubasedonthegivenhaiku*)StringTemplate["Over the `adj`Forest, winds howl in `noun`With no leaves to `verb`."][AssociationMap[#[[1]]->RandomChoice[#[[2]]]&,validWordsBySyllables]]
In[]:=
Manipulate[Grid[Table[generateHaiku[],m,n],Frame->All,Alignment->{Left,Top}],{{m,5,"Rows"},1,20,1},{{n,3,"Cols"},1,4,1}]
Annoyances I ran into during this project:
Annoyances I ran into during this project:
◼
Map[#[“Name”]&, EntityList[“PartOfSpeech”]] returns lower-case names but WordList[“part-of-speech”] accepts only capitalized names.
◼
Project says haikus follow a 5/7/5 syllable rule but the template haiku does not (it is 5/6/5).
◼
Supposing 5/7/5 the remaining syllables for the replacements would be 2/2/1 (or 1/1/1 if following the actual haiku given) but the project notebook’s solution uses 3/1/1
◼
I tried to use WordList[“KnownWords”] instead of WordList[“CommonWords”] to get more words (poetry generally uses a larger vocabulary?) but WordList[“KnownWords”] includes a lot of non-words! (E.g., numbers.)