Files
tilde-projects/Code/irc/rhymesWith.py
T

40 lines
1.0 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2016-01-06 14:08:25 -05:00
import urllib
2018-10-05 16:02:38 -04:00
# from lxml.html import fromstring
2016-01-06 14:08:25 -05:00
from bs4 import BeautifulSoup
import random
2018-10-05 16:02:38 -04:00
2016-01-06 14:08:25 -05:00
def getRhymes(word):
2018-10-05 16:02:38 -04:00
words = []
url = "http://www.rhymer.com/RhymingDictionaryLast/%s.html" % word
soup = BeautifulSoup(urllib.request.urlopen(url).read(), "html.parser")
2018-10-05 16:02:38 -04:00
for t in soup.find_all("table", "table"):
words.append(
random.choice(
[
w
for w in t.text.split("\n")
if w not in [u"", u"\xa0"] and "-" not in w
]
)
)
return words
2016-01-06 14:08:25 -05:00
def rhymeZone(word):
words = []
2018-10-05 16:02:38 -04:00
url = (
"http://rhymezone.com/r/rhyme.cgi?Word=%s&typeofrhyme=perfect&org1=syl&org2=l&org3=y"
% word
)
soup = BeautifulSoup(urllib.request.urlopen(url).read(), "html.parser")
2016-01-06 14:08:25 -05:00
2018-10-05 16:02:38 -04:00
for t in soup.find_all("a", "d"):
2016-01-06 14:08:25 -05:00
w = t.text.rstrip()
2018-10-05 16:02:38 -04:00
if w not in [u"", u"\xa0"] and "?" not in t:
words.append(w)
2016-01-06 14:08:25 -05:00
random.shuffle(words)
return words[0:5]