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

33 lines
903 B
Python
Raw Normal View History

2016-01-06 14:08:25 -05:00
import urllib
from bs4 import BeautifulSoup
import random
2018-10-05 16:02:38 -04:00
2016-01-06 14:08:25 -05:00
def define(word):
2018-10-05 16:02:38 -04:00
defs = []
2018-10-09 02:43:19 -04:00
url = "http://www.merriam-webster.com/dictionary/{}".format(word)
2018-10-05 16:02:38 -04:00
soup = BeautifulSoup(urllib.urlopen(url).read(), "html.parser")
head = soup.find("div", id="headword")
if head:
for p in head.find_all("p"):
defs.append(p.text.encode("ascii", "ignore"))
return defs
2016-01-06 14:08:25 -05:00
key = open("/home/krowbar/.secret/key").readline().rstrip()
2018-10-05 16:02:38 -04:00
def defWord(word, short=True):
2016-01-06 14:08:25 -05:00
defs = []
2018-10-09 02:43:19 -04:00
url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/{}?key={}".format(
word, key
2018-10-05 16:02:38 -04:00
)
soup = BeautifulSoup(urllib.urlopen(url).read(), "html5lib")
entry = soup.find("entry")
2016-01-06 14:08:25 -05:00
if entry:
2018-10-05 16:02:38 -04:00
for d in entry.find_all("dt"):
defs.append(d.text.encode("ascii", "ignore"))
2017-03-30 21:39:44 +00:00
if short:
2018-10-05 16:02:38 -04:00
return " ".join(defs)
2017-03-30 21:39:44 +00:00
else:
return defs