def add_node(self, name, id, operator): if id in self.nodes: return self.nodes_no += 1 self.nodes.append(id) new_node = Node(name, id, operator) self.graph[id] = new_node
def clonegraph(node: Node) -> Node: if not node: return None if node in map: return map[node] val = node.val newNode = Node(val) map[node] = newNode for neighbor in node.neighbors: newNode.neighbors.append(clonegraph(neighbor)) return newNode
def _add_children(self, board, node): self._lut[node.coordinates] = node x = node.coordinates[0] y = node.coordinates[1] for check in [(0, 1), (0, -1), (1, 0), (-1, 0)]: temp_x = x + check[0] temp_y = y + check[1] if temp_x >= 0 and temp_x < board._board.shape[0]: if temp_y >= 0 and temp_y < board._board.shape[1]: if (temp_x, temp_y) not in self._lut: temp = Node(board._board[temp_x][temp_y], (temp_x, temp_y)) self._add_children(board, temp) else: temp = self._lut[(temp_x, temp_y)] if (temp_x, temp_y) not in map(lambda x: x[0], node.links) and \ node.coordinates not in map(lambda x: x[0], temp.links): if temp.value == node.value: node.links.append((temp.coordinates, 0.0000001)) temp.links.append((node.coordinates, 0.0000001)) else: node.links.append((temp.coordinates, 1)) temp.links.append((node.coordinates, 1))
def remove_node(self, node: Node): node.remove_from_neighbors() del self._nodes_by_key[node.get_key()] self._nodes.remove(node)
def add_node(self, node_properties: Dict, key: str = None, node_color: str = 'black', node_type: str = '') -> Node: node = Node(node_properties, key, node_color=node_color, node_type=node_type) self._nodes.append(node) self._nodes_by_key[key] = node return node
def build(self, graph, cursor, embeddings, index_utils, docid, use_entities, nr_terms=0, term_tfidf=0.0, term_position=0.0, text_distance=0.0, term_embedding=0.0): # Retrieve named entities from database. if use_entities: entities = db_utils.get_entities_from_docid( cursor, docid, 'entity_ids') # Create nodes for named entities # [['Washington Redskins', '[30]', '1', 'ORG']] for entity in entities: ent_name = entity[0] try: ent_positions = json.loads(entity[1]) except: print(f'issue with enitity: {entity[1]}') continue ent_tf = int(entity[2]) ent_type = entity[3] graph.add_node(Node(ent_name, ent_type, ent_positions, ent_tf)) # Retrieve top n tfidf terms from database. if nr_terms > 0.0: terms = db_utils.get_entities_from_docid(cursor, docid, 'tfidf_terms')[:nr_terms] # Create nodes for tfidf terms for term in terms[: nr_terms]: # [['Washington Redskins', '[30]', '1', 'ORG']] term_name = term[0] term_positions = json.loads(term[1]) term_tf = int(term[2]) graph.add_node(Node(term_name, 'term', term_positions, term_tf)) # Determine node weighs N = graph.nr_nodes() n_stat = index_utils.stats()['documents'] for node_name, node in graph.nodes.items(): weight = 0.0 if term_tfidf > 0: tf = tf_func(node, N) if node.node_type == 'term': df = index_utils.get_term_counts( utils.clean_NE_term(node_name), analyzer=None)[0] + 1e-5 weight += utils.tfidf(tf, df, n_stat) else: weight += tf if term_position > 0: weight += term_position * \ position_in_text(node, docid, index_utils) node.weight = weight # Enity weights differ in magnitide from terms, since they are tf only (normalize both individually). equalize_term_and_entities(graph) embeddings_not_found = 0 # Initialize edges + weights for node_key in graph.nodes.keys(): for other_node_key in graph.nodes.keys(): if node_key == other_node_key: continue weight = 0.0 if text_distance > 0: distance = closest_distance(graph.nodes[node_key], graph.nodes[other_node_key]) weight += text_distance * distance_in_text(distance) if term_embedding > 0: weight += term_embedding * edge_embedding_weight( graph.nodes[node_key], graph.nodes[other_node_key], embeddings, embeddings_not_found) if weight > 0.0: graph.add_edge(node_key, other_node_key, weight)
def _build_graph(self, board): self._root = Node(board.get(0, 0), (0, 0)) self._lut[(0, 0)] = self._root self._add_children(board, self._root)