-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhymer.py
135 lines (113 loc) · 4.22 KB
/
rhymer.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# import libraries
import requests
import re
import argparse
# We can only use RHYMEZONE
DATAMUSE_API = "https://api.datamuse.com/words"
RHYMEZONE_API = "http:https://api.rhymezone.com/words?k=rza&arhy=1"
def get_ryhmes(word: str, option='rel_rhy', max=100, api_endoint="https://api.datamuse.com/words"):
""" getting all rhyming words from the
Args:
word (string): word as a string (no \n)
Returns:
list(str): list of words
"""
parameter = {}
parameter[option] = word
parameter['max'] = max
request = requests.get(api_endoint, parameter)
print(f"checking [{option}] for {word}...")
rhyme = request.json()
return [elt['word'] for elt in rhyme]
# getting all ryhmes from api.datamuse.com
def get_last_word(sentence: str):
"""getting last word of a sentence
Args:
sentence (str): _description_
Returns:
_type_: _description_
"""
sentence = sentence.replace(')', '')
sentence = sentence.replace('(', '')
sentence = sentence.replace('.', ' ')
return re.split(r'\W+', sentence)[-1]
def main(filename, output_file='output.txt', nry=False):
output = []
i = 0
with open(filename) as f:
lines = f.readlines()
lines = [line for line in lines if len(line) > 2]
while len(lines) > 0:
last_word = get_last_word(lines[0].strip('\n'))
rhymes = get_ryhmes(last_word)
output.append([])
output[i].append(lines[0])
lines.remove(lines[0])
lines_swap = lines.copy()
for s in lines_swap:
lw = get_last_word(s.strip('\n'))
if lw in rhymes or lw == last_word:
lines.remove(s)
output[i].append(s)
i += 1
# sorting based on number of rhyming sentences
output.sort(key=len, reverse=True)
# output has lines
# near rhymes section
if nry:
print("[INFO] checking near rhymes.")
separator = 0
for separator, elt in enumerate(output):
if len(elt) == 1:
break
multies = output[:separator]
singles = output[separator:]
mul = multies.copy()
sin = singles.copy()
for single in singles:
get_out = False
single_last_word = get_last_word(single[0].strip('\n'))
near_rhymes = get_ryhmes(
single_last_word, 'sl', 1000, RHYMEZONE_API)[1:]
for i, multi in enumerate(multies):
if not get_out:
for line in multi:
line_last_word = get_last_word(line.strip('\n'))
for word in near_rhymes:
if word == line_last_word:
mul[i].append(single[0])
sin.remove(single)
get_out = True
break
else:
continue
break
else:
break
output = mul + sin
output.sort(key=len, reverse=True)
# output part
with open(output_file, 'w') as f:
for lines in output:
for line in lines:
f.write(line + '\n'*('\n' not in line))
f.write('\n')
# for elt in output:
# print(elt)
# input()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog='rhymer.py',
description='RhymerV0.2 script will sort sentences acording to their rhymes and near rhymes.',
epilog='This script uses datamuse.com and rhymezone APIs.')
parser.add_argument('input_filename', help="Input file containing lines.")
parser.add_argument('output_filename', default='output.txt',
help="output file where the sorted poem is stored.")
parser.add_argument('-no_sl', action='store_false',
help="Will not use the [sl] Sound like API calls. (ie. no near rhymes)")
args = parser.parse_args()
try:
main(args.input_filename, output_file=args.output_filename, nry=args.no_sl)
print("[OK] sorted in output.txt")
except:
print("usage: python rhymer.py sample.txt")