@@ -1421,6 +1421,7 @@ def _next_token():
14211421 # map lemmas and parts of speech to synsets
14221422 self ._lemma_pos_offset_map [lemma ][pos ] = synset_offsets
14231423 if pos == ADJ :
1424+ # Duplicate all adjectives indiscriminately?:
14241425 self ._lemma_pos_offset_map [lemma ][ADJ_SAT ] = synset_offsets
14251426
14261427 def _load_exception_map (self ):
@@ -2016,8 +2017,9 @@ def morphy(self, form, pos=None, check_exceptions=True):
20162017 """
20172018 Find a possible base form for the given form, with the given
20182019 part of speech, by checking WordNet's list of exceptional
2019- forms, and by recursively stripping affixes for this part of
2020- speech until a form in WordNet is found.
2020+ forms, or by substituting suffixes for this part of speech.
2021+ If pos=None, try every part of speech until finding lemmas.
2022+ Return the first form found in WordNet, or eventually None.
20212023
20222024 >>> from nltk.corpus import wordnet as wn
20232025 >>> print(wn.morphy('dogs'))
@@ -2033,19 +2035,11 @@ def morphy(self, form, pos=None, check_exceptions=True):
20332035 book
20342036 >>> wn.morphy('book', wn.ADJ)
20352037 """
2036-
2037- if pos is None :
2038- morphy = self ._morphy
2039- analyses = chain (a for p in POS_LIST for a in morphy (form , p ))
2040- else :
2038+ for pos in [pos ] if pos else POS_LIST :
20412039 analyses = self ._morphy (form , pos , check_exceptions )
2042-
2043- # get the first one we find
2044- first = list (islice (analyses , 1 ))
2045- if len (first ) == 1 :
2046- return first [0 ]
2047- else :
2048- return None
2040+ if analyses :
2041+ # Stop (don't try more parts of speech):
2042+ return analyses [0 ]
20492043
20502044 MORPHOLOGICAL_SUBSTITUTIONS = {
20512045 NOUN : [
@@ -2080,8 +2074,7 @@ def _morphy(self, form, pos, check_exceptions=True):
20802074 # Given an original string x
20812075 # 1. Apply rules once to the input to get y1, y2, y3, etc.
20822076 # 2. Return all that are in the database
2083- # 3. If there are no matches, keep applying rules until you either
2084- # find a match or you can't go any further
2077+ # (edited by ekaf) If there are no matches return an empty list.
20852078
20862079 exceptions = self ._exception_map [pos ]
20872080 substitutions = self .MORPHOLOGICAL_SUBSTITUTIONS [pos ]
@@ -2105,28 +2098,15 @@ def filter_forms(forms):
21052098 seen .add (form )
21062099 return result
21072100
2108- # 0. Check the exception lists
2109- if check_exceptions :
2110- if form in exceptions :
2111- return filter_forms ([form ] + exceptions [form ])
2112-
2113- # 1. Apply rules once to the input to get y1, y2, y3, etc.
2114- forms = apply_rules ([form ])
2101+ if check_exceptions and form in exceptions :
2102+ # 0. Check the exception lists
2103+ forms = exceptions [form ]
2104+ else :
2105+ # 1. Apply rules once to the input to get y1, y2, y3, etc.
2106+ forms = apply_rules ([form ])
21152107
21162108 # 2. Return all that are in the database (and check the original too)
2117- results = filter_forms ([form ] + forms )
2118- if results :
2119- return results
2120-
2121- # 3. If there are no matches, keep applying rules until we find a match
2122- while forms :
2123- forms = apply_rules (forms )
2124- results = filter_forms (forms )
2125- if results :
2126- return results
2127-
2128- # Return an empty list if we can't find anything
2129- return []
2109+ return filter_forms ([form ] + forms )
21302110
21312111 #############################################################
21322112 # Create information content from corpus
0 commit comments