我编写了一个从边列表(取自数据库)创建图形的函数。我使用图形工具库。Python 和这个库对我来说很新。
图中的每个顶点都应该用一对字符串和数字来描述。在函数中,如果我考虑向图中添加新顶点,首先我检查图中是否存在具有相同属性的顶点。为此,我使用 find_vertex 函数。我不明白为什么会发生 TypeError,请帮忙。这里有这个函数的代码和回溯(如下):
编辑:还有一件事我必须处理 unicode 字符串。
def make_graph():
conn = get_connection()
cursor = conn.cursor()
cursor.execute(sql)
wordnetList = cursor.fetchall()
g= Graph(directed=False)
vprop = g.new_vertex_property("python::object")
g.vertex_properties['lexicalunit'] = vprop
for (hyponym, v1, hyperonym, v2) in wordnetList: # hyponym and v1 (string and integer respectively) are properties of first vertex, hyperonym and v2 for the second
matched1 = find_vertex(g, g.vp['lexicalunit'], (hyponym, v1)) # this is line with problem
if len(matched1) == 0:
ver1 = g.add_vertex()
vprop[ver1] = (hyponym, v1)
elif len(matched1) >= 1:
ver1 = matched1[0]
matched2 = find_vertex(g, g.vp['lexicalunit'], (hyperonym, v2))
if len(matched2) == 0:
ver2 = g.add_vertex()
vprop[ver2] = (hyperonym, v2)
elif len(matched2) >= 1:
ver2 = matched2[0]
g.add_edge(ver1, ver2)
return g
追溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/olorin/Dokumenty/nlp-rr/CRFRelationRecogniser/python/wordnet_explorer/<ipython-input-2-163ed9b92398> in <module>()
----> 1 grf = make_graph()
/home/olorin/Dokumenty/nlp-rr/CRFRelationRecogniser/python/wordnet_explorer/hypgraph.py in make_graph()
65 for (hyponym, v1, hyperonym, v2) in wordnetList:
66 print(hyponym, v1, hyperonym, v2)
---> 67 matched1 = find_vertex(g, g.vp['lexicalunit'], (hyponym, v1))
68 if len(matched1) == 0:
69 ver1 = g.add_vertex()
/usr/lib/python2.7/dist-packages/graph_tool/util/__init__.pyc in find_vertex(g, prop, match)
53 can be either a :class:`~graph_tool.PropertyMap` or string with value "in",
54 "out" or "total", representing a degree type."""
---> 55 val = _convert(prop, match)
56 ret = libgraph_tool_util.\
57 find_vertex_range(weakref.ref(g), _degree(g, prop),
/usr/lib/python2.7/dist-packages/graph_tool/__init__.pyc in _convert(prop, val)
232 if type(vtype) is tuple:
233 return [vtype[1](x) for x in val]
--> 234 return vtype(val)
235
236
TypeError: object.__new__() takes no parameters