Beispiel #1
0
 def sort_by_distance(self, root, concepts):
     """ Returns the list of concepts sorted by distance from root.
     """
     def _cmp(a, b):
         p1 = self.shortest_path(root, a)
         p2 = self.shortest_path(root, b)
         if p1 == None: return  1
         if p2 == None: return -1
         return len(p1) - len(p2)
     return sorted(concepts, _cmp)
    def sort_by_distance(self, root, concepts):
        """ Returns the list of concepts sorted by distance from root.
        """
        def _cmp(a, b):
            p1 = self.shortest_path(root, a)
            p2 = self.shortest_path(root, b)
            if p1 == None: return 1
            if p2 == None: return -1
            return len(p1) - len(p2)

        return sorted(concepts, _cmp)
 def __getattr__(self, a):
     """ Each attribute behaves as a list of hyponym nodes.
     Attributes are expected to be singular nouns.
     Some of these are fine-tuned in the range, others are generic.
     """
     if a in self.rules:
         concept, context, fringe, proper = self.rules[a]
         return self._hyponyms(concept, context, fringe, proper)
     elif a == "properties":
         g = cluster(None, context="properties", depth=None, wait=30)
         return sorted(g.keys())
     elif a == "basic properties" \
       or a == "basic_properties":
         return basic_properties
     else:
         a = a.replace("_", " ")
         return self._hyponyms(a, None, 2, False)
Beispiel #4
0
 def __getattr__(self, a):
     """ Each attribute behaves as a list of hyponym nodes.
     Attributes are expected to be singular nouns.
     Some of these are fine-tuned in the range, others are generic.
     """
     if a in self.rules:
         concept, context, fringe, proper = self.rules[a]
         return self._hyponyms(concept, context, fringe, proper)
     elif a == "properties":
         g = cluster(None, context="properties", depth=None, wait=30)
         return sorted(g.keys())
     elif a == "basic properties" \
       or a == "basic_properties":
           return basic_properties
     else:
         a = a.replace("_", " ")
         return self._hyponyms(a, None, 2, False)
Beispiel #5
0
def graph_enumerate_rules(graph, relation, distance=2, heuristic=None, reversed=False):
    """ Returns nodes in the graph that have given outgoing relation.
    Nodes directly connected to the root are at the beginning of the list,
    followed by nodes sorted by weight (eigenvalue).
    The distance of the shortest path between the root and the node is limited,
    and a heuristic for finding shortest paths can be passed.
    """
    nodes = []
    if graph.root:
        nodes = graph.root.enumerate_rules(relation, 1, not reversed)
    nodes  = sorted(nodes, lambda a, b: (a.betweenness < b.betweenness)*2-1) # XXX - use betweenness or eigenvalue?
    nodes += graph.nodes_by_eigenvalue(-1)
    nodes  = unique(nodes)
    nodes  = filter(lambda n: n.has_rule(relation, reversed=reversed), nodes)
    if graph.root and distance != None:
        if isinstance(heuristic, cost):
            heuristic.graph = graph
        nodes = graph.root.neighbors(nodes, distance, heuristic)
    return nodes
def graph_enumerate_rules(graph,
                          relation,
                          distance=2,
                          heuristic=None,
                          reversed=False):
    """ Returns nodes in the graph that have given outgoing relation.
    Nodes directly connected to the root are at the beginning of the list,
    followed by nodes sorted by weight (eigenvalue).
    The distance of the shortest path between the root and the node is limited,
    and a heuristic for finding shortest paths can be passed.
    """
    nodes = []
    if graph.root:
        nodes = graph.root.enumerate_rules(relation, 1, not reversed)
    nodes = sorted(nodes, lambda a, b: (a.betweenness < b.betweenness) * 2 - 1
                   )  # XXX - use betweenness or eigenvalue?
    nodes += graph.nodes_by_eigenvalue(-1)
    nodes = unique(nodes)
    nodes = filter(lambda n: n.has_rule(relation, reversed=reversed), nodes)
    if graph.root and distance != None:
        if isinstance(heuristic, cost):
            heuristic.graph = graph
        nodes = graph.root.neighbors(nodes, distance, heuristic)
    return nodes
 def _hyponyms(self, concept, context=None, fringe=2, proper=False):
     g = taxonomy(concept, context)
     return sorted([n.id for n in g.hyponyms(proper, fringe=fringe)])
Beispiel #8
0
 def _hyponyms(self, concept, context=None, fringe=2, proper=False):
     g = taxonomy(concept, context)
     return sorted([n.id for n in g.hyponyms(proper, fringe=fringe)])