0
我生成一個圖表,其中包含一個句子的詞作爲記號和依賴關係作爲邊。Graph_tool的find_vertex()會產生一個異常
此信息來自((單詞,索引),(單詞,索引),關係)元組格式的外部工具。每個單詞可以有很多依賴關係,每個單詞只需要一個節點。
當我處理每個依賴我想通過自己的ID進行搜索,但是當我嘗試使用find_vertex()我得到下一個例外:
TypeError: No registered converter was able to extract a C++ reference to type graph_tool::GraphInterface from this Python object of type Graph
這是代碼:
def graph(self, sentence, sentenceNamespace="sentence", separator="_"):
graph = Graph()
root_node = graph.add_vertex()
# Create a link to Root Node
graph.graph_properties['root'] = graph.new_graph_property("python::object", root_node)
# Create Node's properties
graph.vertex_properties['id'] = graph.new_vertex_property("string")
graph.vertex_properties['form'] = graph.new_vertex_property("string")
graph.vertex_properties['ord'] = graph.new_vertex_property("int64_t")
# Create edge's properties
graph.edge_properties['type'] = graph.new_edge_property("string")
# Create the root node
graph.vertex_properties['form'][root_node] = "S"
graph.vertex_properties['ord'][root_node] = -1
# Find or create the dependant and governor nodes and link with a new edge
for dependency in self.dependency(sentence):
dependency_governor = dependency[0]
# Governor's generated ID
governor_name = "{1}{0}{2[0]}{0}{1[1]}".format(separator, sentenceNamespace,dependency_governor)
dependency_dependant = dependency[1]
# Dependant's generated ID
dependant_name = "{1}{0}{2[0]}{0}{1[1]}".format(separator, sentenceNamespace, dependency_dependant)
# Relation Type
relation = dependency[2]
# This is the problematic Line ################################################
governor = find_vertex(graph, graph.vertex_properties['id'], governor_name)
##############################################################
# If search is successful use the first node else create a new one
if governor :
governor = governor[0]
else:
governor = graph.add_vertex()
graph.vertex_properties['id'][governor] = governor_name
graph.vertex_properties['form'][governor] = dependency_governor[0]
graph.vertex_properties['ord'][governor] = dependency_governor[1]
dependant = find_vertex(graph, 'id', dependant_name)
# If search is successful use the first node else create a new one
if dependant :
dependant = dependant[0]
else:
dependant = graph.add_vertex()
graph.vertex_properties['id'][dependant] = dependant_name
graph.vertex_properties['form'][dependant] = dependant_dependant[0]
graph.vertex_properties['ord'][dependant] = dependency_dependant[1]
# Link the nodes with the new dependency relation
dependency_edge = graph.add_edge(governor, dependant)
graph.edge_properties['type'][dependency_edge]= relation
return graph
您是否考慮過使用本地Python網絡庫(如networkx或graphine)? –