Skip to content

Commit db66575

Browse files
committed
Optimize morphy()
1 parent 36fa928 commit db66575

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

nltk/corpus/reader/wordnet.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,8 +2018,8 @@ def morphy(self, form, pos=None, check_exceptions=True):
20182018
"""
20192019
Find a possible base form for the given form, with the given
20202020
part of speech, by checking WordNet's list of exceptional
2021-
forms, and by recursively stripping affixes for this part of
2022-
speech until a form in WordNet is found.
2021+
forms, or by substituting affixes for this part of speech.
2022+
Return the first form found in WordNet, or eventually None.
20232023
20242024
>>> from nltk.corpus import wordnet as wn
20252025
>>> print(wn.morphy('dogs'))
@@ -2037,17 +2037,17 @@ def morphy(self, form, pos=None, check_exceptions=True):
20372037
"""
20382038

20392039
if pos is None:
2040-
morphy = self._morphy
2041-
analyses = chain(a for p in POS_LIST for a in morphy(form, p))
2040+
posl = POS_LIST
20422041
else:
2042+
posl = [pos]
2043+
analyses = []
2044+
for pos in posl:
20432045
analyses = self._morphy(form, pos, check_exceptions)
2044-
2045-
# get the first one we find
2046-
first = list(islice(analyses, 1))
2047-
if len(first) == 1:
2048-
return first[0]
2049-
else:
2050-
return None
2046+
if analyses:
2047+
return analyses[0]
2048+
else:
2049+
continue
2050+
return None
20512051

20522052
MORPHOLOGICAL_SUBSTITUTIONS = {
20532053
NOUN: [
@@ -2096,7 +2096,7 @@ def apply_rules(forms):
20962096
]
20972097

20982098
def filter_forms(forms):
2099-
result = [] # Return an empty list if we can't find anything
2099+
result = []
21002100
seen = set()
21012101
for form in forms:
21022102
if form in self._lemma_pos_offset_map:

0 commit comments

Comments
 (0)