From f4a476324237381d2cec3ed79dc9cd9b481dfaa5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 11 Jul 2018 20:36:00 +0800 Subject: [PATCH 1/2] Use regex and json to parse synonyms --- PyDictionary/core.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/PyDictionary/core.py b/PyDictionary/core.py index e63d8dc..6640801 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -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: @@ -19,7 +19,7 @@ def __init__(self, *args): except: self.args = args - + def printMeanings(self): dic = self.getMeanings() for key in dic.keys(): @@ -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.JSONDecoder().decode(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 From b1ee5068d82b67c6813802ea7bde58f16367548f Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 11 Jul 2018 20:37:29 +0800 Subject: [PATCH 2/2] Use regex and json to parse antonyms --- PyDictionary/core.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/PyDictionary/core.py b/PyDictionary/core.py index 6640801..65b8c88 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -70,7 +70,7 @@ def synonym(term, formatted=False): try: data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(term)) re_synonyms = re.search('"synonyms":(\[.*?\])', data.get_text()).group(1) - synonyms = json.JSONDecoder().decode(re_synonyms) + synonyms = json.loads(re_synonyms) if len(synonyms) > 5: synonyms = synonyms[:5:] li = [] @@ -101,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