| 1 |
Working with Lexicons |
| 2 |
===================== |
| 3 |
|
| 4 |
Terminology |
| 5 |
----------- |
| 6 |
|
| 7 |
In Wn, the following terminology is used: |
| 8 |
|
| 9 |
:lexicon: An inventory of words, senses, synsets, relations, etc. that |
| 10 |
share a namespace (i.e., that can refer to each other). |
| 11 |
:wordnet: A group of lexicons (but usually just one). |
| 12 |
:resource: A file containing lexicons. |
| 13 |
:package: A directory containing a resource and optionally some |
| 14 |
metadata files. |
| 15 |
:collection: A directory containing packages and optionally some |
| 16 |
metadata files. |
| 17 |
:project: A general term for a resource, package, or collection, |
| 18 |
particularly pertaining to its creation, maintenance, and |
| 19 |
distribution. |
| 20 |
|
| 21 |
In general, each resource contains one lexicon. For large projects |
| 22 |
like the `Open English WordNet`_, that lexicon is also a wordnet on |
| 23 |
its own. For a collection like the `Open Multilingual Wordnet`_, most |
| 24 |
lexicons do not include relations as they are instead expected to use |
| 25 |
those from the OMW's included English wordnet, which is derived from |
| 26 |
the `Princeton WordNet`_. As such, a wordnet for these sub-projects is |
| 27 |
best thought of as the grouping of the lexicon with the lexicon |
| 28 |
providing the relations. |
| 29 |
|
| 30 |
.. _Open English WordNet: https://en-word.net |
| 31 |
.. _Open Multilingual Wordnet: https://github.com/omwn/ |
| 32 |
.. _Princeton WordNet: https://wordnet.princeton.edu/ |
| 33 |
|
| 34 |
.. _lexicon-specifiers: |
| 35 |
|
| 36 |
Lexicon and Project Specifiers |
| 37 |
------------------------------ |
| 38 |
|
| 39 |
Wn uses *lexicon specifiers* to deal with the possibility of having |
| 40 |
multiple lexicons and multiple versions of lexicons loaded in the same |
| 41 |
database. The specifiers are the joining of a lexicon's name (ID) and |
| 42 |
version, delimited by ``:``. Here are the possible forms: |
| 43 |
|
| 44 |
.. code-block:: none |
| 45 |
|
| 46 |
* -- any/all lexicons |
| 47 |
id -- the most recently added lexicon with the given id |
| 48 |
id:* -- all lexicons with the given id |
| 49 |
id:version -- the lexicon with the given id and version |
| 50 |
*:version -- all lexicons with the given version |
| 51 |
|
| 52 |
For example, if ``ewn:2020`` was installed followed by ``ewn:2019``, |
| 53 |
then ``ewn`` would specify the ``2019`` version, ``ewn:*`` would |
| 54 |
specify both versions, and ``ewn:2020`` would specify the ``2020`` |
| 55 |
version. |
| 56 |
|
| 57 |
The same format is used for *project specifiers*, which refer to |
| 58 |
projects as defined in Wn's index. In most cases the project specifier |
| 59 |
is the same as the lexicon specifier (e.g., ``ewn:2020`` refers both |
| 60 |
to the project to be downloaded and the lexicon that is installed), |
| 61 |
but sometimes it is not. The 1.4 release of the `Open Multilingual |
| 62 |
Wordnet`_, for instance, has the project specifier ``omw:1.4`` but it |
| 63 |
installs a number of lexicons with their own lexicon specifiers |
| 64 |
(``omw-zsm:1.4``, ``omw-cmn:1.4``, etc.). When only an id is given |
| 65 |
(e.g., ``ewn``), a project specifier gets the *first* version listed |
| 66 |
in the index (in the default index, conventionally, the first version |
| 67 |
is the latest release). |
| 68 |
|
| 69 |
.. _lexicon-filters: |
| 70 |
|
| 71 |
Filtering Queries with Lexicons |
| 72 |
------------------------------- |
| 73 |
|
| 74 |
Queries against the database will search all installed lexicons unless |
| 75 |
they are filtered by ``lang`` or ``lexicon`` arguments: |
| 76 |
|
| 77 |
>>> import wn |
| 78 |
>>> len(wn.words()) |
| 79 |
1538449 |
| 80 |
>>> len(wn.words(lang="en")) |
| 81 |
318289 |
| 82 |
>>> len(wn.words(lexicon="oewn:2024")) |
| 83 |
161705 |
| 84 |
|
| 85 |
The ``lexicon`` parameter can also take multiple specifiers so you can |
| 86 |
include things like lexicon extensions or to explicitly include |
| 87 |
multiple lexicons: |
| 88 |
|
| 89 |
>>> len(wn.words(lexicon="oewn:2024 omw-en:1.4")) |
| 90 |
318289 |
| 91 |
|
| 92 |
If a lexicon selected by the ``lexicon`` or ``lang`` arguments |
| 93 |
specifies a dependency, the dependency is automatically added as an |
| 94 |
*expand* lexicon. Explicitly set :python:`expand=''` to disable this |
| 95 |
behavior: |
| 96 |
|
| 97 |
>>> wn.lexicons(lexicon="omw-es:1.4")[0].requires() # omw-es requires omw-en |
| 98 |
{'omw-en:1.4': <Lexicon omw-en:1.4 [en]>} |
| 99 |
>>> es = wn.Wordnet("omw-es:1.4") |
| 100 |
>>> es.lexicons() |
| 101 |
[<Lexicon omw-es:1.4 [es]>] |
| 102 |
>>> es.expanded_lexicons() # omw-en automatically added |
| 103 |
[<Lexicon omw-en:1.4 [en]>] |
| 104 |
>>> es_no_en = wn.Wordnet("omw-es:1.4", expand='') |
| 105 |
>>> es_no_en.lexicons() |
| 106 |
[<Lexicon omw-es:1.4 [es]>] |
| 107 |
>>> es_no_en.expanded_lexicons() # no expand lexicons |
| 108 |
[] |
| 109 |
|
| 110 |
Also see :ref:`cross-lingual-relation-traversal` for |
| 111 |
selecting expand lexicons for relations. |
| 112 |
|
| 113 |
The objects returned by queries retain the "lexicon configuration" |
| 114 |
used, which includes the lexicons and expand lexicons. This |
| 115 |
configuration determines which lexicons are searched during secondary |
| 116 |
queries. The lexicon configuration also stores a flag indicating |
| 117 |
whether no lexicon filters were used at all, which triggers |
| 118 |
:ref:`default mode <default-mode>` secondary queries. |
| 119 |
|
| 120 |
.. _default-mode: |
| 121 |
|
| 122 |
Default Mode Queries |
| 123 |
-------------------- |
| 124 |
|
| 125 |
A special "default mode" is activated when making a module-function |
| 126 |
query (:func:`wn.words`, :func:`wn.synsets`, etc.) or instantiating a |
| 127 |
:class:`wn.Wordnet` object with no ``lexicon`` or ``lang`` argument |
| 128 |
(so-named because the mode is triggered by using the default values of |
| 129 |
``lexicon`` and ``lang``): |
| 130 |
|
| 131 |
>>> w = wn.Wordnet() |
| 132 |
>>> wn.words("pineapple") # for example |
| 133 |
|
| 134 |
Default-mode causes the following behavior: |
| 135 |
|
| 136 |
1. Primary queries search any installed lexicon |
| 137 |
2. Secondary queries only search the lexicon of the primary entity |
| 138 |
(e.g., :meth:`Synset.words` only finds words from the same lexicon |
| 139 |
as the synset). If the lexicon has any extensions or is itself an |
| 140 |
extension, any extension/base lexicons are also included. |
| 141 |
3. If the ``expand`` argument is :python:`None` (always true for |
| 142 |
module functions like :func:`wn.synsets`), all installed lexicons |
| 143 |
are used as expand lexicons for relations queries. |
| 144 |
|
| 145 |
.. warning:: |
| 146 |
|
| 147 |
Default-mode queries are not reproducible as the results can change |
| 148 |
as lexicons are added or removed from the database. For anything |
| 149 |
more than a casual query, it is highly suggested to instead create |
| 150 |
a :class:`wn.Wordnet` object with fully-specified ``lexicon`` and |
| 151 |
``expand`` arguments. |
| 152 |
|
| 153 |
Downloading Lexicons |
| 154 |
-------------------- |
| 155 |
|
| 156 |
Use :py:func:`wn.download` to download lexicons from the web given |
| 157 |
either an indexed project specifier or the URL of a resource, package, |
| 158 |
or collection. |
| 159 |
|
| 160 |
>>> import wn |
| 161 |
>>> wn.download('odenet') # get the latest Open German WordNet |
| 162 |
>>> wn.download('odenet:1.3') # get the 1.3 version |
| 163 |
>>> # download from a URL |
| 164 |
>>> wn.download('https://github.com/omwn/omw-data/releases/download/v1.4/omw-1.4.tar.xz') |
| 165 |
|
| 166 |
The project specifier is only used to retrieve information from Wn's |
| 167 |
index. The lexicon IDs of the corresponding resource files are what is |
| 168 |
stored in the database. |
| 169 |
|
| 170 |
Adding Local Lexicons |
| 171 |
--------------------- |
| 172 |
|
| 173 |
Lexicons can be added from local files with :py:func:`wn.add`: |
| 174 |
|
| 175 |
>>> wn.add('~/data/omw-1.4/omw-nb/omw-nb.xml') |
| 176 |
|
| 177 |
Or with the parent directory as a package: |
| 178 |
|
| 179 |
>>> wn.add('~/data/omw-1.4/omw-nb/') |
| 180 |
|
| 181 |
Or with the grandparent directory as a collection (installing all |
| 182 |
packages contained by the collection): |
| 183 |
|
| 184 |
>>> wn.add('~/data/omw-1.4/') |
| 185 |
|
| 186 |
Or from a compressed archive of one of the above: |
| 187 |
|
| 188 |
>>> wn.add('~/data/omw-1.4/omw-nb/omw-nb.xml.xz') |
| 189 |
>>> wn.add('~/data/omw-1.4/omw-nb.tar.xz') |
| 190 |
>>> wn.add('~/data/omw-1.4.tar.xz') |
| 191 |
|
| 192 |
Listing Installed Lexicons |
| 193 |
-------------------------- |
| 194 |
|
| 195 |
If you wish to see which lexicons have been added to the database, |
| 196 |
:py:func:`wn.lexicons()` returns the list of :py:class:`wn.Lexicon` |
| 197 |
objects that describe each one. |
| 198 |
|
| 199 |
>>> for lex in wn.lexicons(): |
| 200 |
... print(f'{lex.id}:{lex.version}\t{lex.label}') |
| 201 |
... |
| 202 |
omw-en:1.4 OMW English Wordnet based on WordNet 3.0 |
| 203 |
omw-nb:1.4 Norwegian Wordnet (Bokmål) |
| 204 |
odenet:1.3 Offenes Deutsches WordNet |
| 205 |
ewn:2020 English WordNet |
| 206 |
ewn:2019 English WordNet |
| 207 |
|
| 208 |
Removing Lexicons |
| 209 |
----------------- |
| 210 |
|
| 211 |
Lexicons can be removed from the database with :py:func:`wn.remove`: |
| 212 |
|
| 213 |
>>> wn.remove('omw-nb:1.4') |
| 214 |
|
| 215 |
Note that this removes a single lexicon and not a project, so if, for |
| 216 |
instance, you've installed a multi-lexicon project like ``omw``, you |
| 217 |
will need to remove each lexicon individually or use a star specifier: |
| 218 |
|
| 219 |
>>> wn.remove('omw-*:1.4') |
| 220 |
|
| 221 |
WN-LMF Files, Packages, and Collections |
| 222 |
--------------------------------------- |
| 223 |
|
| 224 |
Wn can handle projects with 3 levels of structure: |
| 225 |
|
| 226 |
* WN-LMF XML files |
| 227 |
* WN-LMF packages |
| 228 |
* WN-LMF collections |
| 229 |
|
| 230 |
WN-LMF XML Files |
| 231 |
'''''''''''''''' |
| 232 |
|
| 233 |
A WN-LMF XML file is a file with a ``.xml`` extension that is valid |
| 234 |
according to the `WN-LMF specification |
| 235 |
<https://github.com/globalwordnet/schemas/>`_. |
| 236 |
|
| 237 |
WN-LMF Packages |
| 238 |
''''''''''''''' |
| 239 |
|
| 240 |
If one needs to distribute metadata or additional files along with |
| 241 |
WN-LMF XML file, a WN-LMF package allows them to include the files in |
| 242 |
a directory. The directory should contain exactly one ``.xml`` file, |
| 243 |
which is the WN-LMF XML file. In addition, it may contain additional |
| 244 |
files and Wn will recognize three of them: |
| 245 |
|
| 246 |
:``LICENSE`` (``.txt`` | ``.md`` | ``.rst`` ): the full text of the license |
| 247 |
:``README`` (``.txt`` | ``.md`` | ``.rst`` ): the project README |
| 248 |
:``citation.bib``: a BibTeX file containing academic citations for the project |
| 249 |
|
| 250 |
|
| 251 |
.. code-block:: |
| 252 |
|
| 253 |
omw-sq/ |
| 254 |
├── omw-sq.xml |
| 255 |
├── LICENSE.txt |
| 256 |
└── README.md |
| 257 |
|
| 258 |
WN-LMF Collections |
| 259 |
'''''''''''''''''' |
| 260 |
|
| 261 |
In some cases a project may manage multiple resources and distribute |
| 262 |
them as a collection. A collection is a directory containing |
| 263 |
subdirectories which are WN-LMF packages. The collection may contain |
| 264 |
its own README, LICENSE, and citation files which describe the project |
| 265 |
as a whole. |
| 266 |
|
| 267 |
.. code-block:: |
| 268 |
|
| 269 |
omw-1.4/ |
| 270 |
├── omw-sq |
| 271 |
│ ├── oms-sq.xml |
| 272 |
│ ├── LICENSE.txt |
| 273 |
│ └── README.md |
| 274 |
├── omw-lt |
| 275 |
│ ├── citation.bib |
| 276 |
│ ├── LICENSE |
| 277 |
│ └── omw-lt.xml |
| 278 |
├── ... |
| 279 |
├── citation.bib |
| 280 |
├── LICENSE |
| 281 |
└── README.md |