In [1]:
from nltk.corpus import wordnet as wn
import nltk
import networkx as nx
#nltk.download()
In [2]:
def closure_graph(synset, fn):
seen = set()
graph = nx.DiGraph()
def recurse(s):
if not s in seen:
seen.add(s)
graph.add_node(s.name)
for s1 in fn(s):
graph.add_node(s1.name)
graph.add_edge(s.name, s1.name)
recurse(s1)
recurse(synset)
return graph
In [3]:
words = ['philosophy.n.01', 'sociology.n.01', 'physics.n.01', 'economics.n.01', 'social_science.n.01', 'social_scientist.n.01']
words1 = ['philosopher.n.01', 'sociologist.n.01', 'physicist.n.01', 'economist.n.01', 'natural_science.n.01', 'social_scientist.n.01', 'researcher.n.01']
for word in words1:
synlist = wn.synset(word)
graph = closure_graph(synlist,
lambda s: s.hypernyms()) #hyponyms
print graph.nodes()
Some more to look into:
- hypernyms, instance_hypernyms
- hyponyms, instance_hyponyms
- member_holonyms, substance_holonyms, part_holonyms
- member_meronyms, substance_meronyms, part_meronyms
- attributes
- entailments
- causes
- also_sees
- verb_groups
- similar_tos
In [4]:
wn.synset('sociology.n.01').definition
Out[4]:
In [11]:
wn.synset('philosophy.n.02').definition
Out[11]:
In [13]:
wn.synset('physics.n.01').definition
Out[13]:
In [14]:
wn.synset('social_science.n.01').definition
Out[14]:
In [15]:
wn.synset('natural_science.n.01').definition
Out[15]:
In [18]:
wn.synset('humanities.n.01').definition
Out[18]: