Ejemplo n.º 1
0
    def __init__(self, graph, init_embedding=None, radius=None, fit_radius=False):
        self.graph = graph
        n = self.graph.number_of_nodes()
        self.fit_radius = fit_radius
        if self.fit_radius:
            self.__expected_vector_size = 2 * n + 1
        else:
            self.__expected_vector_size = 2 * n

        # initialize embedding
        if radius is None:
            radius = 2 * np.log(n)

        self.embedding = {
            'vertices': {},
            'radius': radius,
        }

        def gen_random_coordinates(v):
            r = 2*np.log(float(n) / self.graph.degree(v))
            phi = np.random.uniform(0.0, 2*np.pi)
            return (r, phi)

        def delta_phi(r, R):
            # compute \Delta\phi from equality
            # \cosh(R) = \cosh^2(r) - \sinh^2(r) * \cos(\Delta\phi)
            cos_delta_phi = (np.cosh(r)**2 - np.cosh(R)) / np.sinh(r)**2
            cos_delta_phi = max(-1., min(1., cos_delta_phi))
            return np.arccos(cos_delta_phi)

        if init_embedding is not None:
            init_embedding_fringe = fringe(self.graph, init_embedding.embedding['vertices'])
            for v in self.graph.nodes():
                if v in init_embedding.embedding['vertices']:
                    self.embedding['vertices'][v] = init_embedding.embedding['vertices'][v]
                #elif any([_n in init_embedding.embedding['vertices'] for _n in self.graph.neighbors(v)]):
                elif v in init_embedding_fringe:
                    neigh = list(set(self.graph.neighbors(v)) & set(init_embedding.embedding['vertices']))[0]
                    r_n, phi_n = init_embedding.embedding['vertices'][neigh]
                    delta_phi_n = delta_phi(r_n, radius)
                    r_random, phi_zzz = gen_random_coordinates(v)
                    phi_random_with_delta = np.random.uniform(4*np.pi + phi_n - delta_phi_n, 4*np.pi + phi_n + delta_phi_n)
                    self.embedding['vertices'][v] = (r_random, phi_random_with_delta)
                else:
                    self.embedding['vertices'][v] = gen_random_coordinates(v)
        else:
            for v in self.graph.nodes():
                self.embedding['vertices'][v] = gen_random_coordinates(v)

        self._vertex2index = {}
        for i, v in enumerate(sorted(self.embedding['vertices'])):
            self._vertex2index[v] = i
Ejemplo n.º 2
0
def graph_specific(graph, proper=False, fringe=1):
    """ Returns a list of hyponym nodes in the fringe (all if None).
    """
    if fringe:
        nodes = graph.fringe(fringe)
    else:
        nodes = graph.nodes
    if graph.root:
        nodes = filter(lambda n: n.is_a(graph.root, direct=False), nodes)
    else:
        nodes = filter(lambda n: n.has_rule("is-a", direct=True), nodes)
    if proper:
        nodes = filter(lambda n: n in graph.proper_nouns(), nodes)
    return nodes
Ejemplo n.º 3
0
def graph_specific(graph, proper=False, fringe=1):
    """ Returns a list of hyponym nodes in the fringe (all if None).
    """
    if fringe:
        nodes = graph.fringe(fringe)
    else:
        nodes = graph.nodes
    if graph.root:
        nodes = filter(lambda n: n.is_a(graph.root, direct=False), nodes)
    else:
        nodes = filter(lambda n: n.has_rule("is-a", direct=True), nodes)
    if proper:
        nodes = filter(lambda n: n in graph.proper_nouns(), nodes)
    return nodes