Wolfram Language

Open live version

Make Mnemonics for Telephone Numbers

Make telephone numbers easy to remember by finding corresponding words or phrases.


code

X
X
X
X

how it works

This approach to finding words or phrases that correspond to telephone numbers takes three steps. First make a function that finds single words that correspond to a sequence of digits on a telephone pad. Then make a function that partitions a telephone number into all possible sequences and find words for each sub-sequence. Finally, make a function that forms all combinations of all words for all partitions.

Map digits to the corresponding letters on a telephone pad with a key-value Association. Since 0 and 1 have no letters, associate those digits with themselves:

In[1]:=
X

You can use the Association like a function to map a digit to the corresponding letters:

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

Map a sequence of digits to the corresponding letters by mapping the Association across the digit list:

In[3]:=
X
Out[3]=

To find words that can be formed from those letter choices, construct a regular expression that specifies a sequence of letters, one from each group. Since the dictionary contains both lowercase and uppercase words, make the regular expression contain both lowercase and uppercase letters:

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

Use DictionaryLookup to find words that match the regular expression:

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

Package those steps as a function. Since the words 0 and 1 are not in the dictionary, add special-case rules for those digits:

In[6]:=
X

Test the function:

In[7]:=
X
Out[7]=

The number 4686328 has no telephone word:

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

But it can be broken into smaller sequences that do have words, for example:

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

Find all telephone phrases for a list of digits by finding every way of breaking a list of digits into smaller lists and finding words for each smaller list.

Start with a function that finds all partitions of a list into smaller lists. The function is most naturally formulated recursively by thinking about it this way: the partitions of a list are the list itself plus the first n elements of the list combined with all partitions of the remainder of the list, for every n from one less than the length of the list to one:

In[11]:=
X

An n-element list has 2n-1 partitions:

In[12]:=
X
Out[12]=

With ListPartition, you can find all telephone word phrases by mapping TelephoneWords across the elements of each partition:

In[13]:=
X
Out[13]=

For longer digit lists, however, this approach is prohibitively inefficient. It wastes too much time looking up words later in the list when it already knows that an earlier sublist has no words.

A better approach is to build the TelephoneWords call directly into the recursive function and abort as soon as you know that some sublist has no words. That is what TelephoneWordLists does:

In[14]:=
X

TelephoneWordLists returns lists of lists of words:

In[15]:=
X
Out[15]=

To make phrases from the lists of word alternatives, form all combinations of alternative words, for example:

In[16]:=
X
Out[16]=

Put hyphens between each word in a sequence using Riffle, and join all the components together into single strings with StringJoin:

In[17]:=
X
Out[17]=

Package those steps into a function whose argument is an integer telephone number:

In[18]:=
X

Test the function on a number:

In[19]:=
X
Out[19]=