Debian Codegraph @

docs/guides/lemmatization.rst

raw
1
2 Lemmatization and Normalization
3 ===============================
4
5 Wn provides two methods for expanding queries: lemmatization_ and
6 normalization_\ . Wn also has a setting that allows `alternative forms
7 <alternative-forms_>`_ stored in the database to be included in
8 queries.
9
10 .. seealso::
11
12 The :mod:`wn.morphy` module is a basic English lemmatizer included
13 with Wn.
14
15 .. _lemmatization:
16
17 Lemmatization
18 -------------
19
20 When querying a wordnet with wordforms from natural language text, it
21 is important to be able to find entries for inflected forms as the
22 database generally contains only lemmatic forms, or *lemmas* (or
23 *lemmata*, if you prefer irregular plurals).
24
25 >>> import wn
26 >>> en = wn.Wordnet('oewn:2021')
27 >>> en.words('plurals')
28 []
29 >>> en.words('plural')
30 [Word('oewn-plural-a'), Word('oewn-plural-n')]
31
32 Lemmas are sometimes called *citation forms* or *dictionary forms* as
33 they are often used as the head words in dictionary entries. In
34 Natural Language Processing (NLP), *lemmatization* is a technique
35 where a possibly inflected word form is transformed to yield a
36 lemma. In Wn, this concept is generalized somewhat to mean a
37 transformation that yields a form matching wordforms stored in the
38 database. For example, the English word *sparrows* is the plural
39 inflection of *sparrow*, while the word *leaves* is ambiguous between
40 the plural inflection of the nouns *leaf* and *leave* and the
41 3rd-person singular inflection of the verb *leave*.
42
43 For tasks where high-accuracy is needed, wrapping the wordnet queries
44 with external tools that handle tokenization, lemmatization, and
45 part-of-speech tagging will likely yield the best results as this
46 method can make use of word context. That is, something like this:
47
48 .. code-block:: python
49
50 for lemma, pos in fancy_shmancy_analysis(corpus):
51 synsets = w.synsets(lemma, pos=pos)
52
53 For modest needs, however, Wn provides a way to integrate basic
54 lemmatization directly into the queries.
55
56 Lemmatization in Wn works as follows: if a :class:`wn.Wordnet` object
57 is instantiated with a *lemmatizer* argument, then queries involving
58 wordforms (e.g., :meth:`wn.Wordnet.words`, :meth:`wn.Wordnet.senses`,
59 :meth:`wn.Wordnet.synsets`) will first lemmatize the wordform and then
60 check all resulting wordforms and parts of speech against the
61 database as successive queries.
62
63 Lemmatization Functions
64 '''''''''''''''''''''''
65
66 The *lemmatizer* argument of :class:`wn.Wordnet` is a callable that
67 takes two string arguments: (1) the original wordform, and (2) a
68 part-of-speech or :python:`None`. It returns a dictionary mapping
69 parts-of-speech to sets of lemmatized wordforms. The signature is as
70 follows:
71
72 .. code-block:: python
73
74 lemmatizer(s: str, pos: str | None) -> Dict[str | None, Set[str]]
75
76 The part-of-speech may be used by the function to determine which
77 morphological rules to apply. If the given part-of-speech is
78 :python:`None`, then it is not specified and any rule may apply. A
79 lemmatizer that only deinflects should not change any specified
80 part-of-speech, but this is not a requirement, and a function could be
81 provided that undoes derivational morphology (e.g., *democratic* →
82 *democracy*).
83
84 Querying With Lemmatization
85 '''''''''''''''''''''''''''
86
87 As the needs of lemmatization differs from one language to another, Wn
88 does not provide a lemmatizer by default, and therefore it is
89 unavailable to the convenience functions :func:`wn.words`,
90 :func:`wn.senses`, and :func:`wn.synsets`. A lemmatizer can be added
91 to a :class:`wn.Wordnet` object. For example, using :mod:`wn.morphy`:
92
93 >>> import wn
94 >>> from wn.morphy import Morphy
95 >>> en = wn.Wordnet('oewn:2021', lemmatizer=Morphy())
96 >>> en.words('sparrows')
97 [Word('oewn-sparrow-n')]
98 >>> en.words('leaves')
99 [Word('oewn-leave-v'), Word('oewn-leaf-n'), Word('oewn-leave-n')]
100
101 Querying Without Lemmatization
102 ''''''''''''''''''''''''''''''
103
104 When lemmatization is not used, inflected terms may not return any
105 results:
106
107 >>> en = wn.Wordnet('oewn:2021')
108 >>> en.words('sparrows')
109 []
110
111 Depending on the lexicon, there may be situations where results are
112 returned for inflected lemmas, such as when the inflected form is
113 lexicalized as its own entry:
114
115 >>> en.words('glasses')
116 [Word('oewn-glasses-n')]
117
118 Or if the lexicon lists the inflected form as an alternative form. For
119 example, the English Wordnet lists irregular inflections as
120 alternative forms:
121
122 >>> en.words('lemmata')
123 [Word('oewn-lemma-n')]
124
125 See below for excluding alternative forms from such queries.
126
127 .. _alternative-forms:
128
129 Alternative Forms in the Database
130 ---------------------------------
131
132 A lexicon may include alternative forms in addition to lemmas for each
133 word, and by default these are included in queries. What exactly is
134 included as an alternative form depends on the lexicon. The English
135 Wordnet, for example, adds irregular inflections (or "exceptional
136 forms"), while the Japanese Wordnet includes the same word in multiple
137 orthographies (original, hiragana, katakana, and two romanizations).
138 For the English Wordnet, this means that you might get basic
139 lemmatization for irregular forms only:
140
141 >>> en = wn.Wordnet('oewn:2021')
142 >>> en.words('learnt', pos='v')
143 [Word('oewn-learn-v')]
144 >>> en.words('learned', pos='v')
145 []
146
147 If this is undesirable, the alternative forms can be excluded from
148 queries with the *search_all_forms* parameter:
149
150 >>> en = wn.Wordnet('oewn:2021', search_all_forms=False)
151 >>> en.words('learnt', pos='v')
152 []
153 >>> en.words('learned', pos='v')
154 []
155
156 .. _normalization:
157
158 Normalization
159 -------------
160
161 While lemmatization deals with morphological variants of words,
162 normalization handles minor orthographic variants. Normalized forms,
163 however, may be invalid as wordforms in the target language, and as
164 such they are only used behind the scenes for query expansion and not
165 presented to users. For instance, a user might attempt to look up
166 *résumé* in the English wordnet, but the wordnet only contains the
167 form without diacritics: *resume*. With strict string matching, the
168 entry would not be found using the wordform in the query. By
169 normalizing the query word, the entry can be found. Similarly in the
170 Spanish wordnet, *soñar* (to dream) and *sonar* (to ring) are two
171 different words. A user who types *soñar* likely does not want to get
172 results for *sonar*, but one who types *sonar* may be a non-Spanish
173 speaker who is unaware of the missing diacritic or does not have an
174 input method that allows them to type the diacritic, so this query
175 would return both entries by matching against the normalized forms in
176 the database. Wn handles all of these use cases.
177
178 When a lexicon is added to the database, potentially two wordforms are
179 inserted for every one in the lexicon: the original wordform and a
180 normalized form. When querying against the database, the original
181 query string is first compared with the original wordforms and, if
182 normalization is enabled, with the normalized forms in the database as
183 well. If this first attempt yields no results and if normalization is
184 enabled, the query string is normalized and tried again.
185
186 Normalization Functions
187 '''''''''''''''''''''''
188
189 The normalized form is obtained from a *normalizer* function, passed
190 as an argument to :class:`wn.Wordnet`, that takes a single string
191 argument and returns a string. That is, a function with the following
192 signature:
193
194 .. code-block:: python
195
196 normalizer(s: str) -> str
197
198 While custom *normalizer* functions could be used, in practice the
199 choice is either the default normalizer or :python:`None`. The default
200 normalizer works by downcasing the string and performing NFKD_
201 normalization to remove diacritics. If the normalized form is the same
202 as the original, only the original is inserted into the database.
203
204 .. table:: Examples of normalization
205 :align: center
206
207 ============= ===============
208 Original Form Normalized Form
209 ============= ===============
210 résumé resume
211 soñar sonar
212 San José san jose
213 ハラペーニョ ハラヘーニョ
214 ============= ===============
215
216 .. _NFKD: https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms
217
218 Querying With Normalization
219 '''''''''''''''''''''''''''
220
221 By default, normalization is enabled when a :class:`wn.Wordnet` is
222 created. Enabling normalization does two things: it allows queries to
223 check the original wordform in the query against the normalized forms
224 in the database and, if no results are returned in the first step, it
225 allows the queried wordform to be normalized as a back-off technique.
226
227 >>> en = wn.Wordnet('oewn:2021')
228 >>> en.words('résumé')
229 [Word('oewn-resume-n'), Word('oewn-resume-v')]
230 >>> es = wn.Wordnet('omw-es:1.4')
231 >>> es.words('soñar')
232 [Word('omw-es-soñar-v')]
233 >>> es.words('sonar')
234 [Word('omw-es-sonar-v'), Word('omw-es-soñar-v')]
235
236 .. note::
237
238 Users may supply a custom *normalizer* function to the
239 :class:`wn.Wordnet` object, but currently this is discouraged as
240 the result is unlikely to match normalized forms in the database
241 and there is not yet a way to customize the normalization of forms
242 added to the database.
243
244 Querying Without Normalization
245 ''''''''''''''''''''''''''''''
246
247 Normalization can be disabled by passing :python:`None` as the
248 argument of the *normalizer* parameter of :class:`wn.Wordnet`. The
249 queried wordform will not be checked against normalized forms in the
250 database and neither will it be normalized as a back-off technique.
251
252 >>> en = wn.Wordnet('oewn:2021', normalizer=None)
253 >>> en.words('résumé')
254 []
255 >>> es = wn.Wordnet('omw-es:1.4', normalizer=None)
256 >>> es.words('soñar')
257 [Word('omw-es-soñar-v')]
258 >>> es.words('sonar')
259 [Word('omw-es-sonar-v')]
260
261 .. note::
262
263 It is not possible to disable normalization for the convenience
264 functions :func:`wn.words`, :func:`wn.senses`, and
265 :func:`wn.synsets`.