Wolfram Language

Open live version

Generate Random Pronounceable Words

Generate random English words where the probability of each letter is given by the likelihood that the letter occurs after the previous two letters somewhere in the dictionary.


code

X
X
X
X

Make random words using bigrams instead of trigrams.

X
X

how it works

The data that guides the generation of random words is contained in trigrams, a list of elements like this one, that indicates that there is 1 word where aa is followed by h, 4 words where aa is followed by r, and so on. The right side of the first arrow is in precisely the format required by RandomChoice to make a weighted choice from a list.

In[1]:=
X

To build the complete list of trigrams, first find all triples of characters in the dictionary, tally them, and gather together those that have the same first two letters. Here is an example of the result when the first two letters are aa.

In[2]:=
X
Out[2]=

Using the convention that AA indicates the beginning of a word and Z the end, special entries in trigrams for {A,A} and {A, a} give the frequencies, respectively, of words that begin with a given letter and words whose second letter is a given letter. Z elements indicate no subsequent letter. A default case is appended to trigrams to avoid errors. This is the complete list of trigrams:

In[3]:=
X

To generate a random word using the trigrams, start with AA and repeatedly add random characters guided by the weights in trigrams until a Z is produced.

In[4]:=
X
Out[4]=

Extract the third to next-to-last characters and join them together to make a pronounceable word.

In[5]:=
X
Out[5]=

Package those steps as the function randomWord.

X

Test randomWord by making a list of random words.

In[6]:=
X
Out[6]=

englishWordQ tests if a word is in the dictionary.

In[7]:=
X

This selects the English words in a sample of 20 random words:

In[8]:=
X
Out[8]=

You can estimate the likelihood of getting an actual word with randomWord experimentally:

In[9]:=
X
Out[9]=

Heres how to make a web page that gives you a different, nicely formatted word each time you visit it.

Format a word using Style to set the font and size and Pane to add some white space around the word. Set the color to red if it is an English word.

In[10]:=
X
Out[10]=

Deploy that to the cloud with APIFunction and CloudDeploy, specifying PermissionsPublic so that everyone has access.

In[11]:=
X
Out[11]=