nerd/nerd_client.py
def _process_query(self, query, prepared=False):
        """ Process query recursively, if the text is too long,
        it is split and processed bit a bit.

        Args:
            query (sdict): Text to be processed.
            prepared (bool): True when the query is ready to be submitted via
            POST request.
        Returns:
            str: Body ready to be submitted to the API.
        """

        # Exit condition and POST
        if prepared is True:
            files = {'query': str(query)}

            logger.debug('About to submit the following query {}'.format(query))

            res, status = self.post(
                self.disambiguate_service,
                files=files,
                headers={'Accept': 'application/json'},
            )

            if status == 200:
                return self.decode(res), status
            else:
                logger.debug('Disambiguation failed.')
                return None, status

        text = query['text']

        sentence_coordinates = [
            {
                "offsetStart": 0,
                "offsetEnd": len(text)
            }
        ]

        total_nb_sentences = len(sentence_coordinates)  # Sentences from text.
        sentences_groups = []

        if len(text) > self.max_text_length:
            res, status_code = self.segment(text)

            if status_code == 200:
                sentence_coordinates = res['sentences']
                total_nb_sentences = len(sentence_coordinates)
            else:
                logger.error('Error during the segmentation of the text.')

            logger.debug(
                'Text too long, split in {} sentences; building groups of {} '
                'sentences.'.format(
                    total_nb_sentences, self.sentences_per_group
                )
            )
            sentences_groups = self._group_sentences(
                total_nb_sentences,
                self.sentences_per_group
            )
        else:
            query['sentence'] = "true"

        if total_nb_sentences > 1:
            query['sentences'] = sentence_coordinates

        if len(sentences_groups) > 0:
            for group in sentences_groups:
                query['processSentence'] = group

                res, status_code = self._process_query(query, prepared=True)

                if status_code == 200:
                    if 'entities' in res:
                        query['entities'] = res[u'entities']
                    query['language'] = res[u'language']
                else:
                    logger.error(
                        "Error when processing the query {}".format(query)
                    )
                    return None, status_code

        else:
            res, status_code = self._process_query(query, prepared=True)

            if status_code == 200:
                query['language'] = res[u'language']
                if 'entities' in res:
                    query['entities'] = res[u'entities']
            else:
                logger.error("Error when processing the query {}".format(query))
                return None, status_code

        return query, status_code
def _process_query(self, query, prepared=False):
        # Exit condition and POST
        if prepared is True:
            files = {'query': str(query)}

            logger.debug('About to submit the following query {}'.format(query))

            res, status = self.post(
                self.disambiguate_service,
                files=files,
                headers={'Accept': 'application/json'},
            )

            if status == 200:
                return self.decode(res), status
            else:
                logger.debug('Disambiguation failed.')
                return None, status

        text = query['text']

        sentence_coordinates = [
            {
                "offsetStart": 0,
                "offsetEnd": len(text)
            }
        ]

        total_nb_sentences = len(sentence_coordinates)  # Sentences from text.
        sentences_groups = []

        if len(text) > self.max_text_length:
            res, status_code = self.segment(text)

            if status_code == 200:
                sentence_coordinates = res['sentences']
                total_nb_sentences = len(sentence_coordinates)
            else:
                logger.error('Error during the segmentation of the text.')

            logger.debug(
                'Text too long, split in {} sentences; building groups of {} '
                'sentences.'.format(
                    total_nb_sentences, self.sentences_per_group
                )
            )
            sentences_groups = self._group_sentences(
                total_nb_sentences,
                self.sentences_per_group
            )
        else:
            query['sentence'] = "true"

        if total_nb_sentences > 1:
            query['sentences'] = sentence_coordinates

        if len(sentences_groups) > 0:
            for group in sentences_groups:
                query['processSentence'] = group

                res, status_code = self._process_query(query, prepared=True)

                if status_code == 200:
                    if 'entities' in res:
                        query['entities'] = res[u'entities']
                    query['language'] = res[u'language']
                else:
                    logger.error(
                        "Error when processing the query {}".format(query)
                    )
                    return None, status_code

        else:
            res, status_code = self._process_query(query, prepared=True)

            if status_code == 200:
                query['language'] = res[u'language']
                if 'entities' in res:
                    query['entities'] = res[u'entities']
            else:
                logger.error("Error when processing the query {}".format(query))
                return None, status_code

        return query, status_code
def _process_query ( self , query , prepared = False ) : # Exit condition and POST if prepared is True : files = { 'query' : str ( query ) } logger . debug ( 'About to submit the following query {}' . format ( query ) ) res , status = self . post ( self . disambiguate_service , files = files , headers = { 'Accept' : 'application/json' } , ) if status == 200 : return self . decode ( res ) , status else : logger . debug ( 'Disambiguation failed.' ) return None , status text = query [ 'text' ] sentence_coordinates = [ { "offsetStart" : 0 , "offsetEnd" : len ( text ) } ] total_nb_sentences = len ( sentence_coordinates ) # Sentences from text. sentences_groups = [ ] if len ( text ) > self . max_text_length : res , status_code = self . segment ( text ) if status_code == 200 : sentence_coordinates = res [ 'sentences' ] total_nb_sentences = len ( sentence_coordinates ) else : logger . error ( 'Error during the segmentation of the text.' ) logger . debug ( 'Text too long, split in {} sentences; building groups of {} ' 'sentences.' . format ( total_nb_sentences , self . sentences_per_group ) ) sentences_groups = self . _group_sentences ( total_nb_sentences , self . sentences_per_group ) else : query [ 'sentence' ] = "true" if total_nb_sentences > 1 : query [ 'sentences' ] = sentence_coordinates if len ( sentences_groups ) > 0 : for group in sentences_groups : query [ 'processSentence' ] = group res , status_code = self . _process_query ( query , prepared = True ) if status_code == 200 : if 'entities' in res : query [ 'entities' ] = res [ u'entities' ] query [ 'language' ] = res [ u'language' ] else : logger . error ( "Error when processing the query {}" . format ( query ) ) return None , status_code else : res , status_code = self . _process_query ( query , prepared = True ) if status_code == 200 : query [ 'language' ] = res [ u'language' ] if 'entities' in res : query [ 'entities' ] = res [ u'entities' ] else : logger . error ( "Error when processing the query {}" . format ( query ) ) return None , status_code return query , status_code
Process query recursively, if the text is too long,
        it is split and processed bit a bit.

        Args:
            query (sdict): Text to be processed.
            prepared (bool): True when the query is ready to be submitted via
            POST request.
        Returns:
            str: Body ready to be submitted to the API.
Process query recursively if the text is too long it is split and processed bit a bit .