def __init__(self,
                 entity_name,
                 source_language_script=ENGLISH_LANG,
                 translation_enabled=False):
        """Initializes a PassengerDetector object

        Args:
            entity_name: A string by which the detected passenger count would be replaced with on calling
                        detect_entity()
            source_language_script: ISO 639 code for language of entities to be detected by the instance of this class
            translation_enabled: True if messages needs to be translated in case detector does not support a
                                 particular language, else False
        """
        # assigning values to superclass attributes
        self._supported_languages = [ENGLISH_LANG]
        super(PassengerDetector, self).__init__(source_language_script,
                                                translation_enabled)
        self.text = ''
        self.entity_name = entity_name
        self.tagged_text = ''
        self.processed_text = ''
        self.passenger = []
        self.original_passenger_text = []
        self.tag = '__' + self.entity_name + '__'
        self.bot_message = None
        self.number_detection = NumberDetector('numeric_range')
        self.number_detection.set_min_max_digits(min_digit=1, max_digit=2)
Пример #2
0
def get_number(message, entity_name, structured_value, fallback_value,
               bot_message):
    """This functionality calls the NumberDetector class to detect numerals

    Attributes:
        NOTE: Explained above

    Output:
        NOTE: Explained above

    For Example:

        message = "I want to purchase 30 units of mobile and 40 units of Television"
        entity_name = 'number_of_unit'
        structured_value = None
        fallback_value = None
        bot_message = None
        output = get_number(message=message, entity_name=entity_name, structured_value=structured_value,
                          fallback_value=fallback_value, bot_message=bot_message)
        print output

            >> [{'detection': 'message', 'original_text': '30', 'entity_value': {'value': '30'}},
            {'detection': 'message', 'original_text': '40', 'entity_value': {'value': '40'}}]


        message = "I want to reserve a table for 3 people"
        entity_name = 'number_of_people'
        structured_value = None
        fallback_value = None
        bot_message = None
        output = get_number(message=message, entity_name=entity_name, structured_value=structured_value,
                          fallback_value=fallback_value, bot_message=bot_message)
        print output

            >> [{'detection': 'message', 'original_text': 'for 3 people', 'entity_value': {'value': '3'}}]

    """

    number_detection = NumberDetector(entity_name=entity_name)

    if structured_value:
        entity_list, original_text_list = number_detection.detect_entity(
            text=structured_value)
        if entity_list:
            return output_entity_dict_list(entity_list, original_text_list,
                                           FROM_STRUCTURE_VALUE_VERIFIED)
        else:
            return output_entity_dict_value(structured_value, structured_value,
                                            FROM_STRUCTURE_VALUE_NOT_VERIFIED)
    else:
        entity_list, original_text_list = number_detection.detect_entity(
            text=message)
        if entity_list:
            return output_entity_dict_list(entity_list, original_text_list,
                                           FROM_MESSAGE)
        elif fallback_value:
            return output_entity_dict_value(fallback_value, fallback_value,
                                            FROM_FALLBACK_VALUE)

    return None
Пример #3
0
def get_number(message, entity_name, structured_value, fallback_value, bot_message, min_digit=None, max_digit=None):
    """Use NumberDetector to detect numerals

    Args:
        message (str): natural text on which detection logic is to be run. Note if structured value is
                                detection is run on structured value instead of message
        entity_name (str): name of the entity. Also acts as elastic-search dictionary name
                           if entity uses elastic-search lookup
        structured_value (str): Value obtained from any structured elements. Note if structured value is
                                detection is run on structured value instead of message
                                (For example, UI elements like form, payload, etc)
        fallback_value (str): If the detection logic fails to detect any value either from structured_value
                          or message then we return a fallback_value as an output.
        bot_message (str): previous message from a bot/agent.
        min_digit (str): min digit
        max_digit (str): max digit


    Returns:
        dict or None: dictionary containing entity_value, original_text and detection;
                      entity_value is in itself a dict with its keys varying from entity to entity

    Example:

        message = "I want to purchase 30 units of mobile and 40 units of Television"
        entity_name = 'number_of_unit'
        structured_value = None
        fallback_value = None
        bot_message = None
        output = get_number(message=message, entity_name=entity_name, structured_value=structured_value,
                          fallback_value=fallback_value, bot_message=bot_message)
        print output

            >> [{'detection': 'message', 'original_text': '30', 'entity_value': {'value': '30'}},
            {'detection': 'message', 'original_text': '40', 'entity_value': {'value': '40'}}]


        message = "I want to reserve a table for 3 people"
        entity_name = 'number_of_people'
        structured_value = None
        fallback_value = None
        bot_message = None
        output = get_number(message=message, entity_name=entity_name, structured_value=structured_value,
                          fallback_value=fallback_value, bot_message=bot_message)
        print output

            >> [{'detection': 'message', 'original_text': 'for 3 people', 'entity_value': {'value': '3'}}]

    """

    number_detection = NumberDetector(entity_name=entity_name)
    if min_digit and max_digit:
        min_digit = int(min_digit)
        max_digit = int(max_digit)
        number_detection.set_min_max_digits(min_digit=min_digit, max_digit=max_digit)
    return number_detection.detect(message=message, structured_value=structured_value, fallback_value=fallback_value,
                                   bot_message=bot_message)