Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
23 changes: 11 additions & 12 deletions word_forms/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from collections import defaultdict
from pathlib import Path
import json
from pathlib import Path
from collections import defaultdict


ABSOLUTE_PATH = Path(__file__).parent.absolute()
ADJECTIVE_TO_ADVERB = json.loads((ABSOLUTE_PATH / "adj_to_adv.txt").read_text())


class Verb(object):
def __init__(self, verbs=None):
Expand All @@ -13,17 +18,11 @@ def update(self, verbs):
self.verbs.update(verbs)


verbs_fh = open(str(Path(__file__).parent.absolute() / "en-verbs.txt"))
lines = verbs_fh.readlines()
verbs_fh.close()

CONJUGATED_VERB_DICT = defaultdict(Verb)
for line in lines:
if line[0] != ";":
with open(ABSOLUTE_PATH / "en-verbs.txt") as lines:
for line in lines:
if line.startswith(";"):
continue
verbs = {string for string in line.strip().split(",") if string != ""}
for verb in verbs:
CONJUGATED_VERB_DICT[verb].update(verbs)

adj_fh = open(str(Path(__file__).parent.absolute() / "adj_to_adv.txt"))
ADJECTIVE_TO_ADVERB = json.load(adj_fh)
adj_fh.close()
11 changes: 3 additions & 8 deletions word_forms/lemmatizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ def lemmatize(word):
"""
Out of all the related word forms of ``word``, return the smallest form that appears first in the dictionary
"""
forms = [word for pos_form in get_word_forms(word).values() for word in pos_form]
forms.sort()
forms.sort(key=len)
try:
return forms[0]
except IndexError:
forms = get_word_forms(word).values()
if not any(forms):
raise ValueError("{} is not a real word".format(word))


return min([word for pos_form in forms for word in pos_form], key=len)