Esempio n. 1
0
    def get_sentence_reference_graph(self, ):
        # a graph containing sentence to sentence links
        self.get_sentence_boundaries_amr()
        sentence_connections = []
        weights = {}
        for index_node, node in enumerate(self.amr):
            current_sent_index = self.node_index_to_sent_index(index_node)
            if current_sent_index == -1: continue
            current_var = node['variable']
            for location in self.var_to_index[current_var]:
                location_sent_index = self.node_index_to_sent_index(location)
                if location_sent_index != current_sent_index:
                    if (current_sent_index,
                            location_sent_index) not in sentence_connections:
                        sentence_connections.append(
                            (current_sent_index, location_sent_index))
                        sentence_connections.append(
                            (location_sent_index, current_sent_index))
                        weights[str(current_sent_index) + ' ' +
                                str(location_sent_index)] = 1
                        weights[str(location_sent_index) + ' ' +
                                str(current_sent_index)] = 1
                    else:
                        weights[str(current_sent_index) + ' ' +
                                str(location_sent_index)] += 1
                        weights[str(location_sent_index) + ' ' +
                                str(current_sent_index)] += 1

        self.sentence_reference_graph = Graph(
            connections=sentence_connections,
            nodes=range(0, len(self.sentence_boundries)),
            weights=weights)
Esempio n. 2
0
    def __init__(self,
                 text_list=[],
                 amr_with_attributes=False,
                 text='',
                 alignments=[],
                 var_to_sent={},
                 sent_index=0):
        # If the 'amr' that we get doesn't has attributes, it is just as text,
        # 	i.e. each element of the list is just a line of the text
        # else the amr that has all the attributes, and it is in the required form
        self.text_list = text_list
        self.amr = self.text_list
        # mapping from 'variables' to indices in self.amr
        self.var_to_index = {}
        if amr_with_attributes == False:
            # add attributes
            self.add_attributes()
            # add other attributes like 'variable_start_index'
            self.add_variable_info()
        # contains the edge lable for every class
        self.edges = {}
        self.connections = self.get_edge_info()

        self.get_var_to_index_mapping()

        # Contains all the 'variables' in the list
        self.nodes = self.get_node_info()
        self.common_text = self.get_common_text_var_mapping()

        # get 'var_to_sent'
        if var_to_sent == {}:
            for key in self.var_to_index.keys():
                var_to_sent[key] = [sent_index]
        self.var_to_sent = var_to_sent

        self.alignments = None
        self.get_alignments(alignments)
        # Not updated while mering any 2 nodes
        self.get_sentence_boundaries_amr()

        self.get_text_index_to_var()

        self.directed_graph = Graph(connections=self.connections,
                                    nodes=self.nodes,
                                    edge_lables=self.edges,
                                    var_to_sent=self.var_to_sent,
                                    common_text=self.common_text,
                                    text_index_to_var=self.text_index_to_var,
                                    root=self.amr[0]['variable'])

        self.topological_order = self.directed_graph.topological_order
        # self.text is a list of sentences in case of a document AMR
        self.text = text
        self.split_text = (' '.join(self.text)).split()

        # get detph_list
        self.depth_dict = {}
        self.get_depth_dict()
def main():
    g = Graph()
    g.add_edge(0, 1)
    g.add_edge(0, 5)
    # g.add_edge(1, 2)
    # g.add_edge(2, 3)
    g.add_edge(3, 4)
    g.add_edge(3, 5)
    g.add_edge(4, 0)
    g.add_edge(5, 4)
    # g.add_edge(5, 2)
    print route_between_nodes(g, 3, 5)
Esempio n. 4
0
	def reconstruct_amr(self):
		text_list=self.directed_graph.generate_text_amr()
		text_list =[line + '\n' for line in text_list]
	
		text_index_to_var = self.directed_graph.text_index_to_var
		var_to_sent = self.directed_graph.var_to_sent

		# Reconstruct the AMR after merging two nodes
		del self.text_list
		del self.amr
		del self.var_to_index
		del self.nodes
		del self.edges
		del self.directed_graph
		del self.topological_order
		del	self.depth_dict

		# self.text is a list of sentences in case of a document AMR
		self.text_list = text_list
		self.amr = self.text_list
		# mapping from 'variables' to indices in self.amr
		self.var_to_index = {}
		# add attributes
		self.add_attributes()
		# add other attributes like 'variable_start_index'
		self.add_variable_info()
		# contains the edge lable for every class
		self.edges = {}
		self.connections = self.get_edge_info()
		self.get_var_to_index_mapping()
		# Contains all the 'variables' in the list
		self.nodes = self.get_node_info()

		del self.var_to_sent
		self.var_to_sent = {}
		for var in var_to_sent.keys():
			if var in self.nodes:
				self.var_to_sent[var] = var_to_sent[var]

		self.common_text = self.get_common_text_var_mapping()

		temp = set(self.alignments.keys())
		del self.alignments
		self.alignments = {}
		for text_index in text_index_to_var.keys():
			# alignment in case of KeyError is mostly useless (but not always)
			self.alignments[text_index] = []
			for var in text_index_to_var[text_index]:
				try:	node_index = self.var_to_index[var][0]
				except KeyError:	break
				var_path = self.node_index_to_alignment(node_index)
				self.alignments[text_index].append(var_path)

		alignments = []
		for key in self.alignments.keys():
			for alignment in self.alignments[key]:
				alignments.append(key+'-'+'.'.join(alignment))

		self.alignments = None
		self.get_alignments(alignments)
		self.get_text_index_to_var()

		var_set = []
		for key in self.text_index_to_var.keys():
			var_set.extend(self.text_index_to_var[key])

		var_set = list(set(var_set))
		for var in var_set:
			if var not in self.nodes:
				print 'some bug'
				0/0
	
		self.directed_graph = Graph(connections=self.connections,nodes=self.nodes,
									edge_lables=self.edges,var_to_sent=self.var_to_sent,
									common_text=self.common_text,
									text_index_to_var=self.text_index_to_var,
									root=self.amr[0]['variable'])


		self.topological_order = self.directed_graph.topological_order
		self.get_depth_dict()