From a0a288d5126380fa8ff2bf2b7756ab15cc1e2335 Mon Sep 17 00:00:00 2001 From: 123vivekr <123vivekr@gmail.com> Date: Sat, 10 Sep 2022 12:52:27 +0530 Subject: [PATCH 1/2] meaning: support multiple word input --- PyDictionary/core.py | 46 +++++++++++++++---------------- PyDictionary/test_pydictionary.py | 1 + 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/PyDictionary/core.py b/PyDictionary/core.py index 1eb7298..74d2309 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -113,29 +113,29 @@ def antonym(term, formatted=False): @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') diff --git a/PyDictionary/test_pydictionary.py b/PyDictionary/test_pydictionary.py index aff1e61..f5c220b 100644 --- a/PyDictionary/test_pydictionary.py +++ b/PyDictionary/test_pydictionary.py @@ -9,6 +9,7 @@ class PyDictionaryTest(unittest.TestCase): def testMeaning(self): self.assertIsInstance(dictionary.meaning('python'),dict) + self.assertIsInstance(dictionary.meaning("neural network"), dict) def testSynonym(self): self.assertIsInstance(dictionary.synonym('happy'),list) def testAntonym(self): From 506ddf627bd0be196a95df1eb4a95908d775acd2 Mon Sep 17 00:00:00 2001 From: 123vivekr <123vivekr@gmail.com> Date: Sat, 10 Sep 2022 14:29:06 +0530 Subject: [PATCH 2/2] throw error on API fetch failure --- PyDictionary/core.py | 5 ++++- PyDictionary/exceptions.py | 5 +++++ PyDictionary/test_pydictionary.py | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 PyDictionary/exceptions.py diff --git a/PyDictionary/core.py b/PyDictionary/core.py index 74d2309..b6ee1c9 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -4,6 +4,7 @@ from .utils import _get_soup_object except: from utils import _get_soup_object +from exceptions import WordNotFound python2 = False if list(sys.version_info)[0] == 2: @@ -133,10 +134,12 @@ def meaning(term, disable_errors=False): name = a.text out[name] = meanings return out - except Exception as e: + except IndexError as e: if disable_errors == False: print("Error: The Following Error occured: %s" % e) + raise WordNotFound("Failed to fetch meaning from API") + if __name__ == '__main__': d = PyDictionary('honest','happy') d.printSynonyms() diff --git a/PyDictionary/exceptions.py b/PyDictionary/exceptions.py new file mode 100644 index 0000000..edde300 --- /dev/null +++ b/PyDictionary/exceptions.py @@ -0,0 +1,5 @@ +class WordNotFound(Exception): + def __init__(self, msg): + self.msg = msg + def __str__(self): + return self.msg \ No newline at end of file diff --git a/PyDictionary/test_pydictionary.py b/PyDictionary/test_pydictionary.py index f5c220b..f9ce7f3 100644 --- a/PyDictionary/test_pydictionary.py +++ b/PyDictionary/test_pydictionary.py @@ -3,6 +3,7 @@ from .__init__ import PyDictionary #Python 3 except: from __init__ import PyDictionary +from exceptions import WordNotFound dictionary=PyDictionary() @@ -10,6 +11,8 @@ class PyDictionaryTest(unittest.TestCase): def testMeaning(self): self.assertIsInstance(dictionary.meaning('python'),dict) self.assertIsInstance(dictionary.meaning("neural network"), dict) + self.assertRaises(WordNotFound, dictionary.meaning, "blies", disable_errors=False) + def testSynonym(self): self.assertIsInstance(dictionary.synonym('happy'),list) def testAntonym(self):