def show_sentence(self, root, node_type=None): """ Show a windows with the sentence nodes. :param node_type: The type of the nodes to show. :param root: The root node of the sentence to show. """ GraphWrapper.show_sentence(graph=self.graph, root=root, node_type=node_type)
def link_root(self, sentence, element): """ Link a word with the sentence where it appears :param sentence: The sentence root node :param element: The element """ GraphWrapper.link(graph=self.graph, origin=sentence, target=element, link_type=self.root_edge_type)
def link_sentences(self, sentence, next_sentence): """ link two consecutive sentences. :param sentence: first sentence of the union. :param next_sentence: Second sentence of the union. """ GraphWrapper.link(self.graph, sentence, next_sentence, self.sentence_order_edge_type, 1, self.sentence_order_edge_label)
def add_mention_of_named_entity(self, sentence, mention): """ Add a mention of an Named Entity. :param sentence: The sentence where the mention is. :param mention: The mention of the Name entity. """ GraphWrapper.link(graph=self.graph, origin=sentence, target=mention, link_type=self.named_entity_edge_type, label=self.named_entity_edge_label)
def set_head(self, parent, head): """ Set a child as parent head and inverse inherit some values. :param parent: The parent constituent :param head: The child constituent or word """ # Inverse inherit head[self.head_edge_type] = True # link parent[self.HEAD] = head GraphWrapper.link(self.graph, parent, head, self.head_edge_type)
def link_word(self, sentence, word): """ Link a word with the sentence where it appears. Also make a root link. :param sentence: The sentence root node :param word: The word node """ GraphWrapper.link(graph=self.graph, origin=sentence, target=word, link_type=self.word_edge_type)
def add_mention_of_gold_mention(self, sentence, mention): """ Add gold mention to a sentence. :param sentence: The sentence where the Gold mention is. :param mention: The gold mention """ GraphWrapper.link(graph=self.graph, origin=sentence, target=mention, link_type=self.gold_mention_edge_type, label=self.gold_mention_edge_label)
def set_head_word(self, element, head_word): """Set the head word of the element. :param element: Word, constituent or Named entity :param head_word: The word that is the head word """ if head_word[self.node_type] != self.word_node_type: raise Exception("No word as head word") GraphWrapper.link(self.graph, element, head_word, self.head_word_edge_type) element[self.HEADWORD] = head_word
def get_all_entity_mentions(self, entity): """ Get mentions of a entity :param entity: The source entity :return: A list of mentions(elements) """ return GraphWrapper.get_out_neighbours_by_relation_type( graph=self.graph, node=entity, relation_type=self.entity_edge_type)
def get_all_constituents(self): """ Get all named entities of the graph :return: A list of named entities """ return GraphWrapper.get_all_node_by_type( graph=self.graph, node_type=self.syntactic_node_type)
def get_all_words(self): """ Get all named entities of the graph :return: A list of named entities """ return GraphWrapper.get_all_node_by_type(graph=self.graph, node_type=self.word_node_type)
def get_all_coref_entities(self): """ Get all entities of the graph :return: A list of entities """ return GraphWrapper.get_all_node_by_type( graph=self.graph, node_type=self.entity_node_type)
def add_sentence(self, root_index, form, label, node_id): """ Create a new sentence in the graph. Also link it to the previous sentence. :param root_index: The index of the sentence. :param form: :param label: :param node_id: :return: The sentence node """ sentence_root_node = GraphWrapper.new_node( # graph=self.graph, graph=self.graph, node_type=self.root_type, node_id=node_id, form="{0}#{1}".format(self.root_label, form), label="{0}#{1}".format(self.root_label, label), ord=root_index, tag=self.root_pos, pos=self.root_pos, ) if self.previous_sentence: self.link_sentences(self.previous_sentence, sentence_root_node) self.previous_sentence = sentence_root_node return sentence_root_node
def link_dependency(self, dependency_from, dependency_to, dependency_type): """ Add a dependency relation to the graph. Remember that dependency relations are down-top. :param dependency_from: The origin of the link :param dependency_to: The target of the link :param dependency_type: The value of the link """ GraphWrapper.link(graph=self.graph, origin=dependency_from, target=dependency_to, link_type=self.dependency_edge_type, value=dependency_type, weight=1, label=self.dependency_edge_type + "_" + dependency_type)
def get_all_sentences(self): """ Get all the sentences of the graph. :return: A list of sentences. """ return GraphWrapper.get_all_node_by_type(graph=self.graph, node_type=self.root_type)
def link_syntax_terminal(self, parent, terminal): """ Link a word to a constituent. Also add the word to :param parent: :param terminal: :return: """ self.link_word(parent, terminal) label = self.syntactic_edge_type \ + "_" \ + self.syntactic_edge_value_terminal GraphWrapper.link(graph=self.graph, origin=parent, target=terminal, link_type=self.syntactic_edge_type, value=self.syntactic_edge_value_terminal, weight=1, label=label)
def get_root(self, element): """Get the sentence of the element :param element: The constituent or word whose parent is wanted. """ element = element.get(CONSTITUENT, element) return GraphWrapper.get_in_neighbour_by_relation_type( self.graph, element, self.root_edge_type)
def get_dependant_words(self, word): """ Get all words that depend on the word and the dependency type. :param word: The word where the dependency starts """ children = GraphWrapper.get_out_neighbours_by_relation_type( node=word, relation_type=self.dependency_edge_type, graph=self.graph, keys=True) return children
def get_syntactic_parent(self, element): """Return the syntactic parent of the node(constituent or word). :param element: The Word, constituent or Named entity whose parent is wanted. :return: The parent of the element. """ element = element.get(CONSTITUENT, element) return GraphWrapper.get_in_neighbour_by_relation_type( self.graph, element, self.syntactic_edge_type)
def link_syntax_non_terminal(self, parent, child): """ Link a non-terminal(constituent) to the constituent. Also link the constituent child word with the parent. :param parent: The parent constituent :param child: The child constituent """ for word in self.get_words(child): self.link_word(parent, word) label = self.syntactic_edge_label\ + "_" \ + self.syntactic_edge_value_branch GraphWrapper.link(graph=self.graph, origin=parent, target=child, link_type=self.syntactic_edge_type, value=self.syntactic_edge_value_branch, label=label)
def get_syntactic_children(self, element): """Get all the syntactical children of a element. :param element: The Word, constituent or Named entity whose children are wanted. """ element = element.get(CONSTITUENT, element) children = GraphWrapper.get_out_neighbours_by_relation_type( graph=self.graph, node=element, relation_type=self.syntactic_edge_type) return children
def get_governor_words(self, word): """ Get all words that rules a dependency link with the word and the dependency type. :param word: The word where the dependency ends """ children = GraphWrapper.get_in_neighbours_by_relation_type( node=word, relation_type=self.dependency_edge_type, graph=self.graph, keys=True) return children
def get_head(self, element): """Get the head of the element. If a word id passed by error the word itself is returned. :param element: Word, constituent or Named entity """ if element[self.node_type] == self.word_node_type: return element head = GraphWrapper.get_out_neighbour_by_relation_type( graph=self.graph, node=element, relation_type=self.head_edge_type) if head is None: return self.get_syntactic_children_sorted(element=element)[-1] return head
def add_coref_entity(self, node_id, mentions, label=None): """ Creates a entity with the identification provided and link to all the mentions passed. :param label: Optional label for the mention. :param mentions: List of mentions that forms the entity. :param node_id: identifier assigned to the mention. """ graph = self.graph # Create the node that links all mentions as an entity. new_node = GraphWrapper.new_node(graph=graph, node_type=self.entity_node_type, node_id=node_id, label=label) # Mention lis is a list of mention node identifiers for mention in mentions: GraphWrapper.link(self.graph, node_id, mention, self.entity_edge_type, label=self.entity_edge_label) return new_node
def get_sentence_named_entities(self, root): """Get the (Sorted)named entities contained in a sentence. This method not traverse the syntactic tree, used the root direct links. NOT USE WITH CONSTITUENT. :param root: the root of the sentence """ return sorted(GraphWrapper.get_out_neighbours_by_relation_type( graph=self.graph, node=root, relation_type=self.named_entity_edge_type), key=lambda y: y[SPAN])
def get_sentence_words(self, sentence): """Get the (Sorted)words contained in a sentence. This method not traverse the syntactic tree, used the root direct links. NOT USE WITH CONSTITUENT. :param sentence: the root node of the sentence. :return: The words of the sentence. """ return sorted(GraphWrapper.get_out_neighbours_by_relation_type( graph=self.graph, node=sentence, relation_type=self.word_edge_type), key=lambda y: y[SPAN])
def add_gold_mention(self, node_id, gold_entity, label): """Creates a gold mention into the graph. :param entity_id: The ID of the entity. :param node_id: The ID of the gold mention in the graph :param label: A label for representation uses. """ new_entity = GraphWrapper.new_node( graph=self.graph, node_type=self.gold_mention_node_type, node_id=node_id, label=label, gold_entity=gold_entity) return new_entity
def add_named_entity(self, entity_type, entity_id, label): """Creates a named entity into the graph. :param label: A label for representation uses. :param entity_id: The ID of the entity in the graph :param entity_type: The type of the NER """ new_entity = GraphWrapper.new_node( graph=self.graph, node_type=self.named_entity_node_type, node_id=entity_id, label=label, ner=entity_type, tag=entity_type) return new_entity
def get_words(self, element): """ Get the words(sorted in textual order) of the constituent. If constituent is a word, returns the words in a list. :param element: Word, constituent or Named entity :return the words of the element in a list """ if element[self.node_type] == self.word_node_type: return [element] words = GraphWrapper.get_out_neighbours_by_relation_type( self.graph, element, relation_type=self.word_edge_type) return sorted(words, key=lambda y: y[SPAN])
def add_constituent(self, node_id, sentence, tag, order, label=None): """ Add a new constituent to the graph. :param node_id: The unique id in the graph :param sentence: The sentence Root :param tag: The tag of the constituent :param order: The order of the constituent :param label: A label for representation proposes :return: The constituent node """ new_node = GraphWrapper.new_node(graph=self.graph, node_type=self.syntactic_node_type, node_id=node_id, tag=tag, label=label or tag, ord=order) if sentence: self.link_root(sentence, new_node) return new_node