我在这里完全是个菜鸟,所以请原谅我的罪过。这就是问题所在:即使我只从《 Python的自然语言处理》手册中复制粘贴了代码,也找不到从下面发布的代码制作图形的方法。我的“唯一”目标是在论文中显示代码如何与WordNet中的图形交互。这是代码:
# Natural Language Toolkit: code_networkx
import networkx as nx
import matplotlib
from nltk.corpus import wordnet as wn
def traverse(graph, start, node):
graph.depth[node.name] = node.shortest_path_distance(start)
for child in node.hyponyms():
graph.add_edge(node.name, child.name) # [_add-edge]
traverse(graph, start, child) # [_recursive-traversal]
def hyponym_graph(start):
G = nx.Graph() # [_define-graph]
G.depth = {}
traverse(G, start, start)
return G
def graph_draw(graph):
nx.draw_graphviz(graph,
node_size = [16 * graph.degree(n) for n in graph],
node_color = [graph.depth[n] for n in graph],
with_labels = False)
matplotlib.pyplot.show()
>>> dog = wn.synset('dog.n.01')
>>> graph = hyponym_graph(dog)
>>> graph_draw(graph)
没有弹出错误,但是这里没有图形。请原谅我的消息长度,但我确实在您的手中! :/
答案 0 :(得分:0)
它不起作用,因为您依赖graphviz。这是一个可行的示例用法:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.petersen_graph()
nx.draw(G)
plt.show()
因此,您应该执行以下操作:
def graph_draw(graph):
nx.draw_shell(graph)
matplotlib.pyplot.show()