Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

An NLP exercise on homophones

7867 views
Kernel: Python 3 (Anaconda 5)

Puzzler

In this exercise we're going to try to solve the Car Talk Puzzler given below (Exercise 6).

http://greenteapress.com/thinkpython2/html/thinkpython2012.html#sec139

I've loaded the pronunciation dictionary into this directory. Below you'll find the read_dictionary function and a couple of examples for its use.

def read_dictionary(filename='cmudict.0.6d.txt'): """Reads from a file and builds a dictionary that maps from each word to a string that describes its primary pronunciation. Secondary pronunciations are added to the dictionary with a number, in parentheses, at the end of the key, so the key for the second pronunciation of "abdominal" is "abdominal(2)". filename: string returns: map from string to pronunciation """ d = dict() fin = open(filename) for line in fin: # skip over the comments if line[0] == '#': continue t = line.split() word = t[0].lower() pron = ' '.join(t[1:]) d[word] = pron return d
pron = read_dictionary()
for word in pron: wo_1st_letter = word[1:] wo_2nd_letter = word[0]+word[2:] pronunciation = pron[word] if wo_1st_letter not in pron or wo_2nd_letter not in pron: continue if len(word) != 5: continue pron_wo_1st = pron[wo_1st_letter] pron_wo_2nd = pron[wo_2nd_letter] if pronunciation == pron_wo_1st == pron_wo_2nd : print(word)
aaron eerie llama llana llano lloyd ooohs scent
pron["vomit"]
'V AA1 M AH0 T'
word = "crow" ## array slice!!! word = "antidisestablishmentarianism" word[1:]
'ntidisestablishmentarianism'
word[0] + word[2:]
'atidisestablishmentarianism'
word = "wrack" pron[word] == pron[word[1:]]
True