Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions PyDictionary/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
import sys, re, goslate
import sys, re, goslate, json
try:
from .utils import _get_soup_object
except:
Expand All @@ -19,7 +19,7 @@ def __init__(self, *args):
except:
self.args = args


def printMeanings(self):
dic = self.getMeanings()
for key in dic.keys():
Expand Down Expand Up @@ -69,12 +69,13 @@ def synonym(term, formatted=False):
else:
try:
data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(term))
terms = data.select("div#filters-0")[0].findAll("li")
if len(terms) > 5:
terms = terms[:5:]
re_synonyms = re.search('"synonyms":(\[.*?\])', data.get_text()).group(1)
synonyms = json.loads(re_synonyms)
if len(synonyms) > 5:
synonyms = synonyms[:5:]
li = []
for t in terms:
li.append(t.select("span.text")[0].getText())
for s in synonyms:
li.append(s["term"])
if formatted:
return {term: li}
return li
Expand All @@ -100,12 +101,13 @@ def antonym(word, formatted=False):
else:
try:
data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(word))
terms = data.select("section.antonyms")[0].findAll("li")
if len(terms) > 5:
terms = terms[:5:]
re_antonyms = re.search('"antonyms":(\[.*?\])', data.get_text()).group(1)
antonyms = json.loads(re_antonyms)
if len(antonyms) > 5:
antonyms = antonyms[:5:]
li = []
for t in terms:
li.append(t.select("span.text")[0].getText())
for a in antonyms:
li.append(a["term"])
if formatted:
return {word: li}
return li
Expand Down