def read_graph(self):
        """
        Dataset is in txt each line is in the format "FromNodeId, ToNodeId"
        Since we are not interested on the weight, we get only the first two attributes
        We remove the last element in the list splitted [:-1]
        :return:
        """
        filename = self.path
        edges = []
        with open(filename) as f:
            for line in f:
                edges.append([int(n) for n in line.strip().split()][:-1])

        nodes = []
        for e in edges[:-1]:
            nodes.append(e[0])
            nodes.append(e[1])

        nodes = list(set(nodes))

        myGraph = Graph(len(nodes))
        for el in edges[:-1]:
            curr_edge = Edge(Node(el[0]), Node(el[1]))
            myGraph.add_edge(curr_edge)

            if self.is_undirected:
                curr_edge_inv = Edge(Node(el[1]), Node(el[0]))
                myGraph.add_edge(curr_edge_inv)

        return myGraph
    def __init__(self, input_file_name):
        self.nodes = []
        self.links = []
        self.removed_links = []

        input_file = open(input_file_name, "r")

        input_file.readline()
        for node in input_file:
            if node[:5] == "LINKS":
                break
            node = node.strip('\n')
            node_desc = node.split(' ')
            node_desc.pop()
            node_desc.pop(1)
            self.nodes.append(Node(node_desc[0], float(node_desc[1]), float(node_desc[2])))

        for link in input_file:
            link = link.strip('\n')
            link_desc = link.split(' ')
            link_desc.pop()
            link_desc.pop(1)
            for i in range(6):
                link_desc.pop(3)
            new_link = (Link(int(link_desc[0].lstrip('L')), self.get_node(link_desc[1]), self.get_node(link_desc[2]),
                             float(link_desc[3]),
                             float(link_desc[4])))
            self.links.append(new_link)
            for node in self.nodes:
                if node.city == link_desc[1]:
                    node.add_link(new_link)
                    break

        input_file.close()
Example #3
0
 def add_value(self, aisle):
     """
     adds aisle to graph
     :param aisle:(String) name of the aisle
     :return: Nothing
     """
     node = Node(aisle)
     self.vertices[aisle] = node
Example #4
0
    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
Example #5
0
        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
Example #6
0
 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))
Example #7
0
 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
Example #8
0
    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)
Example #9
0
 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)