Skip to content

Commit fe91fc4

Browse files
committed
Adding homophone.py
1 parent 49526c8 commit fe91fc4

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

code/homophone.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
14+
from pronounce import read_dictionary
15+
16+
17+
def make_word_dict():
18+
"""Read. the words in words.txt and return a dictionary
19+
that contains the words as keys."""
20+
d = dict()
21+
fin = open('words.txt')
22+
for line in fin:
23+
word = line.strip().lower()
24+
d[word] = word
25+
26+
return d
27+
28+
29+
def homophones(a, b, phonetic):
30+
"""Checks if words two can be pronounced the same way.
31+
32+
If either word is not in the pronouncing dictionary, return False
33+
34+
a, b: strings
35+
phonetic: map from words to pronunciation codes
36+
"""
37+
if a not in phonetic or b not in phonetic:
38+
return False
39+
40+
return phonetic[a] == phonetic[b]
41+
42+
43+
def check_word(word, word_dict, phonetic):
44+
"""Checks to see if the word has the following property:
45+
removing the first letter yields a word with the same
46+
pronunciation, and removing the second letter yields a word
47+
with the same pronunciation.
48+
49+
word: string
50+
word_dict: dictionary with words as keys
51+
phonetic: map from words to pronunciation codes
52+
"""
53+
word1 = word[1:]
54+
if word1 not in word_dict:
55+
return False
56+
if not homophones(word, word1, phonetic):
57+
return False
58+
59+
word2 = word[0] + word[2:]
60+
if word2 not in word_dict:
61+
return False
62+
if not homophones(word, word2, phonetic):
63+
return False
64+
65+
return True
66+
67+
68+
if __name__ == '__main__':
69+
phonetic = read_dictionary()
70+
word_dict = make_word_dict()
71+
72+
for word in word_dict:
73+
if check_word(word, word_dict, phonetic):
74+
print(word, word[1:], word[0] + word[2:])

code/invert_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ def invert_dict(d):
2323
Returns: dict
2424
"""
2525
inverse = {}
26-
for key, val in d.items():
26+
for key in d:
27+
val = d[key]
2728
inverse.setdefault(val, []).append(key)
2829
return inverse
2930

3031

3132
if __name__ == '__main__':
3233
d = dict(a=1, b=2, c=3, z=1)
3334
inverse = invert_dict(d)
34-
for val, keys in inverse.items():
35+
for val in inverse:
36+
keys = inverse[val]
3537
print(val, keys)
3638

code/pronounce.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
14+
15+
def read_dictionary(filename='c06d'):
16+
"""Reads from a file and builds a dictionary that maps from
17+
each word to a string that describes its primary pronunciation.
18+
19+
Secondary pronunciations are added to the dictionary with
20+
a number, in parentheses, at the end of the key, so the
21+
key for the second pronunciation of "abdominal" is "abdominal(2)".
22+
23+
filename: string
24+
returns: map from string to pronunciation
25+
"""
26+
d = dict()
27+
fin = open(filename)
28+
for line in fin:
29+
30+
# skip over the comments
31+
if line[0] == '#': continue
32+
33+
t = line.split()
34+
word = t[0].lower()
35+
pron = ' '.join(t[1:])
36+
d[word] = pron
37+
38+
return d
39+
40+
41+
if __name__ == '__main__':
42+
d = read_dictionary()
43+
for k, v in d.items():
44+
print(k, v)

0 commit comments

Comments
 (0)