In a previous post of mine Google Translate Structure, I've shown how to a TextStructure-like box. In this post, I'll show how can you create a button that changes dynamically when clicked (I didn't find too many examples of it) to show the phonetic form of a word.
First, my MakeBox code slightly modified to allow IPA to work properly.
MakeBoxes[StructureElement[main_, down_], _] := GridBox[{
{MakeBoxes@main}, {StyleBox[MakeBoxes@down, GrayLevel[0.6]]}},
BaseStyle -> "TextElementGrid"]
Next, we need a simple function that outputs IPA, WordData can do this but not all words have phonetic form.
SetAttributes[{IPA, IPAQ, ButtomIPA}, Listable]
IPA[word_String] := IPA@word = Module[{list},
If[StringCount[word, " "] != 0 \[Or] ! DictionaryWordQ@word, (* If it has space or is not in the dictionary *)
Missing@word,
(* WordData doesn't have IgnoreCase option... *)
list = Select[WordData[]~Join~DictionaryLookup[], StringMatchQ[#, word, IgnoreCase -> True] &];
If[Length@list == 0,
Return@Missing@word,
If[! MissingQ@#, ToString@#, Missing@word] &@
WordData[First@list, "PhoneticForm"]
]
]
]
IPAQ[word_String] := IPAQ@word = !MissingQ@IPA@word
RemovePeriod[word_String] := StringTrim@StringReplace[word, "." | "," | "!" | "?" -> ""]
And finally, some code to create the Dynamic Button
ButtomIPA[word_String] := If[IPAQ@RemovePeriod@word,
DynamicModule[{display = word, text = word, action = False},
Button[Dynamic@display,
If[StringQ@display,
display = StructureElement[text, IPA@RemovePeriod@text],
display = text
], Appearance -> None]
]
,
Style[word, GrayLevel[0.5]]
]
A simple usage would be (sorry for the poor GIF quality):
Row@Riffle[ButtomIPA@StringSplit["My lord, the queen would speak with you, and presently.", Whitespace], " "]