| 1 |
|
| 2 |
wn.morphy |
| 3 |
========= |
| 4 |
|
| 5 |
.. automodule:: wn.morphy |
| 6 |
|
| 7 |
.. seealso:: |
| 8 |
|
| 9 |
The Princeton WordNet `documentation |
| 10 |
<https://wordnet.princeton.edu/documentation/morphy7wn>`_ describes |
| 11 |
the original implementation of Morphy. |
| 12 |
|
| 13 |
The :doc:`../guides/lemmatization` guide describes how Wn handles |
| 14 |
lemmatization in general. |
| 15 |
|
| 16 |
|
| 17 |
Initialized and Uninitialized Morphy |
| 18 |
------------------------------------ |
| 19 |
|
| 20 |
There are two ways of using Morphy in Wn: initialized and |
| 21 |
uninitialized. |
| 22 |
|
| 23 |
Unintialized Morphy is a simple callable that returns lemma |
| 24 |
*candidates* for some given wordform. That is, the results might not |
| 25 |
be valid lemmas, but this is not a problem in practice because |
| 26 |
subsequent queries against the database will filter out the invalid |
| 27 |
ones. This callable is obtained by creating a :class:`Morphy` object |
| 28 |
with no arguments: |
| 29 |
|
| 30 |
>>> from wn import morphy |
| 31 |
>>> m = morphy.Morphy() |
| 32 |
|
| 33 |
As an uninitialized Morphy cannot predict which lemmas in the result |
| 34 |
are valid, it always returns the original form and any transformations |
| 35 |
it can find for each part of speech: |
| 36 |
|
| 37 |
>>> m('lemmata', pos='n') # exceptional form |
| 38 |
{'n': {'lemmata'}} |
| 39 |
>>> m('lemmas', pos='n') # regular morphology with part-of-speech |
| 40 |
{'n': {'lemma', 'lemmas'}} |
| 41 |
>>> m('lemmas') # regular morphology for any part-of-speech |
| 42 |
{None: {'lemmas'}, 'n': {'lemma'}, 'v': {'lemma'}} |
| 43 |
>>> m('wolves') # invalid forms may be returned |
| 44 |
{None: {'wolves'}, 'n': {'wolf', 'wolve'}, 'v': {'wolve', 'wolv'}} |
| 45 |
|
| 46 |
|
| 47 |
This lemmatizer can also be used with a :class:`wn.Wordnet` object to |
| 48 |
expand queries: |
| 49 |
|
| 50 |
>>> import wn |
| 51 |
>>> ewn = wn.Wordnet('ewn:2020') |
| 52 |
>>> ewn.words('lemmas') |
| 53 |
[] |
| 54 |
>>> ewn = wn.Wordnet('ewn:2020', lemmatizer=morphy.Morphy()) |
| 55 |
>>> ewn.words('lemmas') |
| 56 |
[Word('ewn-lemma-n')] |
| 57 |
|
| 58 |
An initialized Morphy is created with a :class:`wn.Wordnet` object as |
| 59 |
its argument. It then uses the wordnet to build lists of valid lemmas |
| 60 |
and exceptional forms (this takes a few seconds). Once this is done, |
| 61 |
it will only return lemmas it knows about: |
| 62 |
|
| 63 |
>>> ewn = wn.Wordnet('ewn:2020') |
| 64 |
>>> m = morphy.Morphy(ewn) |
| 65 |
>>> m('lemmata', pos='n') # exceptional form |
| 66 |
{'n': {'lemma'}} |
| 67 |
>>> m('lemmas', pos='n') # regular morphology with part-of-speech |
| 68 |
{'n': {'lemma'}} |
| 69 |
>>> m('lemmas') # regular morphology for any part-of-speech |
| 70 |
{'n': {'lemma'}} |
| 71 |
>>> m('wolves') # invalid forms are pre-filtered |
| 72 |
{'n': {'wolf'}} |
| 73 |
|
| 74 |
In order to use an initialized Morphy lemmatizer with a |
| 75 |
:class:`wn.Wordnet` object, it must be assigned to the object after |
| 76 |
creation: |
| 77 |
|
| 78 |
>>> ewn = wn.Wordnet('ewn:2020') # default: lemmatizer=None |
| 79 |
>>> ewn.words('lemmas') |
| 80 |
[] |
| 81 |
>>> ewn.lemmatizer = morphy.Morphy(ewn) |
| 82 |
>>> ewn.words('lemmas') |
| 83 |
[Word('ewn-lemma-n')] |
| 84 |
|
| 85 |
There is little to no difference in the results obtained from a |
| 86 |
:class:`wn.Wordnet` object using an initialized or uninitialized |
| 87 |
:class:`Morphy` object, but there may be slightly different |
| 88 |
performance profiles for future queries. |
| 89 |
|
| 90 |
|
| 91 |
Default Morphy Lemmatizer |
| 92 |
------------------------- |
| 93 |
|
| 94 |
As a convenience, an uninitialized Morphy lemmatizer is provided in |
| 95 |
this module via the :data:`morphy` member. |
| 96 |
|
| 97 |
.. data:: morphy |
| 98 |
|
| 99 |
A :class:`Morphy` object created without a :class:`wn.Wordnet` |
| 100 |
object. |
| 101 |
|
| 102 |
|
| 103 |
The Morphy Class |
| 104 |
---------------- |
| 105 |
|
| 106 |
.. autoclass:: Morphy |