Skip to content

Commit 2fa4924

Browse files
committed
Add morphy modes to WordNetLemmatizer
1 parent db66575 commit 2fa4924

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

nltk/stem/wordnet.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (C) 2001-2023 NLTK Project
44
# Author: Steven Bird <[email protected]>
55
# Edward Loper <[email protected]>
6+
# Eric Kafe <[email protected]>
67
# URL: <https://www.nltk.org/>
78
# For license information, see LICENSE.TXT
89

@@ -13,9 +14,45 @@ class WordNetLemmatizer:
1314
"""
1415
WordNet Lemmatizer
1516
16-
Lemmatize by picking the shortest of the possible lemmas,
17-
using the wordnet corpus reader's built-in _morphy function.
18-
Returns the input word unchanged if it cannot be found in WordNet.
17+
Provides 3 lemmatizer modes:
18+
19+
1. _morphy() is an alias to WordNet's _morphy lemmatizer.
20+
It returns a list of all lemmas found in WordNet.
21+
22+
>>> wnl = WordNetLemmatizer()
23+
>>> print(wnl._morphy('us', 'n'))
24+
['us', 'u']
25+
26+
2. morphy() is a restrictive wrapper around _morphy().
27+
It returns the first lemma found in WordNet,
28+
or None if no lemma is found.
29+
30+
>>> print(wnl.morphy('us', 'n'))
31+
us
32+
33+
>>> print(wnl.morphy('catss'))
34+
None
35+
36+
3. lemmatize() is a permissive wrapper around _morphy().
37+
It returns the shortest lemma found in WordNet,
38+
or the input string unchanged if nothing is found.
39+
40+
>>> print(wnl.lemmatize('us', 'n'))
41+
u
42+
43+
>>> print(wnl.lemmatize('Anythinggoeszxcv'))
44+
Anythinggoeszxcv
45+
46+
"""
47+
48+
morphy = wn.morphy
49+
50+
_morphy = wn._morphy
51+
52+
def lemmatize(self, word: str, pos: str = "n") -> str:
53+
"""Lemmatize `word` by picking the shortest of the possible lemmas,
54+
using the wordnet corpus reader's built-in _morphy function.
55+
Returns the input word unchanged if it cannot be found in WordNet.
1956
2057
>>> from nltk.stem import WordNetLemmatizer
2158
>>> wnl = WordNetLemmatizer()
@@ -29,12 +66,6 @@ class WordNetLemmatizer:
2966
abacus
3067
>>> print(wnl.lemmatize('hardrock'))
3168
hardrock
32-
"""
33-
34-
def lemmatize(self, word: str, pos: str = "n") -> str:
35-
"""Lemmatize `word` by picking the shortest of the possible lemmas,
36-
using the wordnet corpus reader's built-in _morphy function.
37-
Returns the input word unchanged if it cannot be found in WordNet.
3869
3970
:param word: The input word to lemmatize.
4071
:type word: str
@@ -44,7 +75,7 @@ def lemmatize(self, word: str, pos: str = "n") -> str:
4475
:type pos: str
4576
:return: The shortest lemma of `word`, for the given `pos`.
4677
"""
47-
lemmas = wn._morphy(word, pos)
78+
lemmas = self._morphy(word, pos)
4879
return min(lemmas, key=len) if lemmas else word
4980

5081
def __repr__(self):

0 commit comments

Comments
 (0)