Esempio n. 1
0
def combine_trees(trees_to_combine):
	if len(trees_to_combine) == 0:
		return None
	hypergraphs_to_combine = []
	total_scores = sum(score for _, score in trees_to_combine)
	for tree, score in trees_to_combine:
		if total_scores != 0.0:
			score = score / total_scores
		else:
			score = 1.0 / len(trees_to_combine)
		computeSpans(tree)
		tree_hg = Hypergraph.from_tree(tree, score)
		tree_hg.sanity_check()
		hypergraphs_to_combine.append(tree_hg)

	final_hypergraph = hypergraphs_to_combine[0]
	for hypergraph in hypergraphs_to_combine[1:]:
		final_hypergraph.combine(hypergraph)
	return final_hypergraph
Esempio n. 2
0
				if len(self.head_index[m] - removed_edges) == 0:
					terminals.put(m)

		if len([edge for edge in self.edges if edge not in removed_edges]) > 0:
			raise Exception('Invalid attempt to topsort a hypergraph with cycles')
		else:
			# Any nodes not connected to the graph are essentially terminals, so just add them to the front of the list.
			sorted_nodes = list(self.nodes - set(sorted_nodes)) + sorted_nodes
			assert len(self.nodes) == len(sorted_nodes)
			return sorted_nodes


if __name__ == "__main__":
	s = u'(S (NP (DT le) (JJ petit) (NN garçon)) (VP (VB a) (VBN marché) (PP (P à) (NP (DT l\') (NN école)))) (. .))'
	tree = TreeNode.from_string(s)
	computeSpans(tree)
	hg = Hypergraph.from_tree(tree)
	hg.sanity_check()
	print 'HG has %d nodes and %d edges' % (len(hg.nodes), len(hg.edges))
	print hg.to_tree_string()
	for node in hg.topsort():
		print node
	#print hg.to_json()
	hg.add_virtual_nodes(2, False)
	hg.sanity_check()
	print 'HG has %d nodes and %d edges' % (len(hg.nodes), len(hg.edges))
	#print hg.to_json()
	hg.add_composed_edges(5)
	hg.sanity_check()
	print 'HG has %d nodes and %d edges' % (len(hg.nodes), len(hg.edges))
	#print hg.to_json()