Advertisement
alvations

Wordnet add synsets

Dec 24th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. from nltk.corpus import wordnet as wn
  2. from collections import defaultdict
  3. from nltk.corpus.reader.wordnet import Synset
  4. import cPickle as pickle
  5.  
  6. def synset2pos(x):
  7.     return str(x.offset)+'-'+x.pos
  8.  
  9. try:
  10.     wordnet = pickle.load(open('wn.pk','rb'))
  11. except:
  12.     wordnet = defaultdict(dict)
  13.     for i in wn.all_synsets():
  14.         offset= synset2pos(i)
  15.         hyper = [synset2pos(j) for j in i.hypernyms()]
  16.         hypo = [synset2pos(j) for j in i.hyponyms()]
  17.    
  18.         wordnet[offset] = {'hypers':hyper,'hypos':hypo, 'lemmas':i.lemma_names}
  19.    
  20.     with open('wn.pk','wb') as fout:
  21.         pickle.dump(wordnet,fout)  
  22.  
  23. wordnet['99999999-n'] = {'hypers':['12345678-n','23456789-n'],
  24.                                                  'hypos':['98765432-n','87654321-n'],
  25.                                                  'lemmas': ['foobar','barfoo']}
  26.                                                  
  27. wordnet['98765432-n'] = {'hypers':['99999999-n','23456789-n'],
  28.                                                  'hypos':['87654321-n'],
  29.                                                  'lemmas': ['barbar']}
  30.                                                  
  31. wordnet['87654321-n'] = {'hypers':['98765432-n','23456789-n'],
  32.                                                  'hypos':[],
  33.                                                  'lemmas': ['blacksheep']}
  34.  
  35. # To access synsets with query words.
  36. query = 'dog'
  37. print [i for i in wordnet if query in wordnet[i]['lemmas']]
  38. print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement