Refactored rhyme.py
parent
c3f29a3993
commit
b6901d4604
21
rhyme.py
21
rhyme.py
|
@ -1,17 +1,24 @@
|
||||||
|
import itertools
|
||||||
from Phyme import Phyme
|
from Phyme import Phyme
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
random.seed(time.clock())
|
random.seed(time.time())
|
||||||
|
|
||||||
ph = Phyme()
|
ph = Phyme()
|
||||||
|
|
||||||
rhymes = ph.get_perfect_rhymes(sys.argv[1])
|
def get_rhyme(word):
|
||||||
rhyme_vals = []
|
# Get all rhymes and merge to one list (normally separated by syllable count)
|
||||||
|
rhymes = ph.get_perfect_rhymes(word)
|
||||||
|
rhyme_vals = list(itertools.chain.from_iterable(list(rhymes.values())))
|
||||||
|
|
||||||
for arr in rhymes.values():
|
# Pick a random rhyme and strip out any non alpha characters
|
||||||
for rhyme in arr:
|
rhymed_word = rhyme_vals[random.randint(0, len(rhyme_vals) - 1)]
|
||||||
rhyme_vals.append(rhyme)
|
rhymed_word = ''.join(letter for letter in rhymed_word if letter.isalpha())
|
||||||
|
|
||||||
print(rhyme_vals[random.randint(0, len(rhyme_vals))].capitalize())
|
return rhymed_word.capitalize()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(get_rhyme(sys.argv[1]))
|
||||||
|
|
Loading…
Reference in New Issue