Skip to content

Instantly share code, notes, and snippets.

@vsraptor
Last active June 27, 2021 18:19
Show Gist options
  • Save vsraptor/8815944d1e75947ad0c3676c0279519a to your computer and use it in GitHub Desktop.
Save vsraptor/8815944d1e75947ad0c3676c0279519a to your computer and use it in GitHub Desktop.
Easy way to explore Wordnet !!!
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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