diff --git a/PyDictionary/core.py b/PyDictionary/core.py index 1eb7298..e6aab04 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -80,62 +80,62 @@ def getAntonyms(self, formatted=True): @staticmethod def synonym(term, formatted=False): if len(term.split()) > 1: - print("Error: A Term must be only a single word") - else: - try: - data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) - section = data.find('div', {'class': 'type-synonym'}) - spans = section.findAll('a') - synonyms = [span.text.strip() for span in spans] - if formatted: - return {term: synonyms} - return synonyms - except: - print("{0} has no Synonyms in the API".format(term)) + term="+".join(term.split()) + + try: + data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) + section = data.find('div', {'class': 'type-synonym'}) + spans = section.findAll('a') + synonyms = [span.text.strip() for span in spans] + if formatted: + return {term: synonyms} + return synonyms + except: + print("{0} has no Synonyms in the API".format(term)) @staticmethod def antonym(term, formatted=False): if len(term.split()) > 1: - print("Error: A Term must be only a single word") - else: - try: - data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) - section = data.find('div', {'class': 'type-antonym'}) - spans = section.findAll('a') - antonyms = [span.text.strip() for span in spans] - if formatted: - return {term: antonyms} - return antonyms - except: - print("{0} has no Antonyms in the API".format(term)) + term="+".join(term.split()) + + try: + data = _get_soup_object("https://www.synonym.com/synonyms/{0}".format(term)) + section = data.find('div', {'class': 'type-antonym'}) + spans = section.findAll('a') + antonyms = [span.text.strip() for span in spans] + if formatted: + return {term: antonyms} + return antonyms + except: + print("{0} has no Antonyms in the API".format(term)) @staticmethod def meaning(term, disable_errors=False): if len(term.split()) > 1: - print("Error: A Term must be only a single word") - else: - try: - html = _get_soup_object("http://wordnetweb.princeton.edu/perl/webwn?s={0}".format( - term)) - types = html.findAll("h3") - length = len(types) - lists = html.findAll("ul") - out = {} - for a in types: - reg = str(lists[types.index(a)]) - meanings = [] - for x in re.findall(r'\((.*?)\)', reg): - if 'often followed by' in x: - pass - elif len(x) > 5 or ' ' in str(x): - meanings.append(x) - name = a.text - out[name] = meanings - return out - except Exception as e: - if disable_errors == False: - print("Error: The Following Error occured: %s" % e) + term="+".join(term.split()) + + try: + html = _get_soup_object("http://wordnetweb.princeton.edu/perl/webwn?s={0}".format( + term)) + types = html.findAll("h3") + length = len(types) + lists = html.findAll("ul") + out = {} + for a in types: + reg = str(lists[types.index(a)]) + meanings = [] + for x in re.findall(r'\((.*?)\)', reg): + if 'often followed by' in x: + pass + elif len(x) > 5 or ' ' in str(x): + meanings.append(x) + name = a.text + out[name] = meanings + return out + except Exception as e: + if disable_errors == False: + print("Error: The Following Error occured: %s" % e) if __name__ == '__main__': d = PyDictionary('honest','happy')