Skip to content
Open
Show file tree
Hide file tree
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
49 changes: 26 additions & 23 deletions PyDictionary/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -113,29 +114,31 @@ 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 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')
Expand Down
5 changes: 5 additions & 0 deletions PyDictionary/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class WordNotFound(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
4 changes: 4 additions & 0 deletions PyDictionary/test_pydictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
from .__init__ import PyDictionary #Python 3
except:
from __init__ import PyDictionary
from exceptions import WordNotFound

dictionary=PyDictionary()

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):
Expand Down