Repository navigation
Expand file tree
/
Copy pathrhymer.py
More file actions
executable file
·108 lines (81 loc) · 2.95 KB
/
Copy pathrhymer.py
File metadata and controls
executable file
·108 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Title : Rhymer
Author : wrjt <wrjt@localhost>
Date : 2021-09-04
Purpose: Find rhyming words using regexes
"""
import argparse
import re
import string
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Make rhyming "words"',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('word', metavar='word', help='A word to rhyme')
args = parser.parse_args()
# if len(args.word) != 1:
# parser.error("Please only enter one word.")
return args
# --------------------------------------------------
def main():
""" Main prog """
args = get_args()
consonants = list('bcdfghjklmnpqrstvwxyz')
clusters = """\
bl br ch cl cr dr fl fr gl gr pl pr sc sh sk sl sm sn sp st sw th
tr tw thw wh wr sch scr shr sph spl spr squ str thr""".split()
prefixes = sorted(consonants + clusters)
remove_me, stem = stemmer(args.word)
if stem:
output = '\n'.join([p + stem for p in prefixes if p != remove_me])
else:
output = f'Cannot rhyme "{args.word}"'
print(output)
def stemmer(word: str) -> tuple:
""" Return leading consonants (if any), and 'stem' of the word """
letters, vowels = string.ascii_lowercase, 'aeiou'
consonants = ''.join([c for c in letters if c not in vowels])
# consonants = ''.join(filter(lambda c: c not in vowels, letters))
word = word.lower()
# # Alternative using re.compile and findall (which returns a list)
# pattern_regex = re.compile(
# rf'''(
# ([{consonants}]+)? # Capture one or more (optional)
# ([{vowels}]+) # Capture at least one vowel
# (.*) # Capture zero or more of anything else
# )''', re.VERBOSE)
# match = pattern_regex.findall(word)
# if match:
# p1 = match[0][1] or ''
# p2 = match[0][2] or ''
# p3 = match[0][3] or ''
# return (p1, p2 + p3)
# else:
# return (word, '')
pattern = (
f'([{consonants}]+)?' # Capture one or more (optional)
f'([{vowels}])' # Capture at least one vowel
'(.*)' # Capture zero of more of anything else
)
match = re.match(pattern, word)
if match:
p1 = match.group(1) or ''
p2 = match.group(2) or ''
p3 = match.group(3) or ''
return (p1, p2 + p3)
else:
return (word, '')
def test_stemmer():
""" Test stemmer() """
assert stemmer('') == ('', '')
assert stemmer('cake') == ('c', 'ake')
assert stemmer('chair') == ('ch', 'air')
assert stemmer('APPLE') == ('', 'apple')
assert stemmer('RDNZL') == ('rdnzl', '')
assert stemmer('123') == ('123', '')
# --------------------------------------------------
if __name__ == '__main__':
main()