| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"\n", |
| 8 |
"\n", |
| 9 |
"# Wn Demonstration\n", |
| 10 |
"\n", |
| 11 |
"This is a demonstration of the [Wn](https://github.com/goodmami/wn/) library for working with wordnets in Python. To run this notebook locally, you will need to install the `wn` and `jupyter` packages, and download some wordnet data:\n", |
| 12 |
"\n", |
| 13 |
"* Linux/macOS\n", |
| 14 |
"\n", |
| 15 |
" ```console\n", |
| 16 |
" $ python3 -m pip install wn jupyter\n", |
| 17 |
" $ python3 -m wn download omw oewn:2021\n", |
| 18 |
" ```\n", |
| 19 |
" \n", |
| 20 |
"* Windows\n", |
| 21 |
"\n", |
| 22 |
" ```console\n", |
| 23 |
" > py -3 -m pip install wn jupyter\n", |
| 24 |
" > py -3 -m wn download omw oewn:2021\n", |
| 25 |
" ```\n", |
| 26 |
"\n", |
| 27 |
"Now you should be able to import the `wn` package:" |
| 28 |
] |
| 29 |
}, |
| 30 |
{ |
| 31 |
"cell_type": "code", |
| 32 |
"execution_count": 1, |
| 33 |
"metadata": {}, |
| 34 |
"outputs": [], |
| 35 |
"source": [ |
| 36 |
"import wn" |
| 37 |
] |
| 38 |
}, |
| 39 |
{ |
| 40 |
"cell_type": "markdown", |
| 41 |
"metadata": {}, |
| 42 |
"source": [ |
| 43 |
"## Primary Queries\n", |
| 44 |
"\n", |
| 45 |
"A **primary query** of the database is when basic parameters such as word forms, parts of speech, or public identifiers (e.g., synset IDs) are used to retrieve basic wordnet entities. You can perform these searches via module-level functions such as [wn.words()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.words), [wn.senses()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.senses), and [wn.synsets()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.synsets):" |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
"cell_type": "code", |
| 50 |
"execution_count": 2, |
| 51 |
"metadata": {}, |
| 52 |
"outputs": [ |
| 53 |
{ |
| 54 |
"data": { |
| 55 |
"text/plain": [ |
| 56 |
"[Word('oewn-Malacca-n')]" |
| 57 |
] |
| 58 |
}, |
| 59 |
"execution_count": 2, |
| 60 |
"metadata": {}, |
| 61 |
"output_type": "execute_result" |
| 62 |
} |
| 63 |
], |
| 64 |
"source": [ |
| 65 |
"wn.words(\"Malacca\")" |
| 66 |
] |
| 67 |
}, |
| 68 |
{ |
| 69 |
"cell_type": "code", |
| 70 |
"execution_count": 3, |
| 71 |
"metadata": {}, |
| 72 |
"outputs": [ |
| 73 |
{ |
| 74 |
"data": { |
| 75 |
"text/plain": [ |
| 76 |
"[Synset('oewn-08985168-n')]" |
| 77 |
] |
| 78 |
}, |
| 79 |
"execution_count": 3, |
| 80 |
"metadata": {}, |
| 81 |
"output_type": "execute_result" |
| 82 |
} |
| 83 |
], |
| 84 |
"source": [ |
| 85 |
"wn.synsets(\"Malacca\")" |
| 86 |
] |
| 87 |
}, |
| 88 |
{ |
| 89 |
"cell_type": "markdown", |
| 90 |
"metadata": {}, |
| 91 |
"source": [ |
| 92 |
"### Filtering by Language / Lexicon\n", |
| 93 |
"\n", |
| 94 |
"Once you've added multiple wordnets, however, you will often get many results for such queries. If that's not clear, then the following will give you some idea(s):" |
| 95 |
] |
| 96 |
}, |
| 97 |
{ |
| 98 |
"cell_type": "code", |
| 99 |
"execution_count": 4, |
| 100 |
"metadata": {}, |
| 101 |
"outputs": [ |
| 102 |
{ |
| 103 |
"data": { |
| 104 |
"text/plain": [ |
| 105 |
"[Word('omw-en-idea-n'),\n", |
| 106 |
" Word('omw-sk-idea-n'),\n", |
| 107 |
" Word('omw-pl-idea-n'),\n", |
| 108 |
" Word('omw-is-ídea-n'),\n", |
| 109 |
" Word('omw-zsm-idea-n'),\n", |
| 110 |
" Word('omw-iwn-idea-n'),\n", |
| 111 |
" Word('omw-it-idea-n'),\n", |
| 112 |
" Word('omw-gl-idea-n'),\n", |
| 113 |
" Word('omw-fi-idea-n'),\n", |
| 114 |
" Word('omw-ca-idea-n'),\n", |
| 115 |
" Word('omw-eu-idea-n'),\n", |
| 116 |
" Word('omw-es-idea-n'),\n", |
| 117 |
" Word('oewn-idea-n')]" |
| 118 |
] |
| 119 |
}, |
| 120 |
"execution_count": 4, |
| 121 |
"metadata": {}, |
| 122 |
"output_type": "execute_result" |
| 123 |
} |
| 124 |
], |
| 125 |
"source": [ |
| 126 |
"wn.words(\"idea\")" |
| 127 |
] |
| 128 |
}, |
| 129 |
{ |
| 130 |
"cell_type": "markdown", |
| 131 |
"metadata": {}, |
| 132 |
"source": [ |
| 133 |
"You can filter down the results by language, but that may not be enough if you have multiple wordnets for the same language (e.g., the [OMW English Wordnet based on WordNet 3.0](https://github.com/omwn/omw-data/) and the [Open English WordNet](https://en-word.net/)):" |
| 134 |
] |
| 135 |
}, |
| 136 |
{ |
| 137 |
"cell_type": "code", |
| 138 |
"execution_count": 5, |
| 139 |
"metadata": {}, |
| 140 |
"outputs": [ |
| 141 |
{ |
| 142 |
"data": { |
| 143 |
"text/plain": [ |
| 144 |
"[Word('omw-en-idea-n'), Word('oewn-idea-n')]" |
| 145 |
] |
| 146 |
}, |
| 147 |
"execution_count": 5, |
| 148 |
"metadata": {}, |
| 149 |
"output_type": "execute_result" |
| 150 |
} |
| 151 |
], |
| 152 |
"source": [ |
| 153 |
"wn.words(\"idea\", lang=\"en\")" |
| 154 |
] |
| 155 |
}, |
| 156 |
{ |
| 157 |
"cell_type": "markdown", |
| 158 |
"metadata": {}, |
| 159 |
"source": [ |
| 160 |
"The [wn.lexicons()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.lexicons) function can show which lexicons have been added for a language:" |
| 161 |
] |
| 162 |
}, |
| 163 |
{ |
| 164 |
"cell_type": "code", |
| 165 |
"execution_count": 6, |
| 166 |
"metadata": {}, |
| 167 |
"outputs": [ |
| 168 |
{ |
| 169 |
"data": { |
| 170 |
"text/plain": [ |
| 171 |
"[<Lexicon omw-en:1.4 [en]>, <Lexicon oewn:2021 [en]>]" |
| 172 |
] |
| 173 |
}, |
| 174 |
"execution_count": 6, |
| 175 |
"metadata": {}, |
| 176 |
"output_type": "execute_result" |
| 177 |
} |
| 178 |
], |
| 179 |
"source": [ |
| 180 |
"wn.lexicons(lang=\"en\")" |
| 181 |
] |
| 182 |
}, |
| 183 |
{ |
| 184 |
"cell_type": "markdown", |
| 185 |
"metadata": {}, |
| 186 |
"source": [ |
| 187 |
"You can use the `id:version` string to restrict queries to a particular lexicon:" |
| 188 |
] |
| 189 |
}, |
| 190 |
{ |
| 191 |
"cell_type": "code", |
| 192 |
"execution_count": 7, |
| 193 |
"metadata": {}, |
| 194 |
"outputs": [ |
| 195 |
{ |
| 196 |
"data": { |
| 197 |
"text/plain": [ |
| 198 |
"[Word('omw-en-idea-n')]" |
| 199 |
] |
| 200 |
}, |
| 201 |
"execution_count": 7, |
| 202 |
"metadata": {}, |
| 203 |
"output_type": "execute_result" |
| 204 |
} |
| 205 |
], |
| 206 |
"source": [ |
| 207 |
"wn.words(\"idea\", lexicon=\"omw-en:1.4\")" |
| 208 |
] |
| 209 |
}, |
| 210 |
{ |
| 211 |
"cell_type": "markdown", |
| 212 |
"metadata": {}, |
| 213 |
"source": [ |
| 214 |
"But it can become tedious to enter these specifiers each time. Instead, a [wn.Wordnet](https://wn.readthedocs.io/en/latest/api/wn.html#the-wordnet-class) object can be used to make the language/lexicon filters persistent:" |
| 215 |
] |
| 216 |
}, |
| 217 |
{ |
| 218 |
"cell_type": "code", |
| 219 |
"execution_count": 8, |
| 220 |
"metadata": {}, |
| 221 |
"outputs": [ |
| 222 |
{ |
| 223 |
"data": { |
| 224 |
"text/plain": [ |
| 225 |
"[Word('omw-en-idea-n')]" |
| 226 |
] |
| 227 |
}, |
| 228 |
"execution_count": 8, |
| 229 |
"metadata": {}, |
| 230 |
"output_type": "execute_result" |
| 231 |
} |
| 232 |
], |
| 233 |
"source": [ |
| 234 |
"en = wn.Wordnet(lexicon=\"omw-en:1.4\")\n", |
| 235 |
"en.words(\"idea\")" |
| 236 |
] |
| 237 |
}, |
| 238 |
{ |
| 239 |
"cell_type": "markdown", |
| 240 |
"metadata": {}, |
| 241 |
"source": [ |
| 242 |
"### Filtering by Word Form and Part of Speech\n", |
| 243 |
"\n", |
| 244 |
"Even within a single lexicon a word may return multiple results:" |
| 245 |
] |
| 246 |
}, |
| 247 |
{ |
| 248 |
"cell_type": "code", |
| 249 |
"execution_count": 9, |
| 250 |
"metadata": {}, |
| 251 |
"outputs": [ |
| 252 |
{ |
| 253 |
"data": { |
| 254 |
"text/plain": [ |
| 255 |
"[Word('omw-en-pencil-n'), Word('omw-en-pencil-v')]" |
| 256 |
] |
| 257 |
}, |
| 258 |
"execution_count": 9, |
| 259 |
"metadata": {}, |
| 260 |
"output_type": "execute_result" |
| 261 |
} |
| 262 |
], |
| 263 |
"source": [ |
| 264 |
"en.words(\"pencil\")" |
| 265 |
] |
| 266 |
}, |
| 267 |
{ |
| 268 |
"cell_type": "markdown", |
| 269 |
"metadata": {}, |
| 270 |
"source": [ |
| 271 |
"You can restrict results by part of speech, as well. E.g., to get the verbal sense of *pencil* (e.g., *to pencil in an appointment*), use the `pos` filter:" |
| 272 |
] |
| 273 |
}, |
| 274 |
{ |
| 275 |
"cell_type": "code", |
| 276 |
"execution_count": 10, |
| 277 |
"metadata": {}, |
| 278 |
"outputs": [ |
| 279 |
{ |
| 280 |
"data": { |
| 281 |
"text/plain": [ |
| 282 |
"[Word('omw-en-pencil-v')]" |
| 283 |
] |
| 284 |
}, |
| 285 |
"execution_count": 10, |
| 286 |
"metadata": {}, |
| 287 |
"output_type": "execute_result" |
| 288 |
} |
| 289 |
], |
| 290 |
"source": [ |
| 291 |
"en.words(\"pencil\", pos=\"v\")" |
| 292 |
] |
| 293 |
}, |
| 294 |
{ |
| 295 |
"cell_type": "markdown", |
| 296 |
"metadata": {}, |
| 297 |
"source": [ |
| 298 |
"This works for getting senses and synsets, too:" |
| 299 |
] |
| 300 |
}, |
| 301 |
{ |
| 302 |
"cell_type": "code", |
| 303 |
"execution_count": 11, |
| 304 |
"metadata": {}, |
| 305 |
"outputs": [ |
| 306 |
{ |
| 307 |
"data": { |
| 308 |
"text/plain": [ |
| 309 |
"[Sense('omw-en-pencil-03908204-n'),\n", |
| 310 |
" Sense('omw-en-pencil-14796748-n'),\n", |
| 311 |
" Sense('omw-en-pencil-13863020-n'),\n", |
| 312 |
" Sense('omw-en-pencil-03908456-n'),\n", |
| 313 |
" Sense('omw-en-pencil-01688604-v')]" |
| 314 |
] |
| 315 |
}, |
| 316 |
"execution_count": 11, |
| 317 |
"metadata": {}, |
| 318 |
"output_type": "execute_result" |
| 319 |
} |
| 320 |
], |
| 321 |
"source": [ |
| 322 |
"en.senses(\"pencil\")" |
| 323 |
] |
| 324 |
}, |
| 325 |
{ |
| 326 |
"cell_type": "code", |
| 327 |
"execution_count": 12, |
| 328 |
"metadata": {}, |
| 329 |
"outputs": [ |
| 330 |
{ |
| 331 |
"data": { |
| 332 |
"text/plain": [ |
| 333 |
"[Sense('omw-en-pencil-01688604-v')]" |
| 334 |
] |
| 335 |
}, |
| 336 |
"execution_count": 12, |
| 337 |
"metadata": {}, |
| 338 |
"output_type": "execute_result" |
| 339 |
} |
| 340 |
], |
| 341 |
"source": [ |
| 342 |
"en.senses(\"pencil\", pos=\"v\")" |
| 343 |
] |
| 344 |
}, |
| 345 |
{ |
| 346 |
"cell_type": "code", |
| 347 |
"execution_count": 13, |
| 348 |
"metadata": {}, |
| 349 |
"outputs": [ |
| 350 |
{ |
| 351 |
"data": { |
| 352 |
"text/plain": [ |
| 353 |
"[Synset('omw-en-01688604-v')]" |
| 354 |
] |
| 355 |
}, |
| 356 |
"execution_count": 13, |
| 357 |
"metadata": {}, |
| 358 |
"output_type": "execute_result" |
| 359 |
} |
| 360 |
], |
| 361 |
"source": [ |
| 362 |
"en.synsets(\"pencil\", pos=\"v\")" |
| 363 |
] |
| 364 |
}, |
| 365 |
{ |
| 366 |
"cell_type": "markdown", |
| 367 |
"metadata": {}, |
| 368 |
"source": [ |
| 369 |
"The wordform itself is just a filter on the results. Leaving it off, you can get all results for a particular part of speech:" |
| 370 |
] |
| 371 |
}, |
| 372 |
{ |
| 373 |
"cell_type": "code", |
| 374 |
"execution_count": 14, |
| 375 |
"metadata": {}, |
| 376 |
"outputs": [ |
| 377 |
{ |
| 378 |
"data": { |
| 379 |
"text/plain": [ |
| 380 |
"11531" |
| 381 |
] |
| 382 |
}, |
| 383 |
"execution_count": 14, |
| 384 |
"metadata": {}, |
| 385 |
"output_type": "execute_result" |
| 386 |
} |
| 387 |
], |
| 388 |
"source": [ |
| 389 |
"len(en.words(pos=\"v\"))" |
| 390 |
] |
| 391 |
}, |
| 392 |
{ |
| 393 |
"cell_type": "markdown", |
| 394 |
"metadata": {}, |
| 395 |
"source": [ |
| 396 |
"Or all results, regardless of the part of speech:" |
| 397 |
] |
| 398 |
}, |
| 399 |
{ |
| 400 |
"cell_type": "code", |
| 401 |
"execution_count": 15, |
| 402 |
"metadata": {}, |
| 403 |
"outputs": [ |
| 404 |
{ |
| 405 |
"data": { |
| 406 |
"text/plain": [ |
| 407 |
"156584" |
| 408 |
] |
| 409 |
}, |
| 410 |
"execution_count": 15, |
| 411 |
"metadata": {}, |
| 412 |
"output_type": "execute_result" |
| 413 |
} |
| 414 |
], |
| 415 |
"source": [ |
| 416 |
"len(en.words())" |
| 417 |
] |
| 418 |
}, |
| 419 |
{ |
| 420 |
"cell_type": "markdown", |
| 421 |
"metadata": {}, |
| 422 |
"source": [ |
| 423 |
"## Secondary Queries\n", |
| 424 |
"\n", |
| 425 |
"**Secondary queries** are used when you want to get additional information from a retrieved entity, such as the forms of a word or the definition of a synset. They are also used for finding links between entities, such as the senses of a word or the relations of a sense or synset." |
| 426 |
] |
| 427 |
}, |
| 428 |
{ |
| 429 |
"cell_type": "code", |
| 430 |
"execution_count": 16, |
| 431 |
"metadata": {}, |
| 432 |
"outputs": [ |
| 433 |
{ |
| 434 |
"data": { |
| 435 |
"text/plain": [ |
| 436 |
"'pencil'" |
| 437 |
] |
| 438 |
}, |
| 439 |
"execution_count": 16, |
| 440 |
"metadata": {}, |
| 441 |
"output_type": "execute_result" |
| 442 |
} |
| 443 |
], |
| 444 |
"source": [ |
| 445 |
"pencil = en.words(\"pencil\", pos=\"v\")[0]\n", |
| 446 |
"pencil.lemma()" |
| 447 |
] |
| 448 |
}, |
| 449 |
{ |
| 450 |
"cell_type": "code", |
| 451 |
"execution_count": 17, |
| 452 |
"metadata": {}, |
| 453 |
"outputs": [ |
| 454 |
{ |
| 455 |
"data": { |
| 456 |
"text/plain": [ |
| 457 |
"['pencil', 'pencilled', 'pencilling']" |
| 458 |
] |
| 459 |
}, |
| 460 |
"execution_count": 17, |
| 461 |
"metadata": {}, |
| 462 |
"output_type": "execute_result" |
| 463 |
} |
| 464 |
], |
| 465 |
"source": [ |
| 466 |
"pencil.forms()" |
| 467 |
] |
| 468 |
}, |
| 469 |
{ |
| 470 |
"cell_type": "code", |
| 471 |
"execution_count": 18, |
| 472 |
"metadata": {}, |
| 473 |
"outputs": [ |
| 474 |
{ |
| 475 |
"data": { |
| 476 |
"text/plain": [ |
| 477 |
"'v'" |
| 478 |
] |
| 479 |
}, |
| 480 |
"execution_count": 18, |
| 481 |
"metadata": {}, |
| 482 |
"output_type": "execute_result" |
| 483 |
} |
| 484 |
], |
| 485 |
"source": [ |
| 486 |
"pencil.pos" |
| 487 |
] |
| 488 |
}, |
| 489 |
{ |
| 490 |
"cell_type": "code", |
| 491 |
"execution_count": 19, |
| 492 |
"metadata": {}, |
| 493 |
"outputs": [ |
| 494 |
{ |
| 495 |
"data": { |
| 496 |
"text/plain": [ |
| 497 |
"[Sense('omw-en-pencil-01688604-v')]" |
| 498 |
] |
| 499 |
}, |
| 500 |
"execution_count": 19, |
| 501 |
"metadata": {}, |
| 502 |
"output_type": "execute_result" |
| 503 |
} |
| 504 |
], |
| 505 |
"source": [ |
| 506 |
"pencil.senses()" |
| 507 |
] |
| 508 |
}, |
| 509 |
{ |
| 510 |
"cell_type": "code", |
| 511 |
"execution_count": 20, |
| 512 |
"metadata": {}, |
| 513 |
"outputs": [ |
| 514 |
{ |
| 515 |
"data": { |
| 516 |
"text/plain": [ |
| 517 |
"Synset('omw-en-01688604-v')" |
| 518 |
] |
| 519 |
}, |
| 520 |
"execution_count": 20, |
| 521 |
"metadata": {}, |
| 522 |
"output_type": "execute_result" |
| 523 |
} |
| 524 |
], |
| 525 |
"source": [ |
| 526 |
"pencil.senses()[0].synset()" |
| 527 |
] |
| 528 |
}, |
| 529 |
{ |
| 530 |
"cell_type": "code", |
| 531 |
"execution_count": 21, |
| 532 |
"metadata": {}, |
| 533 |
"outputs": [ |
| 534 |
{ |
| 535 |
"data": { |
| 536 |
"text/plain": [ |
| 537 |
"[Synset('omw-en-01688604-v')]" |
| 538 |
] |
| 539 |
}, |
| 540 |
"execution_count": 21, |
| 541 |
"metadata": {}, |
| 542 |
"output_type": "execute_result" |
| 543 |
} |
| 544 |
], |
| 545 |
"source": [ |
| 546 |
"pencil.synsets() # shorthand for the above" |
| 547 |
] |
| 548 |
}, |
| 549 |
{ |
| 550 |
"cell_type": "code", |
| 551 |
"execution_count": 22, |
| 552 |
"metadata": {}, |
| 553 |
"outputs": [ |
| 554 |
{ |
| 555 |
"data": { |
| 556 |
"text/plain": [ |
| 557 |
"'write, draw, or trace with a pencil'" |
| 558 |
] |
| 559 |
}, |
| 560 |
"execution_count": 22, |
| 561 |
"metadata": {}, |
| 562 |
"output_type": "execute_result" |
| 563 |
} |
| 564 |
], |
| 565 |
"source": [ |
| 566 |
"pencil_ss = pencil.synsets()[0]\n", |
| 567 |
"pencil_ss.definition()" |
| 568 |
] |
| 569 |
}, |
| 570 |
{ |
| 571 |
"cell_type": "code", |
| 572 |
"execution_count": 23, |
| 573 |
"metadata": {}, |
| 574 |
"outputs": [ |
| 575 |
{ |
| 576 |
"data": { |
| 577 |
"text/plain": [ |
| 578 |
"['he penciled a figure']" |
| 579 |
] |
| 580 |
}, |
| 581 |
"execution_count": 23, |
| 582 |
"metadata": {}, |
| 583 |
"output_type": "execute_result" |
| 584 |
} |
| 585 |
], |
| 586 |
"source": [ |
| 587 |
"pencil_ss.examples()" |
| 588 |
] |
| 589 |
}, |
| 590 |
{ |
| 591 |
"cell_type": "code", |
| 592 |
"execution_count": 24, |
| 593 |
"metadata": {}, |
| 594 |
"outputs": [ |
| 595 |
{ |
| 596 |
"data": { |
| 597 |
"text/plain": [ |
| 598 |
"[Synset('omw-en-01690294-v')]" |
| 599 |
] |
| 600 |
}, |
| 601 |
"execution_count": 24, |
| 602 |
"metadata": {}, |
| 603 |
"output_type": "execute_result" |
| 604 |
} |
| 605 |
], |
| 606 |
"source": [ |
| 607 |
"pencil_ss.hypernyms()" |
| 608 |
] |
| 609 |
}, |
| 610 |
{ |
| 611 |
"cell_type": "code", |
| 612 |
"execution_count": 25, |
| 613 |
"metadata": {}, |
| 614 |
"outputs": [ |
| 615 |
{ |
| 616 |
"data": { |
| 617 |
"text/plain": [ |
| 618 |
"['draw']" |
| 619 |
] |
| 620 |
}, |
| 621 |
"execution_count": 25, |
| 622 |
"metadata": {}, |
| 623 |
"output_type": "execute_result" |
| 624 |
} |
| 625 |
], |
| 626 |
"source": [ |
| 627 |
"pencil_ss.hypernyms()[0].lemmas()" |
| 628 |
] |
| 629 |
}, |
| 630 |
{ |
| 631 |
"cell_type": "markdown", |
| 632 |
"metadata": {}, |
| 633 |
"source": [ |
| 634 |
"## Taxonomy Queries\n", |
| 635 |
"\n", |
| 636 |
"A common usage of wordnets is exploring the taxonomic structure via hypernym and hyponym relations. These operations thus have some more dedicated functions. For instance, path functions show the synsets from the starting synset to some other synset or the taxonomic root, such as [Synset.hypernym_paths()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Synset.hypernym_paths):" |
| 637 |
] |
| 638 |
}, |
| 639 |
{ |
| 640 |
"cell_type": "code", |
| 641 |
"execution_count": 26, |
| 642 |
"metadata": {}, |
| 643 |
"outputs": [ |
| 644 |
{ |
| 645 |
"name": "stdout", |
| 646 |
"output_type": "stream", |
| 647 |
"text": [ |
| 648 |
" Synset('omw-en-01690294-v') ['draw']\n", |
| 649 |
" Synset('omw-en-01686132-v') ['represent', 'interpret']\n", |
| 650 |
" Synset('omw-en-01619354-v') ['re-create']\n", |
| 651 |
" Synset('omw-en-01617192-v') ['make', 'create']\n" |
| 652 |
] |
| 653 |
} |
| 654 |
], |
| 655 |
"source": [ |
| 656 |
"for path in pencil_ss.hypernym_paths():\n", |
| 657 |
" for i, ss in enumerate(path):\n", |
| 658 |
" print(\" \" * i, ss, ss.lemmas())" |
| 659 |
] |
| 660 |
}, |
| 661 |
{ |
| 662 |
"cell_type": "markdown", |
| 663 |
"metadata": {}, |
| 664 |
"source": [ |
| 665 |
"Paths do not include the starting synset, so the length of the path (i.e., number of edges) is the length of the list of synsets. The length from a synset to the root is called the *depth*. However, as some synsets have multiple paths to the root, there is not always one single depth. Instead, the [Synset.min_depth()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Synset.min_depth) and [Synset.max_depth()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Synset.max_depth) methods find the lengths of the shortest and longest paths." |
| 666 |
] |
| 667 |
}, |
| 668 |
{ |
| 669 |
"cell_type": "code", |
| 670 |
"execution_count": 27, |
| 671 |
"metadata": {}, |
| 672 |
"outputs": [ |
| 673 |
{ |
| 674 |
"data": { |
| 675 |
"text/plain": [ |
| 676 |
"2" |
| 677 |
] |
| 678 |
}, |
| 679 |
"execution_count": 27, |
| 680 |
"metadata": {}, |
| 681 |
"output_type": "execute_result" |
| 682 |
} |
| 683 |
], |
| 684 |
"source": [ |
| 685 |
"dog = en.synsets(\"dog\", pos=\"n\")[0]\n", |
| 686 |
"len(dog.hypernym_paths()) # two paths" |
| 687 |
] |
| 688 |
}, |
| 689 |
{ |
| 690 |
"cell_type": "code", |
| 691 |
"execution_count": 28, |
| 692 |
"metadata": {}, |
| 693 |
"outputs": [ |
| 694 |
{ |
| 695 |
"data": { |
| 696 |
"text/plain": [ |
| 697 |
"(8, 13)" |
| 698 |
] |
| 699 |
}, |
| 700 |
"execution_count": 28, |
| 701 |
"metadata": {}, |
| 702 |
"output_type": "execute_result" |
| 703 |
} |
| 704 |
], |
| 705 |
"source": [ |
| 706 |
"dog.min_depth(), dog.max_depth()" |
| 707 |
] |
| 708 |
}, |
| 709 |
{ |
| 710 |
"cell_type": "markdown", |
| 711 |
"metadata": {}, |
| 712 |
"source": [ |
| 713 |
"It is also possible to find paths between two synsets by their lowest common hypernym (also called *least common subsumer*). Here I compare the verbs *pencil* and *pen*:" |
| 714 |
] |
| 715 |
}, |
| 716 |
{ |
| 717 |
"cell_type": "code", |
| 718 |
"execution_count": 29, |
| 719 |
"metadata": {}, |
| 720 |
"outputs": [ |
| 721 |
{ |
| 722 |
"name": "stdout", |
| 723 |
"output_type": "stream", |
| 724 |
"text": [ |
| 725 |
" Synset('omw-en-01697816-v') ['create verbally']\n", |
| 726 |
" Synset('omw-en-01617192-v') ['make', 'create']\n" |
| 727 |
] |
| 728 |
} |
| 729 |
], |
| 730 |
"source": [ |
| 731 |
"pen_ss = en.synsets(\"pen\", pos=\"v\")[0]\n", |
| 732 |
"for path in pen_ss.hypernym_paths():\n", |
| 733 |
" for i, ss in enumerate(path):\n", |
| 734 |
" print(\" \" * i, ss, ss.lemmas())" |
| 735 |
] |
| 736 |
}, |
| 737 |
{ |
| 738 |
"cell_type": "code", |
| 739 |
"execution_count": 30, |
| 740 |
"metadata": {}, |
| 741 |
"outputs": [ |
| 742 |
{ |
| 743 |
"data": { |
| 744 |
"text/plain": [ |
| 745 |
"[Synset('omw-en-01617192-v')]" |
| 746 |
] |
| 747 |
}, |
| 748 |
"execution_count": 30, |
| 749 |
"metadata": {}, |
| 750 |
"output_type": "execute_result" |
| 751 |
} |
| 752 |
], |
| 753 |
"source": [ |
| 754 |
"pencil_ss.lowest_common_hypernyms(pen_ss)" |
| 755 |
] |
| 756 |
}, |
| 757 |
{ |
| 758 |
"cell_type": "code", |
| 759 |
"execution_count": 31, |
| 760 |
"metadata": {}, |
| 761 |
"outputs": [ |
| 762 |
{ |
| 763 |
"name": "stdout", |
| 764 |
"output_type": "stream", |
| 765 |
"text": [ |
| 766 |
"Synset('omw-en-01690294-v') ['draw']\n", |
| 767 |
"Synset('omw-en-01686132-v') ['represent', 'interpret']\n", |
| 768 |
"Synset('omw-en-01619354-v') ['re-create']\n", |
| 769 |
"Synset('omw-en-01617192-v') ['make', 'create']\n", |
| 770 |
"Synset('omw-en-01697816-v') ['create verbally']\n", |
| 771 |
"Synset('omw-en-01698271-v') ['write', 'compose', 'pen', 'indite']\n" |
| 772 |
] |
| 773 |
} |
| 774 |
], |
| 775 |
"source": [ |
| 776 |
"for ss in pencil_ss.shortest_path(pen_ss):\n", |
| 777 |
" print(ss, ss.lemmas())" |
| 778 |
] |
| 779 |
}, |
| 780 |
{ |
| 781 |
"cell_type": "markdown", |
| 782 |
"metadata": {}, |
| 783 |
"source": [ |
| 784 |
"## Interlingual Queries\n", |
| 785 |
"\n", |
| 786 |
"In Wn, each wordnet (lexicon) added to the database is given its own, independent structure. All queries that traverse across wordnets make use of the Interlingual index (ILI) on synsets." |
| 787 |
] |
| 788 |
}, |
| 789 |
{ |
| 790 |
"cell_type": "code", |
| 791 |
"execution_count": 32, |
| 792 |
"metadata": {}, |
| 793 |
"outputs": [ |
| 794 |
{ |
| 795 |
"data": { |
| 796 |
"text/plain": [ |
| 797 |
"'a thin cylindrical pointed writing implement; a rod of marking substance encased in wood'" |
| 798 |
] |
| 799 |
}, |
| 800 |
"execution_count": 32, |
| 801 |
"metadata": {}, |
| 802 |
"output_type": "execute_result" |
| 803 |
} |
| 804 |
], |
| 805 |
"source": [ |
| 806 |
"pencil_ss = en.synsets(\"pencil\", pos=\"n\")[0] # for this we'll use the nominal sense\n", |
| 807 |
"pencil_ss.definition()" |
| 808 |
] |
| 809 |
}, |
| 810 |
{ |
| 811 |
"cell_type": "markdown", |
| 812 |
"metadata": {}, |
| 813 |
"source": [ |
| 814 |
"To get the corresponding words, senses, or synsets in some other lexicon, use the [Word.translate()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Word.translate), [Sense.translate()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Sense.translate), and [Synset.translate()](https://wn.readthedocs.io/en/latest/api/wn.html#wn.Synset.translate) functions. Of these, the function on the sense is the most natural, as it translates a specific meaning of a specific word, although all translations go through the synsets. As a word may have many senses, translating a word returns a mapping of each sense to its list of translations." |
| 815 |
] |
| 816 |
}, |
| 817 |
{ |
| 818 |
"cell_type": "code", |
| 819 |
"execution_count": 33, |
| 820 |
"metadata": {}, |
| 821 |
"outputs": [ |
| 822 |
{ |
| 823 |
"data": { |
| 824 |
"text/plain": [ |
| 825 |
"['lapis', 'matita']" |
| 826 |
] |
| 827 |
}, |
| 828 |
"execution_count": 33, |
| 829 |
"metadata": {}, |
| 830 |
"output_type": "execute_result" |
| 831 |
} |
| 832 |
], |
| 833 |
"source": [ |
| 834 |
"pencil_ss.translate(lang=\"it\")[0].lemmas()" |
| 835 |
] |
| 836 |
}, |
| 837 |
{ |
| 838 |
"cell_type": "code", |
| 839 |
"execution_count": 34, |
| 840 |
"metadata": {}, |
| 841 |
"outputs": [ |
| 842 |
{ |
| 843 |
"data": { |
| 844 |
"text/plain": [ |
| 845 |
"['ペンシル', '木筆', '鉛筆']" |
| 846 |
] |
| 847 |
}, |
| 848 |
"execution_count": 34, |
| 849 |
"metadata": {}, |
| 850 |
"output_type": "execute_result" |
| 851 |
} |
| 852 |
], |
| 853 |
"source": [ |
| 854 |
"pencil_ss.translate(lexicon=\"omw-ja\")[0].lemmas()" |
| 855 |
] |
| 856 |
}, |
| 857 |
{ |
| 858 |
"cell_type": "code", |
| 859 |
"execution_count": 35, |
| 860 |
"metadata": {}, |
| 861 |
"outputs": [ |
| 862 |
{ |
| 863 |
"data": { |
| 864 |
"text/plain": [ |
| 865 |
"{Sense('omw-en-pencil-03908204-n'): [Word('omw-ja-ペンシル-n'),\n", |
| 866 |
" Word('omw-ja-木筆-n'),\n", |
| 867 |
" Word('omw-ja-鉛筆-n')],\n", |
| 868 |
" Sense('omw-en-pencil-14796748-n'): [Word('omw-ja-鉛筆-n')],\n", |
| 869 |
" Sense('omw-en-pencil-13863020-n'): [],\n", |
| 870 |
" Sense('omw-en-pencil-03908456-n'): [Word('omw-ja-ペンシル-n')]}" |
| 871 |
] |
| 872 |
}, |
| 873 |
"execution_count": 35, |
| 874 |
"metadata": {}, |
| 875 |
"output_type": "execute_result" |
| 876 |
} |
| 877 |
], |
| 878 |
"source": [ |
| 879 |
"en.words(\"pencil\", pos=\"n\")[0].translate(lexicon=\"omw-ja\")" |
| 880 |
] |
| 881 |
}, |
| 882 |
{ |
| 883 |
"cell_type": "markdown", |
| 884 |
"metadata": {}, |
| 885 |
"source": [ |
| 886 |
"Interlingual synsets are also used to traversing relations from another wordnet. For instance, many of the lexicons in the [Open Multilingual Wordnet](https://github.com/omwn/omw-data) were created using the *expand* method where only words were translated on top of Princeton WordNet synsets. All relations (hypernyms, hyponyms, etc.) then depend on those from WordNet. In Wn, a [Wordnet](https://wn.readthedocs.io/en/latest/api/wn.html#the-wordnet-class) object may be instantiated with an `expand` parameter which selects lexicons containing such relations. By default, all lexicons are used (i.e., `expand='*'`), but you can tell Wn to not use any expand lexicons (`expand=''`) or to use a specific lexicon (`expand='omw-en:1.4'`). By being specific, you can better control the behaviour of your program, e.g., for experimental reproducibility." |
| 887 |
] |
| 888 |
}, |
| 889 |
{ |
| 890 |
"cell_type": "code", |
| 891 |
"execution_count": 36, |
| 892 |
"metadata": {}, |
| 893 |
"outputs": [ |
| 894 |
{ |
| 895 |
"data": { |
| 896 |
"text/plain": [ |
| 897 |
"[Synset('omw-ja-14796575-n')]" |
| 898 |
] |
| 899 |
}, |
| 900 |
"execution_count": 36, |
| 901 |
"metadata": {}, |
| 902 |
"output_type": "execute_result" |
| 903 |
} |
| 904 |
], |
| 905 |
"source": [ |
| 906 |
"# by default, any other installed lexicon may be used\n", |
| 907 |
"wn.Wordnet(lexicon=\"omw-ja\").synsets(\"鉛筆\")[0].hypernyms()" |
| 908 |
] |
| 909 |
}, |
| 910 |
{ |
| 911 |
"cell_type": "code", |
| 912 |
"execution_count": 37, |
| 913 |
"metadata": {}, |
| 914 |
"outputs": [ |
| 915 |
{ |
| 916 |
"data": { |
| 917 |
"text/plain": [ |
| 918 |
"[]" |
| 919 |
] |
| 920 |
}, |
| 921 |
"execution_count": 37, |
| 922 |
"metadata": {}, |
| 923 |
"output_type": "execute_result" |
| 924 |
} |
| 925 |
], |
| 926 |
"source": [ |
| 927 |
"# disable interlingual query expansion\n", |
| 928 |
"wn.Wordnet(lexicon=\"omw-ja\", expand=\"\").synsets(\"鉛筆\")[0].hypernyms()" |
| 929 |
] |
| 930 |
}, |
| 931 |
{ |
| 932 |
"cell_type": "code", |
| 933 |
"execution_count": 38, |
| 934 |
"metadata": {}, |
| 935 |
"outputs": [ |
| 936 |
{ |
| 937 |
"data": { |
| 938 |
"text/plain": [ |
| 939 |
"[Synset('omw-ja-14796575-n')]" |
| 940 |
] |
| 941 |
}, |
| 942 |
"execution_count": 38, |
| 943 |
"metadata": {}, |
| 944 |
"output_type": "execute_result" |
| 945 |
} |
| 946 |
], |
| 947 |
"source": [ |
| 948 |
"# specify the expand set\n", |
| 949 |
"wn.Wordnet(lexicon=\"omw-ja\", expand=\"omw-en:1.4\").synsets(\"鉛筆\")[0].hypernyms()" |
| 950 |
] |
| 951 |
}, |
| 952 |
{ |
| 953 |
"cell_type": "code", |
| 954 |
"execution_count": null, |
| 955 |
"metadata": {}, |
| 956 |
"outputs": [], |
| 957 |
"source": [] |
| 958 |
} |
| 959 |
], |
| 960 |
"metadata": { |
| 961 |
"kernelspec": { |
| 962 |
"display_name": "Python 3 (ipykernel)", |
| 963 |
"language": "python", |
| 964 |
"name": "python3" |
| 965 |
}, |
| 966 |
"language_info": { |
| 967 |
"codemirror_mode": { |
| 968 |
"name": "ipython", |
| 969 |
"version": 3 |
| 970 |
}, |
| 971 |
"file_extension": ".py", |
| 972 |
"mimetype": "text/x-python", |
| 973 |
"name": "python", |
| 974 |
"nbconvert_exporter": "python", |
| 975 |
"pygments_lexer": "ipython3", |
| 976 |
"version": "3.9.5" |
| 977 |
} |
| 978 |
}, |
| 979 |
"nbformat": 4, |
| 980 |
"nbformat_minor": 4 |
| 981 |
} |