From 12d0d881a4aee0076801c815dd1165a856efc887 Mon Sep 17 00:00:00 2001 From: hp0404 Date: Fri, 25 Jun 2021 00:44:10 +0300 Subject: [PATCH 1/2] return the smallest form using min() --- word_forms/lemmatizer.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/word_forms/lemmatizer.py b/word_forms/lemmatizer.py index 7f509c1..217a8dc 100644 --- a/word_forms/lemmatizer.py +++ b/word_forms/lemmatizer.py @@ -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) From eca7104994821943da09a550a6c100525ae48cf9 Mon Sep 17 00:00:00 2001 From: hp0404 Date: Fri, 25 Jun 2021 00:50:14 +0300 Subject: [PATCH 2/2] use context manager to open files --- word_forms/constants.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/word_forms/constants.py b/word_forms/constants.py index 98c84b3..188ff63 100644 --- a/word_forms/constants.py +++ b/word_forms/constants.py @@ -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): @@ -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()