File size: 1,655 Bytes
d916065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Natural Language Toolkit: WordNet stemmer interface
#
# Copyright (C) 2001-2023 NLTK Project
# Author: Steven Bird <[email protected]>
#         Edward Loper <[email protected]>
# URL: <https://www.nltk.org/>
# For license information, see LICENSE.TXT

from nltk.corpus import wordnet as wn


class WordNetLemmatizer:
    """

    WordNet Lemmatizer



    Lemmatize using WordNet's built-in morphy function.

    Returns the input word unchanged if it cannot be found in WordNet.



        >>> from nltk.stem import WordNetLemmatizer

        >>> wnl = WordNetLemmatizer()

        >>> print(wnl.lemmatize('dogs'))

        dog

        >>> print(wnl.lemmatize('churches'))

        church

        >>> print(wnl.lemmatize('aardwolves'))

        aardwolf

        >>> print(wnl.lemmatize('abaci'))

        abacus

        >>> print(wnl.lemmatize('hardrock'))

        hardrock

    """

    def lemmatize(self, word: str, pos: str = "n") -> str:
        """Lemmatize `word` using WordNet's built-in morphy function.

        Returns the input word unchanged if it cannot be found in WordNet.



        :param word: The input word to lemmatize.

        :type word: str

        :param pos: The Part Of Speech tag. Valid options are `"n"` for nouns,

            `"v"` for verbs, `"a"` for adjectives, `"r"` for adverbs and `"s"`

            for satellite adjectives.

        :param pos: str

        :return: The lemma of `word`, for the given `pos`.

        """
        lemmas = wn._morphy(word, pos)
        return min(lemmas, key=len) if lemmas else word

    def __repr__(self):
        return "<WordNetLemmatizer>"