Last active
June 27, 2021 18:19
-
-
Save vsraptor/8815944d1e75947ad0c3676c0279519a to your computer and use it in GitHub Desktop.
Easy way to explore Wordnet !!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[car]" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"w('car')|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['ewn-car-n']" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"w('car')|ids|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[automobile:motorcar:machine:car:auto, railcar:railroad car:car:railway car, car:gondola, elevator car:car, cable car:car]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"w('car')|syns|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[automobile:motorcar:machine:car:auto, railcar:railroad car:car:railway car, car:gondola, elevator car:car, cable car:car]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('car')|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['ewn-02961779-n', 'ewn-02963378-n', 'ewn-02963937-n', 'ewn-02963788-n', 'ewn-02937835-n']" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('car')|ids|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[lemon,\n", | |
" lemon:maize:gamboge:lemon yellow,\n", | |
" lemon:lemon tree:Citrus limon,\n", | |
" lemon,\n", | |
" lemon:stinker,\n", | |
" orange,\n", | |
" orange:orangeness,\n", | |
" orange tree:orange,\n", | |
" orange,\n", | |
" orangish:orange,\n", | |
" Orange River:Orange]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"w(['lemon','orange'])|syns|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[wheeled vehicle, steering system:steering mechanism]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('wheel')|partof|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[simple machine:machine,\n", | |
" handwheel,\n", | |
" force,\n", | |
" helm,\n", | |
" game equipment,\n", | |
" instrument of torture,\n", | |
" wheeled vehicle,\n", | |
" rotate:go around:revolve,\n", | |
" transport,\n", | |
" move:go:travel:locomote,\n", | |
" ride]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('wheel')|isa|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[cabochon, opaque gem, transparent gem, semi-precious stone, bran muffin, corn muffin, popover, crown jewel, solitaire, diamond, ruby, pearl, emerald, sapphire]" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('gem')|typeof|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[gas:gun:accelerator pedal:gas pedal:accelerator:throttle,\n", | |
" air bag,\n", | |
" auto accessory,\n", | |
" automobile engine,\n", | |
" car horn:automobile horn:hooter:motor horn:horn,\n", | |
" buffer:fender,\n", | |
" bumper,\n", | |
" car door,\n", | |
" car mirror,\n", | |
" car seat]" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws('car')|haspart|take(10)|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(0.444, (wheel:roulette wheel, bullock:steer)),\n", | |
" (0.444, (wheel:bicycle:cycle:bike, bullock:steer)),\n", | |
" (0.400, (wheel, bullock:steer)),\n", | |
" (0.400, (wheel, bullock:steer)),\n", | |
" (0.400, (wheel:steering wheel, bullock:steer))]" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(ws('wheel'),ws('steer'))|prod|wup|sort|reverse|take(5)|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1.000, (stone:gem:gemstone, stone:gem:gemstone)),\n", | |
" (0.200, (gem:jewel, Lucy Stone:Stone)),\n", | |
" (0.200, (gem:jewel, Stone:Edward Durell Stone)),\n", | |
" (0.167, (gem:jewel, Harlan Stone:Harlan F. Stone:Stone:Harlan Fisk Stone)),\n", | |
" (0.167, (gem:jewel, Stone:Oliver Stone))]" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(ws('gem'),ws('stone'))|prod|path|sort|reverse|take(5)|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['ewn-motorcycle-n-03796045-01', 'ewn-bike-n-03796045-02']" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[list((ws('car'),ws('bike'))|prod|wup|sort|reverse|take(5))[0][1][1]]|sns|ids|lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
import re | |
import wn | |
import wn.similarity | |
from pipe import * | |
import itertools | |
@Pipe | |
def lst(iterable): return list(iterable) | |
@Pipe | |
def mapflat(it,selector): return builtins.map(selector, it) | chain | |
@Pipe | |
def prod(it): | |
for x in itertools.product(it[0],it[1]): yield x | |
def w(z): | |
if isinstance(z,list) : return [ wn.words(x) for x in z ] | chain | |
return iter(wn.words(z)) | |
syns = mapflat(lambda z : z.synsets()) | |
ws = lambda z: w(z) | syns | |
sns = mapflat(lambda z : z.senses()) | |
words = mapflat(lambda z : z.words()) | |
word = mapflat(lambda z : [z.word()]) | |
syn = mapflat(lambda z : [z.synset()]) | |
lems = mapflat(lambda z : z.lemmas()) | |
lem = mapflat(lambda z : [z.lemma()]) | |
ids = map(lambda z : z.id) | |
isa = mapflat(lambda z : z.hypernyms()) | |
typeof = mapflat(lambda z : z.hyponyms()) | |
partof = mapflat(lambda z : z.holonyms()) | |
haspart = mapflat(lambda z : z.meronyms()) | |
bto = where(lambda x: len(x) > 0) | |
wup = map(lambda pair : ( wn.similarity.wup(pair[0],pair[1]), pair) ) | |
path = map(lambda pair : ( wn.similarity.path(pair[0],pair[1]), pair) ) | |
lch = map(lambda pair : ( wn.similarity.lch(pair[0],pair[1],15), pair) ) | |
#monkey patching wordnet | |
def synset_repr(self): | |
return re.sub(r"[\[\]']", '', str(self.lemmas()) ).replace(', ',':') | |
def word_repr(self): return str(self.lemma()) | |
def sense_repr(self): return str(self.word()) | |
wn.Synset.__repr__ = synset_repr | |
wn.Sense.__repr__ = sense_repr | |
wn.Word.__repr__ = word_repr | |
wn.Synset.isa = wn.Synset.hypernyms | |
wn.Synset.typeof = wn.Synset.hyponyms | |
wn.Synset.partof = wn.Synset.holonyms | |
wn.Synset.haspart = wn.Synset.meronyms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment