Ejemplo n.º 1
0
    def answer(self):
        '''
        Answer the question.

        Returns the answer struct, something along the lines of:
        https://docs.google.com/document/d/1O6_sVSdSjgMmXacyI44JJfEVQLATagal9ydWLBgi-vE
        '''

        # get all subgraphs relevant to the question from the knowledge graph
        database = KnowledgeGraph()
        subgraphs = database.query(
            self)  # list of lists of nodes with 'id' and 'bound'
        G = database.getGraphByLabel(self.id)
        del database

        # compute scores with NAGA, export to json
        pr = ProtocopRank(G)
        score_struct, subgraphs = pr.report_scores_dict(
            subgraphs)  # returned subgraphs are sorted by rank

        out_struct = []
        for substruct, subgraph in zip(score_struct, subgraphs):
            graph = UniversalGraph(nodes=substruct['nodes'],
                                   edges=substruct['edges'])
            graph.merge_multiedges()
            graph.to_answer_walk(subgraph)

            out_struct += [
                {'nodes':graph.nodes,\
                'edges':graph.edges,\
                'score':substruct['score']},
                ]
        score_struct = out_struct

        for i in range(len(score_struct)):
            # score_struct[i]['edges'] = UniversalGraph.mergeMultiEdges(score_struct[i]['edges'])
            score_struct[i]['info'] = {
                'name': AnswerSet.constructName(score_struct[i])
            }

        max_results = 1000
        if len(score_struct) > max_results:
            return score_struct[:max_results]
        else:
            return score_struct