| 1 |
Basic Usage |
| 2 |
=========== |
| 3 |
|
| 4 |
.. seealso:: |
| 5 |
|
| 6 |
This document covers the basics of querying wordnets, filtering |
| 7 |
results, and performing secondary queries on the results. For |
| 8 |
adding, removing, or inspecting lexicons, see :doc:`lexicons`. For |
| 9 |
more information about interlingual queries, see |
| 10 |
:doc:`interlingual`. |
| 11 |
|
| 12 |
For the most basic queries, Wn provides several module functions for |
| 13 |
retrieving words, senses, and synsets: |
| 14 |
|
| 15 |
>>> import wn |
| 16 |
>>> wn.words('pike') |
| 17 |
[Word('ewn-pike-n')] |
| 18 |
>>> wn.senses('pike') |
| 19 |
[Sense('ewn-pike-n-03311555-04'), Sense('ewn-pike-n-07795351-01'), Sense('ewn-pike-n-03941974-01'), Sense('ewn-pike-n-03941726-01'), Sense('ewn-pike-n-02563739-01')] |
| 20 |
>>> wn.synsets('pike') |
| 21 |
[Synset('ewn-03311555-n'), Synset('ewn-07795351-n'), Synset('ewn-03941974-n'), Synset('ewn-03941726-n'), Synset('ewn-02563739-n')] |
| 22 |
|
| 23 |
Once you start working with multiple wordnets, these simple queries |
| 24 |
may return more than desired: |
| 25 |
|
| 26 |
>>> wn.words('pike') |
| 27 |
[Word('ewn-pike-n'), Word('wnja-n-66614')] |
| 28 |
>>> wn.words('chat') |
| 29 |
[Word('ewn-chat-n'), Word('ewn-chat-v'), Word('frawn-lex14803'), Word('frawn-lex21897')] |
| 30 |
|
| 31 |
You can specify which language or lexicon you wish to query: |
| 32 |
|
| 33 |
>>> wn.words('pike', lang='ja') |
| 34 |
[Word('wnja-n-66614')] |
| 35 |
>>> wn.words('chat', lexicon='frawn') |
| 36 |
[Word('frawn-lex14803'), Word('frawn-lex21897')] |
| 37 |
|
| 38 |
But it might be easier to create a :class:`~wn.Wordnet` object and use |
| 39 |
it for queries: |
| 40 |
|
| 41 |
>>> wnja = wn.Wordnet(lang='ja') |
| 42 |
>>> wnja.words('pike') |
| 43 |
[Word('wnja-n-66614')] |
| 44 |
>>> frawn = wn.Wordnet(lexicon='frawn') |
| 45 |
>>> frawn.words('chat') |
| 46 |
[Word('frawn-lex14803'), Word('frawn-lex21897')] |
| 47 |
|
| 48 |
In fact, the simple queries above implicitly create such a |
| 49 |
:class:`~wn.Wordnet` object, but one that includes all installed |
| 50 |
lexicons. |
| 51 |
|
| 52 |
|
| 53 |
.. _primary-queries: |
| 54 |
|
| 55 |
Primary Queries |
| 56 |
--------------- |
| 57 |
|
| 58 |
The queries shown above are "primary" queries, meaning they are the |
| 59 |
first step in a user's interaction with a wordnet. Operations |
| 60 |
performed on the resulting objects are then `secondary |
| 61 |
queries`_. Primary queries optionally take several fields for |
| 62 |
filtering the results, namely the word form and part of |
| 63 |
speech. Synsets may also be filtered by an interlingual index (ILI). |
| 64 |
|
| 65 |
Searching for Words |
| 66 |
''''''''''''''''''' |
| 67 |
|
| 68 |
The :func:`wn.words()` function returns a list of :class:`~wn.Word` |
| 69 |
objects that match the given word form or part of speech: |
| 70 |
|
| 71 |
>>> wn.words('pencil') |
| 72 |
[Word('ewn-pencil-n'), Word('ewn-pencil-v')] |
| 73 |
>>> wn.words('pencil', pos='v') |
| 74 |
[Word('ewn-pencil-v')] |
| 75 |
|
| 76 |
Calling the function without a word form will return all words in the |
| 77 |
database: |
| 78 |
|
| 79 |
>>> len(wn.words()) |
| 80 |
311711 |
| 81 |
>>> len(wn.words(pos='v')) |
| 82 |
29419 |
| 83 |
>>> len(wn.words(pos='v', lexicon='ewn')) |
| 84 |
11595 |
| 85 |
|
| 86 |
If you know the word identifier used by a lexicon, you can retrieve |
| 87 |
the word directly with the :func:`wn.word()` function. Identifiers are |
| 88 |
guaranteed to be unique within a single lexicon, but not across |
| 89 |
lexicons, so it's best to call this function from an instantiated |
| 90 |
:class:`~wn.Wordnet` object or with the ``lexicon`` parameter |
| 91 |
specified. If multiple words are found when querying multiple |
| 92 |
lexicons, only the first is returned. |
| 93 |
|
| 94 |
>>> wn.word('ewn-pencil-n', lexicon='ewn') |
| 95 |
Word('ewn-pencil-n') |
| 96 |
|
| 97 |
|
| 98 |
Searching for Senses |
| 99 |
'''''''''''''''''''' |
| 100 |
|
| 101 |
The :func:`wn.senses()` and :func:`wn.sense()` functions behave |
| 102 |
similarly to :func:`wn.words()` and :func:`wn.word()`, except that |
| 103 |
they return matching :class:`~wn.Sense` objects. |
| 104 |
|
| 105 |
>>> wn.senses('plow', pos='n') |
| 106 |
[Sense('ewn-plow-n-03973894-01')] |
| 107 |
>>> wn.sense('ewn-plow-v-01745745-01') |
| 108 |
Sense('ewn-plow-v-01745745-01') |
| 109 |
|
| 110 |
Senses represent a relationship between a :class:`~wn.Word` and a |
| 111 |
:class:`~wn.Synset`. Seen as an edge between nodes, senses are often |
| 112 |
given less prominence than words or synsets, but they are the natural |
| 113 |
locus of several interesting features such as sense relations (e.g., |
| 114 |
for derived words) and the natural level of representation for |
| 115 |
translations to other languages. |
| 116 |
|
| 117 |
Searching for Synsets |
| 118 |
''''''''''''''''''''' |
| 119 |
|
| 120 |
The :func:`wn.synsets()` and :func:`wn.synset()` functions are like |
| 121 |
those above but allow the ``ili`` parameter for filtering by |
| 122 |
interlingual index, which is useful in interlingual queries: |
| 123 |
|
| 124 |
>>> wn.synsets('scepter') |
| 125 |
[Synset('ewn-14467142-n'), Synset('ewn-07282278-n')] |
| 126 |
>>> wn.synset('ewn-07282278-n').ili |
| 127 |
'i74874' |
| 128 |
>>> wn.synsets(ili='i74874') |
| 129 |
[Synset('ewn-07282278-n'), Synset('wnja-07267573-n'), Synset('frawn-07267573-n')] |
| 130 |
|
| 131 |
|
| 132 |
Secondary Queries |
| 133 |
----------------- |
| 134 |
|
| 135 |
Once you have gotten some results from a primary query, you can |
| 136 |
perform operations on the :class:`~wn.Word`, :class:`~wn.Sense`, or |
| 137 |
:class:`~wn.Synset` objects to get at further information in the |
| 138 |
wordnet. |
| 139 |
|
| 140 |
Exploring Words |
| 141 |
''''''''''''''' |
| 142 |
|
| 143 |
Here are some of the things you can do with :class:`~wn.Word` objects: |
| 144 |
|
| 145 |
>>> w = wn.words('goose')[0] |
| 146 |
>>> w.pos # part of speech |
| 147 |
'n' |
| 148 |
>>> w.forms() # other word forms (e.g., irregular inflections) |
| 149 |
['goose', 'geese'] |
| 150 |
>>> w.lemma() # canonical form |
| 151 |
'goose' |
| 152 |
>>> w.derived_words() |
| 153 |
[Word('ewn-gosling-n'), Word('ewn-goosy-s'), Word('ewn-goosey-s')] |
| 154 |
>>> w.senses() |
| 155 |
[Sense('ewn-goose-n-01858313-01'), Sense('ewn-goose-n-10177319-06'), Sense('ewn-goose-n-07662430-01')] |
| 156 |
>>> w.synsets() |
| 157 |
[Synset('ewn-01858313-n'), Synset('ewn-10177319-n'), Synset('ewn-07662430-n')] |
| 158 |
|
| 159 |
Since translations of a word into another language depend on the sense |
| 160 |
used, :meth:`Word.translate <wn.Word.translate>` returns a dictionary |
| 161 |
mapping each sense to words in the target language: |
| 162 |
|
| 163 |
>>> for sense, ja_words in w.translate(lang='ja').items(): |
| 164 |
... print(sense, ja_words) |
| 165 |
... |
| 166 |
Sense('ewn-goose-n-01858313-01') [Word('wnja-n-1254'), Word('wnja-n-33090'), Word('wnja-n-38995')] |
| 167 |
Sense('ewn-goose-n-10177319-06') [] |
| 168 |
Sense('ewn-goose-n-07662430-01') [Word('wnja-n-1254')] |
| 169 |
|
| 170 |
|
| 171 |
Exploring Senses |
| 172 |
'''''''''''''''' |
| 173 |
|
| 174 |
Compared to :class:`~wn.Word` and :class:`~wn.Synset` objects, there |
| 175 |
are relatively few operations available on :class:`~wn.Sense` |
| 176 |
objects. Sense relations and translations, however, are important |
| 177 |
operations on senses. |
| 178 |
|
| 179 |
>>> s = wn.senses('dark', pos='n')[0] |
| 180 |
>>> s.word() # each sense links to a single word |
| 181 |
Word('ewn-dark-n') |
| 182 |
>>> s.synset() # each sense links to a single synset |
| 183 |
Synset('ewn-14007000-n') |
| 184 |
>>> s.get_related('antonym') |
| 185 |
[Sense('ewn-light-n-14006789-01')] |
| 186 |
>>> s.get_related('derivation') |
| 187 |
[Sense('ewn-dark-a-00273948-01')] |
| 188 |
>>> s.translate(lang='fr') # translation returns a list of senses |
| 189 |
[Sense('frawn-lex52992--13983515-n')] |
| 190 |
>>> s.translate(lang='fr')[0].word().lemma() |
| 191 |
'obscurité' |
| 192 |
|
| 193 |
|
| 194 |
Exploring Synsets |
| 195 |
''''''''''''''''' |
| 196 |
|
| 197 |
Many of the operations people care about happen on synsets, such as |
| 198 |
hierarchical relations and metrics. |
| 199 |
|
| 200 |
>>> ss = wn.synsets('hound', pos='n')[0] |
| 201 |
>>> ss.senses() |
| 202 |
[Sense('ewn-hound-n-02090203-01'), Sense('ewn-hound_dog-n-02090203-02')] |
| 203 |
>>> ss.words() |
| 204 |
[Word('ewn-hound-n'), Word('ewn-hound_dog-n')] |
| 205 |
>>> ss.lemmas() |
| 206 |
['hound', 'hound dog'] |
| 207 |
>>> ss.definition() |
| 208 |
'any of several breeds of dog used for hunting typically having large drooping ears' |
| 209 |
>>> ss.hypernyms() |
| 210 |
[Synset('ewn-02089774-n')] |
| 211 |
>>> ss.hypernyms()[0].lemmas() |
| 212 |
['hunting dog'] |
| 213 |
>>> len(ss.hyponyms()) |
| 214 |
20 |
| 215 |
>>> ss.hyponyms()[0].lemmas() |
| 216 |
['Afghan', 'Afghan hound'] |
| 217 |
>>> ss.max_depth() |
| 218 |
15 |
| 219 |
>>> ss.shortest_path(wn.synsets('dog', pos='n')[0]) |
| 220 |
[Synset('ewn-02090203-n'), Synset('ewn-02089774-n'), Synset('ewn-02086723-n')] |
| 221 |
>>> ss.translate(lang='fr') # translation returns a list of synsets |
| 222 |
[Synset('frawn-02087551-n')] |
| 223 |
>>> ss.translate(lang='fr')[0].lemmas() |
| 224 |
['chien', 'chien de chasse'] |
| 225 |
|
| 226 |
|
| 227 |
Filtering by Language |
| 228 |
--------------------- |
| 229 |
|
| 230 |
The ``lang`` parameter of :func:`wn.words()`, :func:`wn.senses()`, |
| 231 |
:func:`wn.synsets()`, and :class:`~wn.Wordnet` allows a single `BCP 47 |
| 232 |
<https://en.wikipedia.org/wiki/IETF_language_tag>`_ language |
| 233 |
code. When this parameter is used, only entries in the specified |
| 234 |
language will be returned. |
| 235 |
|
| 236 |
>>> import wn |
| 237 |
>>> wn.words('chat') |
| 238 |
[Word('ewn-chat-n'), Word('ewn-chat-v'), Word('frawn-lex14803'), Word('frawn-lex21897')] |
| 239 |
>>> wn.words('chat', lang='fr') |
| 240 |
[Word('frawn-lex14803'), Word('frawn-lex21897')] |
| 241 |
|
| 242 |
If a language code not used by any lexicon is specified, a |
| 243 |
:exc:`wn.Error` is raised. |
| 244 |
|
| 245 |
|
| 246 |
Filtering by Lexicon |
| 247 |
-------------------- |
| 248 |
|
| 249 |
The ``lexicon`` parameter of :func:`wn.words()`, :func:`wn.senses()`, |
| 250 |
:func:`wn.synsets()`, and :class:`~wn.Wordnet` take a string of |
| 251 |
space-delimited :ref:`lexicon specifiers |
| 252 |
<lexicon-specifiers>`. Entries in a lexicon whose ID matches one of |
| 253 |
the lexicon specifiers will be returned. For these, the following |
| 254 |
rules are used: |
| 255 |
|
| 256 |
- A full ``id:version`` string (e.g., ``ewn:2020``) selects a specific |
| 257 |
lexicon |
| 258 |
- Only a lexicon ``id`` (e.g., ``ewn``) selects the most recently |
| 259 |
added lexicon with that ID |
| 260 |
- A star ``*`` may be used to match any lexicon; a star may not |
| 261 |
include a version |
| 262 |
|
| 263 |
>>> wn.words('chat', lexicon='ewn:2020') |
| 264 |
[Word('ewn-chat-n'), Word('ewn-chat-v')] |
| 265 |
>>> wn.words('chat', lexicon='wnja') |
| 266 |
[] |
| 267 |
>>> wn.words('chat', lexicon='wnja frawn') |
| 268 |
[Word('frawn-lex14803'), Word('frawn-lex21897')] |