-1

Managed to create a hypernym graph but keep on getting bound method Synset.name of Synset('dog.n') instead of dog.n in the graph. Where is the mistake?

from nltk.corpus import wordnet as wn
import networkx as nx
import matplotlib.pyplot as plt

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


dog = wn.synsets('dog')[0]
G = closure_graph(dog,
                      lambda s: s.hypernyms())
index = nx.betweenness_centrality(G)
plt.rc('figure', figsize=(12, 7))
node_size = [index[n]*1000 for n in G]
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, node_size=node_size, edge_color='r', alpha=.3, linewidths=0)
plt.show()

Edit 1:

Managed to create a hypernym graph but keep on getting bound method Synset.name of Synset('dog.n') instead of dog.n in the graph. Where is the mistake?

Programmer_nltk
  • 863
  • 16
  • 38
  • Can you provide a complete example [MCVE] and can you tell us what you expect to happen in this code and what does happen? – Joel Oct 03 '16 at 03:42
  • Please see Edit 1. – Programmer_nltk Oct 03 '16 at 05:46
  • I'm not quite sure what you're describing for the output. But it looks like the function that creates the graph is doing something different in creating the nodes than you expect. I suspect that has something to do with the fact that you've defined `dog` to be `wn.synsets('dog')[0]`, while the link you provide has `dog = wn.synset('dog.n.01')`. – Joel Oct 04 '16 at 04:30
  • The issue is as explained in this link, thats why I added [0] http://stackoverflow.com/questions/27795623/not-getting-the-required-output-using-wordnet-synsets-definition-method – Programmer_nltk Oct 04 '16 at 13:25
  • I'm pretty sure I know what the error is (I can get the graph you're after on my machine). I would have found it a lot quicker if you had provided the full code necessary to reproduce your error. Before I say for sure what it is, I'd like to make sure that `wn`, `nx`, and `closure_graph` are defined the way I think they are. Can you provide a [MCVE]? It should be something I can just copy and paste and have it run. – Joel Oct 04 '16 at 23:32
  • I amended the question with M,C and V example. It runs to provide a messy graph. – Programmer_nltk Oct 05 '16 at 00:48

1 Answers1

2

So what is printing out is that networkx has created a bunch of nodes, each node being a function. So we need to look at the command that is adding nodes. This occurs in closure_graph.

In the definition of closure_graph, we see that s.name is added as a node. This is a function (it probably used to actually be the name before nltk was updated). Instead you want to add s.name() which is now a function that returns the name. There are 4 places where this occurs.

from nltk.corpus import wordnet as wn
import networkx as nx
import matplotlib.pyplot as plt

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


dog = wn.synsets('dog')[0]
G = closure_graph(dog,
                      lambda s: s.hypernyms())
index = nx.betweenness_centrality(G)
plt.rc('figure', figsize=(12, 7))
node_size = [index[n]*1000 for n in G]
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, node_size=node_size, edge_color='r', alpha=.3, linewidths=0)
plt.show()

enter image description here

Joel
  • 22,598
  • 6
  • 69
  • 93