This program is basically a rhyming dictionary. It makes use of Sphinx's groups phonemic dictionary of English which includes a lot of words broken into their respective phonemes, and if we assume that, at least for most cases, anytime the two last phonemes match, the two words rhyme, then it's very easy to whip up a program that does so. Here's my try at this:
import requests
import os
import re
def load_library():
with open('library.txt', 'r') as library:
lines = library.readlines()
return [x for x in lines if not x.startswith(';;;')]
def rhyme(string):
if os.path.isfile('library.txt'):
library = load_library()
else:
url = "http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b"
response = requests.get(url, stream=True)
if not response.ok:
print("Error loading library")
with open('library.txt', 'wb+') as library:
for block in response.iter_content(32):
library.write(block)
library = load_library()
string = string.upper()
phonemes = [y for y in library if y.startswith(string)][0][len(string):]
phonemes_split = str.strip(phonemes).split(' ')
matches = [z for z in library if z.endswith(phonemes_split[-2] + ' ' + phonemes_split[-1] + '\n')]
matches = [z.strip().split(' ') for z in matches]
return matches
Of course it's naive to assume that EVERYTIME the last two phonemes of two different words match, they rhyme, but most of the times, they do.
This program returns a list of matching words broken into their respective phonemes.
I spent two years studying English literature before I dropped out in favor of pursuing computer programming so I'm qualified to say that I barely know what a phoneme is.