Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98eedf4

Browse files
committed
version that verifies words
1 parent bcee3f5 commit 98eedf4

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

14_rhymer/solution3_dict_words.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python3
2+
"""Make rhyming words"""
3+
4+
import argparse
5+
import io
6+
import re
7+
import string
8+
from pydash import flatten
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Make rhyming "words"',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('word', metavar='str', help='A word to rhyme')
20+
21+
parser.add_argument('-w',
22+
'--wordlist',
23+
metavar='FILE',
24+
type=argparse.FileType('r'),
25+
default='/usr/share/dict/words',
26+
help='Wordlist to verify authenticity')
27+
28+
return parser.parse_args()
29+
30+
31+
# --------------------------------------------------
32+
def main():
33+
"""Make a jazz noise here"""
34+
35+
args = get_args()
36+
prefixes = list('bcdfghjklmnpqrstvwxyz') + (
37+
'bl br ch cl cr dr fl fr gl gr pl pr sc '
38+
'sh sk sl sm sn sp st sw th tr tw thw wh wr '
39+
'sch scr shr sph spl spr squ str thr').split()
40+
41+
dict_words = read_wordlist(args.wordlist)
42+
43+
def is_dict_word(word):
44+
return word.lower() in dict_words if dict_words else True
45+
46+
start, rest = stemmer(args.word)
47+
if rest:
48+
print('\n'.join(
49+
sorted(
50+
filter(is_dict_word,
51+
[p + rest for p in prefixes if p != start]))))
52+
else:
53+
print(f'Cannot rhyme "{args.word}"')
54+
55+
56+
# --------------------------------------------------
57+
def stemmer(word):
58+
"""Return leading consonants (if any), and 'stem' of word"""
59+
60+
word = word.lower()
61+
pos = list(
62+
filter(lambda v: v >= 0,
63+
map(lambda c: word.index(c) if c in word else -1, 'aeiou')))
64+
if pos:
65+
first = min(pos)
66+
return (word[:first], word[first:])
67+
else:
68+
return (word, '')
69+
70+
71+
# --------------------------------------------------
72+
def test_stemmer():
73+
"""test the stemmer"""
74+
75+
assert stemmer('') == ('', '')
76+
assert stemmer('cake') == ('c', 'ake')
77+
assert stemmer('chair') == ('ch', 'air')
78+
assert stemmer('APPLE') == ('', 'apple')
79+
assert stemmer('RDNZL') == ('rdnzl', '')
80+
81+
82+
# --------------------------------------------------
83+
def read_wordlist(fh):
84+
"""Read the wordlist file"""
85+
86+
return set(
87+
flatten([line.lower().strip().split() for line in fh] if fh else []))
88+
89+
90+
# --------------------------------------------------
91+
def test_read_wordlist():
92+
"""test"""
93+
94+
assert read_wordlist(io.StringIO('foo\nbar\nfoo')) == set(['foo', 'bar'])
95+
assert read_wordlist(io.StringIO('foo bar\nbar foo\nfoo')) == set(
96+
['foo', 'bar'])
97+
98+
99+
# --------------------------------------------------
100+
if __name__ == '__main__':
101+
main()

0 commit comments

Comments
 (0)