Exemple #1
0
def text(request):
    """This functionality calls the get_text() functionality to detect textual entities. It is called through api call

    Attributes:
        request: url parameters

    """
    try:
        parameters_dict = get_parameters_dictionary(request)
        ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        entity_output = get_text(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
                                 parameters_dict[PARAMETER_STRUCTURED_VALUE],
                                 parameters_dict[PARAMETER_FALLBACK_VALUE],
                                 parameters_dict[PARAMETER_BOT_MESSAGE])
        ner_logger.debug('Finished %s : %s ' % (parameters_dict[PARAMETER_ENTITY_NAME], entity_output))
    except TypeError, e:
        ner_logger.debug('Exception for text_synonym: %s ' % e)
        return HttpResponse(status=400)
Exemple #2
0
def get_entity_function(entity, message):
    """Calls the specific detection logic based on entity name. entity name plays crucial role in detecting textual
    entities (restaurant, cuisine, occupation, etc) but not while detecting phone number, email, etc.

    In this functionality we define a dictionary called as entity_function_dictionary = {}.
    In entity_function_dictionary  key is the name of the entity and the value is which functionality to call for that
    entity

    Attributes:
        entity: name of the entity that calls specific detection logic
        message: message on which detection logic needs to run

    Example:
        entity_output = get_entity_function(entity='date', message='set me reminder on 30th March')
        print entity_output
    """
    entity_function_dictionary = {
        'date': get_date,
        'time': get_time,
        'email': get_email,
        'phone_number': get_phone_number,
        'budget': get_budget,
        'number': get_number,
        'city': get_city,
        'date_advance': get_date_advance,
        'train_pnr': get_pnr,
        'flight_pnr': get_pnr,
        'shopping_size': get_shopping_size,
        'otp_number': get_number,
        'order_id': get_number
    }
    if entity in entity_function_dictionary:
        return entity_function_dictionary.get(entity)(message=message,
                                                      entity_name=entity,
                                                      structured_value=None,
                                                      fallback_value=None,
                                                      bot_message=None)
    else:
        return get_text(message=message,
                        entity_name=entity,
                        structured_value=None,
                        fallback_value=None,
                        bot_message=None)
Exemple #3
0
def text(request):
    """
    Run text detector with crf model on the 'message' passed in the request

    Args:
        request (django.http.HttpRequest): HTTP response from url

    Returns:
       dict: GET parameters from the request
    """
    try:
        parameters_dict = get_parameters_dictionary(request)
        ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        entity_output = get_text(
            message=parameters_dict[PARAMETER_MESSAGE],
            entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
            structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE],
            fallback_value=parameters_dict[PARAMETER_FALLBACK_VALUE],
            bot_message=parameters_dict[PARAMETER_BOT_MESSAGE],
            language=parameters_dict[PARAMETER_SOURCE_LANGUAGE],
            fuzziness=parameters_dict[PARAMETER_FUZZINESS],
            min_token_len_fuzziness=parameters_dict[
                PARAMETER_MIN_TOKEN_LEN_FUZZINESS],
            live_crf_model_path=parameters_dict[PARAMETER_LIVE_CRF_MODEL_PATH],
            read_model_from_s3=parameters_dict[PARAMETER_READ_MODEL_FROM_S3],
            read_embeddings_from_remote_url=parameters_dict[
                PARAMETER_READ_EMBEDDINGS_FROM_REMOTE_URL],
        )
        ner_logger.debug(
            'Finished %s : %s ' %
            (parameters_dict[PARAMETER_ENTITY_NAME], entity_output))
    except TypeError as e:
        ner_logger.exception('Exception for text_synonym: %s ' % e)
        return HttpResponse(status=500)
    return HttpResponse(json.dumps({'data': entity_output}),
                        content_type='application/json')
Exemple #4
0
def text(request):
    """
    Run text detector with crf model on the 'message or list of messages' passed in the request

    Args:
        request (django.http.HttpRequest): HTTP response from url

    Returns:
        response (django.http.HttpResponse): HttpResponse object containing "entity_output"

        where "entity_output" is :
            list of dict: containing dict of detected entities with their original texts for a message
                OR
            list of lists: containing dict of detected entities with their original texts for each message in the list

        EXAMPLES:
        --- Single message
            >>> message = u'i want to order chinese from  mainland china and pizza from domminos'
            >>> entity_name = 'restaurant'
            >>> structured_value = None
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'message',
                    'original_text': 'mainland china',
                    'entity_value': {'value': u'Mainland China'}
                },
                {
                    'detection': 'message',
                    'original_text': 'domminos',
                    'entity_value': {'value': u"Domino's Pizza"}
                }
            ]



            >>> message = u'i wanted to watch movie'
            >>> entity_name = 'movie'
            >>> structured_value = u'inferno'
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'structure_value_verified',
                    'original_text': 'inferno',
                    'entity_value': {'value': u'Inferno'}
                }
            ]

            >>> message = u'i wanted to watch inferno'
            >>> entity_name = 'movie'
            >>> structured_value = u'delhi'
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'message',
                    'original_text': 'inferno',
                    'entity_value': {'value': u'Inferno'}
                }
            ]

        --- Bulk detection
            >>> message = [u'book a flight to mumbai',
                            u'i want to go to delhi from mumbai']
            >>> entity_name = u'city'
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                [
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'mumbai'},
                        'original_text': u'mumbai'
                    }
                ],
                [
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'New Delhi'},
                        'original_text': u'delhi'
                    },
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'mumbai'},
                        'original_text': u'mumbai'
                    }
                ]
            ]
    """
    try:
        parameters_dict = {}
        if request.method == "POST":
            parameters_dict = parse_post_request(request)
            ner_logger.debug('Start Bulk Detection: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        elif request.method == "GET":
            parameters_dict = get_parameters_dictionary(request)
            ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        entity_output = get_text(
            message=parameters_dict[PARAMETER_MESSAGE],
            entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
            structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE],
            fallback_value=parameters_dict[PARAMETER_FALLBACK_VALUE],
            bot_message=parameters_dict[PARAMETER_BOT_MESSAGE],
            language=parameters_dict[PARAMETER_SOURCE_LANGUAGE],
            fuzziness=parameters_dict[PARAMETER_FUZZINESS],
            min_token_len_fuzziness=parameters_dict[PARAMETER_MIN_TOKEN_LEN_FUZZINESS],
            live_crf_model_path=parameters_dict[PARAMETER_LIVE_CRF_MODEL_PATH],
            read_model_from_s3=parameters_dict[PARAMETER_READ_MODEL_FROM_S3],
            read_embeddings_from_remote_url=parameters_dict[PARAMETER_READ_EMBEDDINGS_FROM_REMOTE_URL],
            predetected_values=parameters_dict[PARAMETER_PRIOR_RESULTS]
        )
        ner_logger.debug('Finished %s : %s ' % (parameters_dict[PARAMETER_ENTITY_NAME], entity_output))
    except TypeError as e:
        ner_logger.exception('Exception for text_synonym: %s ' % e)
        return HttpResponse(status=500)
    return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')
Exemple #5
0
def text(request):
    """
    Run text detector with crf model on the 'message or list of messages' passed in the request

    Args:
        request (django.http.HttpRequest): HTTP response from url

    Returns:
        response (django.http.HttpResponse): HttpResponse object containing "entity_output"

        where "entity_output" is :
            list of dict: containing dict of detected entities with their original texts for a message
                OR
            list of lists: containing dict of detected entities with their original texts for each message in the list

        EXAMPLES:
        --- Single message
            >>> message = u'i want to order chinese from  mainland china and pizza from domminos'
            >>> entity_name = 'restaurant'
            >>> structured_value = None
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'message',
                    'original_text': 'mainland china',
                    'entity_value': {'value': u'Mainland China'}
                },
                {
                    'detection': 'message',
                    'original_text': 'domminos',
                    'entity_value': {'value': u"Domino's Pizza"}
                }
            ]



            >>> message = u'i wanted to watch movie'
            >>> entity_name = 'movie'
            >>> structured_value = u'inferno'
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'structure_value_verified',
                    'original_text': 'inferno',
                    'entity_value': {'value': u'Inferno'}
                }
            ]

            >>> message = u'i wanted to watch inferno'
            >>> entity_name = 'movie'
            >>> structured_value = u'delhi'
            >>> fallback_value = None
            >>> bot_message = None
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                {
                    'detection': 'message',
                    'original_text': 'inferno',
                    'entity_value': {'value': u'Inferno'}
                }
            ]

        --- Bulk detection
            >>> message = [u'book a flight to mumbai',
                            u'i want to go to delhi from mumbai']
            >>> entity_name = u'city'
            >>> entity_output = get_text(message=message,
            >>>                   entity_name=entity_name,
            >>>                   structured_value=structured_value,
            >>>                   fallback_value=fallback_value,
            >>>                   bot_message=bot_message)
            >>> print(entity_output)

            [
                [
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'mumbai'},
                        'original_text': u'mumbai'
                    }
                ],
                [
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'New Delhi'},
                        'original_text': u'delhi'
                    },
                    {
                        'detection': 'message',
                        'entity_value': {'value': u'mumbai'},
                        'original_text': u'mumbai'
                    }
                ]
            ]
    """
    try:
        parameters_dict = parse_parameters_from_request(request)
        entity_output = get_text(
            message=parameters_dict[PARAMETER_MESSAGE],
            entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
            structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE],
            fallback_value=parameters_dict[PARAMETER_FALLBACK_VALUE],
            bot_message=parameters_dict[PARAMETER_BOT_MESSAGE],
            language=parameters_dict[PARAMETER_SOURCE_LANGUAGE],
            fuzziness=parameters_dict[PARAMETER_FUZZINESS],
            min_token_len_fuzziness=parameters_dict[PARAMETER_MIN_TOKEN_LEN_FUZZINESS],
            predetected_values=parameters_dict[PARAMETER_PRIOR_RESULTS]
        )
        ner_logger.debug('Finished %s : %s ' % (parameters_dict[PARAMETER_ENTITY_NAME], entity_output))
    except DataStoreRequestException as err:
        ner_logger.exception(f"Error in requesting ES {request.path}, error: {err}, query: {err.request},"
                             f" response: {err.response}")
        return HttpResponse(status=500)
    except es_exceptions.ConnectionTimeout as err:
        ner_logger.exception(f"Error in text_synonym for: {request.path}, error: {err}")
        return HttpResponse(status=500)
    except es_exceptions.ConnectionError as err:
        ner_logger.exception(f"Error in text_synonym for:  {request.path}, error: {err}")
        return HttpResponse(status=500)
    except (TypeError, KeyError) as err:
        ner_logger.exception(f"Error in text_synonym for: {request.path}, error: {err}")
        return HttpResponse(status=500)

    return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')