Example #1
0
    def list(self, request):
        query = request.GET.get('query', '')

        response = {'text': query}

        start = time.time()

        trees = parse(query)

        if len(trees) > 0:
            tree = trees[0]
            response['parse'] = tree.pprint()
            response['concepts'] = list(extract_noun_phrases(tree))
            response['entities'] = list(entities(query))
        else:
            response['parse'] = None
            response['concepts'] = []
            response['entities'] = []

        # Add the status
        response["time"] = time.time() - start

        if response['parse'] is not None:
            status = 'parsed in %0.4f seconds; extracted %i concepts and %i entities'
            status = status % (response['time'], len(response['concepts']),
                               len(response['entities']))
        else:
            status = 'unable to parse input'

        response['status'] = status
        return Response(response)
Example #2
0
    def get_text_concepts(self):
        """
        Returns a list of strings of extracted noun phrases and the time
        it took to perform the concept extraction.
        """

        # Cannot get concepts if there is no parse
        if not self.parse:
            return [], 0.0

        start = time.time()
        tree  = tree_from_string(self.parse)

        # Concepts Parsing
        concepts = []
        current  = set([annotation.text for annotation in self.annotations.all()])
        for concept in extract_noun_phrases(tree):
            if concept not in current:
                concepts.append(concept)

        return (concepts, time.time()-start)