I wrote a function which creates graph from list of edges (taken from database). I use graph-tool library. Python and this library are quite new for me.
Every vertex in graph should be described, by pair of string and number. In function, if I consider adding new vertex to the graph, first I check if the vertex with same properties exists in the graph. To do this I use find_vertex function. I don't understand why the TypeError occurs, please help. Here you have code of this function and traceback (below):
Edit: One thing more I have to deal with unicode strings.
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
Traceback:
---------------------------------------------------------------------------
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
This is a bug in graph-tool. It has been fixed now in the git version: http://git.skewed.de/graph-tool/commit/?id=566d6dd816e167e1c9e824961537301ee1527e14