class NerService(tornado.web.RequestHandler):
        
    def initialize(self, concepts_inlinks, stopwords, entities, inlinks_threshold=400, MAX_WORDS=400, MAX_CHARS=2000):
        """
        """
        self.MAX_WORDS = MAX_WORDS
        self.MAX_CHARS = MAX_CHARS
        self.ner = Ner(concepts_inlinks, entities, stopwords, inlinks_threshold=inlinks_threshold, max_words=MAX_WORDS)
        
    def get(self):
        # Get parameters
        inlinks_threshold = int(self.get_argument("inlinks_threshold", default=400))
        self.ner.inlinks_threshold=inlinks_threshold
        text = self.get_argument("text")
        debug = self.get_argument("debug", default=False)
        # Check warnings if exists
        warning = []
        if len(text) > self.MAX_CHARS:
            warning.append('Only the first %d chars will be processed. This request is over this limit.' % self.MAX_CHARS)
        if  len(text.split(' ')) > self.MAX_WORDS:
            warning.append('Only the first %d words will be processed. This request is over this limit.' % self.MAX_WORDS)
        #result = json.loads(self.ner.fetch_entities(text))
        result = self.ner.fetch_entities(text)
        # Erase text at response
        del(result['text'])
        # if exists warning, append the flags to the output
        if len(warning) > 0:
            result['warnings'] = warning
        if debug:
            self.write(result)
        else:
            self.write({"concepts": list(result["results"].keys())})

    def post(self):
        results = list()
        for line in str(self.request.body, 'utf8').split('\n'):
            if line: 
                fields = line.split('\t')
                text = fields[0]
                #concepts = self.__format_post_result(self.ner.fetch_entities(text))
                concepts = self.ner.fetch_entities(text)
                print(concepts['results'])
                #result_fields = [concepts] + fields
                #result = '\t'.join(result_fields)
                concept_names = list(concepts['results'].keys())
                results.append({"text":text, "concepts":concept_names})
        #for temp in results:
        #    self.write('%s\n' % (temp))
        print(results)
        self.write({"response":results})

    def __format_post_result(self, response):
        response_dict = json.loads(response)
        concepts = response_dict['results'].keys()        
        if concepts:
            return ";;".join(concepts)
        else:
            return ""
Example #2
0
class NerService(tornado.web.RequestHandler):
    def initialize(self,
                   concepts_inlinks,
                   stopwords,
                   inlinks_threshold=400,
                   MAX_WORDS=400,
                   MAX_CHARS=2000):
        """
        """
        self.MAX_WORDS = MAX_WORDS
        self.MAX_CHARS = MAX_CHARS
        self.inlinks_threshold = inlinks_threshold
        self.ner = Ner(concepts_inlinks,
                       stopwords,
                       inlinks_threshold=inlinks_threshold,
                       max_words=MAX_WORDS)

    def get(self):
        # Get parameters
        inlinks_threshold = int(
            self.get_argument("inlinks_threshold",
                              default=self.inlinks_threshold))
        self.ner.inlinks_threshold = inlinks_threshold
        text = self.get_argument("text")
        debug = self.get_argument("debug", default=False)
        # Check warnings if exists
        warning = []
        if len(text) > self.MAX_CHARS:
            warning.append(
                'Only the first %d chars will be processed. This request is over this limit.'
                % self.MAX_CHARS)
        if len(text.split(' ')) > self.MAX_WORDS:
            warning.append(
                'Only the first %d words will be processed. This request is over this limit.'
                % self.MAX_WORDS)
        result = self.ner.fetch_entities(text)
        # Erase text at response
        del (result['text'])
        # if exists warning, append the flags to the output
        if len(warning) > 0:
            result['warnings'] = warning
        if debug:
            self.write(result)
        else:
            self.write({"concepts": list(result["results"].keys())})

    def post(self):
        results = list()
        for line in str(self.request.body, 'utf8').split('\n'):
            if line:
                fields = line.split('\t')
                text = fields[0]
                concepts = self.ner.fetch_entities(text)
                concept_names = list(concepts['results'].keys())
                results.append({"text": text, "concepts": concept_names})
        self.write({"response": results})

    def __format_post_result(self, response):
        response_dict = json.loads(response)
        concepts = response_dict['results'].keys()
        if concepts:
            return ";;".join(concepts)
        else:
            return ""