| 1 |
Interlingual Queries |
| 2 |
==================== |
| 3 |
|
| 4 |
This guide explains how interlingual queries work within Wn. To get |
| 5 |
started, you'll need at least two lexicons that use interlingual |
| 6 |
indices (ILIs). For this guide, we'll use the Open English WordNet |
| 7 |
(``oewn:2024``), the Open German WordNet (``odenet:1.4``), also |
| 8 |
known as OdeNet, and the Japanese wordnet (``omw-ja:1.4``). |
| 9 |
|
| 10 |
>>> import wn |
| 11 |
>>> wn.download('oewn:2024') |
| 12 |
>>> wn.download('odenet:1.4') |
| 13 |
>>> wn.download('omw-ja:1.4') |
| 14 |
|
| 15 |
We will query these wordnets with the following :class:`~wn.Wordnet` |
| 16 |
objects: |
| 17 |
|
| 18 |
>>> en = wn.Wordnet('oewn:2024') |
| 19 |
>>> de = wn.Wordnet('odenet:1.4') |
| 20 |
|
| 21 |
The object for the Japanese wordnet will be discussed and created |
| 22 |
below, in :ref:`cross-lingual-relation-traversal`. |
| 23 |
|
| 24 |
What are Interlingual Indices? |
| 25 |
------------------------------ |
| 26 |
|
| 27 |
It is common for users of the `Princeton WordNet |
| 28 |
<https://wordnet.princeton.edu/>`_ to refer to synsets by their `WNDB |
| 29 |
<https://wordnet.princeton.edu/documentation/wndb5wn>`_ offset and type, |
| 30 |
but this is problematic because the offset is a byte-offset in the |
| 31 |
wordnet data files and it will differ for wordnets in other languages |
| 32 |
and even between versions of the same wordnet. Interlingual indices |
| 33 |
(ILIs) address this issue by providing stable identifiers for concepts, |
| 34 |
whether for a synset across versions of a wordnet or across languages. |
| 35 |
|
| 36 |
The idea of ILIs was proposed by [Vossen99]_ and it came to fruition |
| 37 |
with the release of the Collaborative Interlingual Index (CILI; |
| 38 |
[Bond16]_). CILI therefore represents an instance of, and a namespace |
| 39 |
for, ILIs. There could, in theory, be alternative indexes for |
| 40 |
particular domains (e.g., names of people or places), but currently |
| 41 |
there is only the one. |
| 42 |
|
| 43 |
As an example, the synset for *apricot* (fruit) in WordNet 3.0 is |
| 44 |
``07750872-n``, but it is ``07766848-n`` in WordNet 3.1. In OdeNet |
| 45 |
1.4, which is not released in the WNDB format and therefore doesn't |
| 46 |
use offsets at all, it is ``13235-n`` for the equivalent word |
| 47 |
(*Aprikose*). However, all three use the same ILI: ``i77784``. |
| 48 |
|
| 49 |
Generally, only one synset within a wordnet will be mapped to a |
| 50 |
particular ILI, but this may not always be true, nor does every synset |
| 51 |
necessarily map to an ILI. Some concepts that are lexicalized in one |
| 52 |
language may not be in another language. For example, *rice* in English |
| 53 |
may refer to the rice plant, rice grain, or cooked rice, but in |
| 54 |
languages like Japanese they are distinct things (稲 *ine*, 米 *kome*, |
| 55 |
and 飯 *meshi* / ご飯 *gohan*, respectively). |
| 56 |
|
| 57 |
The ``ili`` property of Synsets serves two purposes in Wn. Mainly it is |
| 58 |
for encoding the ILI identifier associated with the synset, but it is |
| 59 |
also used to indicate when a lexicon is proposing a new concept that is |
| 60 |
not yet part of CILI. In the latter case, a WN-LMF lexicon file will |
| 61 |
have the special value of ``in`` for a synset's ILI and it will provide |
| 62 |
an ``<ILIDefinition>`` element. In Wn, this translates to |
| 63 |
:attr:`wn.Synset.ili` returning :python:`None`, the same as if no ILI |
| 64 |
were mapped at all. Both synsets with proposed ILIs and those with no |
| 65 |
ILI cannot be used in interlingual queries. Proposed ILIs can be |
| 66 |
inspected using the :mod:`wn.ili.get_proposed` function, if you know |
| 67 |
have the synset, or :mod:`wn.ili.get_all_proposed` to get all of them. |
| 68 |
|
| 69 |
|
| 70 |
.. [Vossen99] |
| 71 |
Vossen, Piek, Wim Peters, and Julio Gonzalo. |
| 72 |
"Towards a universal index of meaning." |
| 73 |
In Proceedings of ACL-99 workshop, Siglex-99, standardizing lexical resources, pp. 81-90. |
| 74 |
University of Maryland, 1999. |
| 75 |
|
| 76 |
.. [Bond16] |
| 77 |
Bond, Francis, Piek Vossen, John Philip McCrae, and Christiane Fellbaum. |
| 78 |
"CILI: the Collaborative Interlingual Index." |
| 79 |
In Proceedings of the 8th Global WordNet Conference (GWC), pp. 50-57. 2016. |
| 80 |
|
| 81 |
Using Interlingual Indices |
| 82 |
-------------------------- |
| 83 |
|
| 84 |
For synsets that have an associated ILI, you can retrieve it via the |
| 85 |
:data:`wn.Synset.ili` property: |
| 86 |
|
| 87 |
>>> apricot = en.synsets('apricot')[1] |
| 88 |
>>> apricot.ili |
| 89 |
'i77784' |
| 90 |
|
| 91 |
The value is a :class:`str` ILI identifier. These may be used directly |
| 92 |
for things like interlingual synset lookups: |
| 93 |
|
| 94 |
>>> de.synsets(ili=apricot.ili)[0].lemmas() |
| 95 |
['Marille', 'Aprikose'] |
| 96 |
|
| 97 |
There may be more information about the ILI itself which you can get |
| 98 |
from the :mod:`wn.ili` module: |
| 99 |
|
| 100 |
>>> from wn import ili |
| 101 |
>>> apricot_ili = ili.get(apricot.ili) |
| 102 |
>>> apricot_ili |
| 103 |
ILI(id='i77784') |
| 104 |
|
| 105 |
From this object you can get various properties of the ILI, such as |
| 106 |
the ID string, its status, and its definition, but if you have |
| 107 |
not added CILI to Wn's database, it will not be very informative: |
| 108 |
|
| 109 |
>>> apricot_ili.id |
| 110 |
'i77784' |
| 111 |
>>> apricot_ili.status |
| 112 |
'presupposed' |
| 113 |
>>> apricot_ili.definition() is None |
| 114 |
True |
| 115 |
|
| 116 |
The ``presupposed`` status means that the ILI ID is in use by a |
| 117 |
lexicon, but there is no other source of truth for the index. CILI can |
| 118 |
be downloaded just like a lexicon: |
| 119 |
|
| 120 |
>>> wn.download('cili:1.0') |
| 121 |
|
| 122 |
Now the status and definition should be more useful: |
| 123 |
|
| 124 |
>>> apricot_ili.status |
| 125 |
'active' |
| 126 |
>>> apricot_ili.definition() |
| 127 |
'downy yellow to rosy-colored fruit resembling a small peach' |
| 128 |
|
| 129 |
|
| 130 |
Translating Words, Senses, and Synsets |
| 131 |
-------------------------------------- |
| 132 |
|
| 133 |
Rather than manually inserting the ILI IDs into Wn's lookup functions |
| 134 |
as shown above, Wn provides the :meth:`wn.Synset.translate` method to |
| 135 |
make it easier: |
| 136 |
|
| 137 |
>>> apricot.translate(lexicon='odenet:1.4') |
| 138 |
[Synset('odenet-13235-n')] |
| 139 |
|
| 140 |
The method returns a list for two reasons: first, it's not guaranteed |
| 141 |
that the target lexicon has only one synset with the ILI and, second, |
| 142 |
you can translate to more than one lexicon at a time. |
| 143 |
|
| 144 |
:class:`~wn.Sense` objects also have a :meth:`~wn.Sense.translate` |
| 145 |
method, returning a list of senses instead of synsets: |
| 146 |
|
| 147 |
>>> de_senses = apricot.senses()[0].translate(lexicon='odenet:1.4') |
| 148 |
>>> [s.word().lemma() for s in de_senses] |
| 149 |
['Marille', 'Aprikose'] |
| 150 |
|
| 151 |
:class:`~wn.Word` have a :meth:`~wn.Word.translate` method, too, but |
| 152 |
it works a bit differently. Since each word may be part of multiple |
| 153 |
synsets, the method returns a mapping of each word sense to the list |
| 154 |
of translated words: |
| 155 |
|
| 156 |
>>> result = en.words('apricot')[0].translate(lexicon='odenet:1.4') |
| 157 |
>>> for sense, de_words in result.items(): |
| 158 |
... print(sense, [w.lemma() for w in de_words]) |
| 159 |
... |
| 160 |
Sense('oewn-apricot__1.20.00..') [] |
| 161 |
Sense('oewn-apricot__1.13.00..') ['Marille', 'Aprikose'] |
| 162 |
Sense('oewn-apricot__1.07.00..') ['lachsrosa', 'lachsfarbig', 'in Lachs', 'lachsfarben', 'lachsrot', 'lachs'] |
| 163 |
|
| 164 |
The three senses above are for *apricot* as a tree, a fruit, and a |
| 165 |
color. OdeNet does not have a synset for apricot trees, or it has one |
| 166 |
not associated with the appropriate ILI, and therefore it could not |
| 167 |
translate any words for that sense. |
| 168 |
|
| 169 |
|
| 170 |
.. _cross-lingual-relation-traversal: |
| 171 |
|
| 172 |
Cross-lingual Relation Traversal |
| 173 |
-------------------------------- |
| 174 |
|
| 175 |
ILIs have a second use in Wn, which is relation traversal for wordnets |
| 176 |
that depend on other lexicons, i.e., those created with the *expand* |
| 177 |
methodology. These wordnets, such as many of those in the `Open |
| 178 |
Multilingual Wordnet <https://github.com/omwn/>`_, do not include |
| 179 |
synset relations on their own as they were built using the English |
| 180 |
WordNet as their taxonomic scaffolding. Trying to load such a lexicon |
| 181 |
when the lexicon it requires is not added to the database presents a |
| 182 |
warning to the user: |
| 183 |
|
| 184 |
>>> ja = wn.Wordnet('omw-ja:1.4') |
| 185 |
[...] WnWarning: lexicon dependencies not available: omw-en:1.4 |
| 186 |
>>> ja.expanded_lexicons() |
| 187 |
[] |
| 188 |
|
| 189 |
.. warning:: |
| 190 |
|
| 191 |
Do not rely on the presence of a warning to determine if the |
| 192 |
lexicon has its expand lexicon loaded. Python's default warning |
| 193 |
filter may only show the warning the first time it is |
| 194 |
encountered. Instead, inspect :meth:`wn.Wordnet.expanded_lexicons` |
| 195 |
to see if it is non-empty. |
| 196 |
|
| 197 |
When a dependency is unmet, Wn only issues a warning, not an error, |
| 198 |
and you can continue to use the lexicon as it is, but it won't be |
| 199 |
useful for exploring relations such as hypernyms and hyponyms: |
| 200 |
|
| 201 |
>>> anzu = ja.synsets(ili='i77784')[0] |
| 202 |
>>> anzu.lemmas() |
| 203 |
['アンズ', 'アプリコット', '杏'] |
| 204 |
>>> anzu.hypernyms() |
| 205 |
[] |
| 206 |
|
| 207 |
One way to resolve this issue is to install the lexicon it requires: |
| 208 |
|
| 209 |
>>> wn.download('omw-en:1.4') |
| 210 |
>>> ja = wn.Wordnet('omw-ja:1.4') # no warning |
| 211 |
>>> ja.expanded_lexicons() |
| 212 |
[<Lexicon omw-en:1.4 [en]>] |
| 213 |
|
| 214 |
Wn will detect the dependency and load ``omw-en:1.4`` as the *expand* |
| 215 |
lexicon for ``omw-ja:1.4`` when the former is in the database. You may |
| 216 |
also specify an expand lexicon manually, even one that isn't the |
| 217 |
specified dependency: |
| 218 |
|
| 219 |
>>> ja = wn.Wordnet('omw-ja:1.4', expand='oewn:2024') # no warning |
| 220 |
>>> ja.expanded_lexicons() |
| 221 |
[<Lexicon oewn:2024 [en]>] |
| 222 |
|
| 223 |
In this case, the Open English WordNet is an actively-developed fork |
| 224 |
of the lexicon that ``omw-ja:1.4`` depends on, and it should contain |
| 225 |
all the relations, so you'll see little difference between using it |
| 226 |
and ``omw-en:1.4``. This works because the relations are found using |
| 227 |
ILIs and not synset offsets. You may still prefer to use the specified |
| 228 |
dependency if you have strict compatibility needs, such as for |
| 229 |
experiment reproducibility and/or compatibility with the `NLTK |
| 230 |
<https://nltk.org>`_. Using some other lexicon as the expand lexicon |
| 231 |
may yield very different results. For instance, ``odenet:1.4`` is much |
| 232 |
smaller than the English wordnets and has fewer relations, so it would |
| 233 |
not be a good substitute for ``omw-ja:1.4``'s expand lexicon. |
| 234 |
|
| 235 |
When an appropriate expand lexicon is loaded, relations between |
| 236 |
synsets, such as hypernyms, are more likely to be present: |
| 237 |
|
| 238 |
>>> anzu = ja.synsets(ili='i77784')[0] # recreate the synset object |
| 239 |
>>> anzu.hypernyms() |
| 240 |
[Synset('omw-ja-07705931-n')] |
| 241 |
>>> anzu.hypernyms()[0].lemmas() |
| 242 |
['果物'] |
| 243 |
>>> anzu.hypernyms()[0].translate(lexicon='oewn:2024')[0].lemmas() |
| 244 |
['edible fruit'] |