def test_en_gregorian_year_day_month_format(self):
        """
        Date detection for pattern '2099 21st Nov'
        """
        message = '2099 21st Nov'
        locale = 'en-in'
        # If we run
        day1 = 21
        month = 11
        year1 = 2099

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en', locale=locale)
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        self.assertIn(
            {
                'normal': True,
                'start_range': False,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day1,
                    'mm': month,
                    'yy': year1,
                    'type': 'date'
                }
            }, date_dicts)

        self.assertEqual(original_texts.count(message.lower()), 1)
    def test_en_date_detection_date_ddth_of_mm_of_yy_with_locale(self):
        """
        Date detection for pattern '2/3/19'
        """
        message = '2/3/19'
        locale = 'en-us'
        # If we run
        day1 = 3
        month = 2
        year1 = 2019

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en', locale=locale)
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        self.assertIn(
            {
                'normal': True,
                'start_range': False,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day1,
                    'mm': month,
                    'yy': year1,
                    'type': 'date'
                }
            }, date_dicts)

        self.assertEqual(original_texts.count(message.lower()), 1)
    def test_en_date_detection_day_range_for_nth_week_month(
            self, mocked_get_weekdays_for_month):
        """
        Date detection for pattern 'first week of jan'
        """
        message = 'first week of jan'
        locale = 'en-in'
        day1 = 1
        day2 = 7
        month = 1
        year = self.now_date.year
        if self.now_date.month > month:
            year += 1

        # TODO: functionality is incorrect, when run after 1st week of Jan, detector must return 1st week of next year
        # if (self.now_date.month == month and self.now_date.day > day1):
        #     year += 1

        mocked_get_weekdays_for_month.return_value = [day1, day2]

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en', locale=locale)
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        # TODO: functionality is incorrect, start_range should be True in 1st and end_range should be True in second
        self.assertIn(
            {
                'normal': True,
                'start_range': False,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day1,
                    'mm': month,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)

        self.assertIn(
            {
                'normal': True,
                'start_range': False,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day2,
                    'mm': month,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)
        self.assertEqual(original_texts.count(message.lower()), 2)
    def test_en_date_detection_date_range_ddth_of_mmm_to_ddth(self):
        """
        Date detection for pattern '2nd jan to 5th'
        """
        message = '2nd jan to 5th'
        locale = 'en-in'
        # If we run
        day1 = 2
        day2 = 5
        month = 1
        year1 = self.now_date.year
        year2 = self.now_date.year
        if self.now_date.month > month or (self.now_date.month == month
                                           and self.now_date.day > day1):
            year1 += 1
            year2 += 1

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en', locale=locale)
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        self.assertIn(
            {
                'normal': False,
                'start_range': True,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day1,
                    'mm': month,
                    'yy': year1,
                    'type': 'date'
                }
            }, date_dicts)

        self.assertIn(
            {
                'normal': False,
                'start_range': False,
                'end_range': True,
                'from': False,
                'to': False,
                'value': {
                    'dd': day2,
                    'mm': month,
                    'yy': year2,
                    'type': 'date'
                }
            }, date_dicts)

        self.assertEqual(original_texts.count(message.lower()), 2)
    def test_en_date_detection_date_range_ddth_of_mmm_to_ddth(self):
        """
        Date detection for pattern '2nd jan to 5th'
        """
        message = '2nd jan to 5th'
        day1 = 2
        day2 = 5
        month = 1
        year = self.now_date.year
        if self.now_date.month > 1:
            year += 1

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en')
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        self.assertIn(
            {
                'end_range': False,
                'from': False,
                'normal': False,
                'start_range': True,
                'to': False,
                'value': {
                    'dd': 2,
                    'mm': 1,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)
        self.assertIn(
            {
                'end_range': True,
                'from': False,
                'normal': False,
                'start_range': False,
                'to': False,
                'value': {
                    'dd': day2,
                    'mm': month,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)

        self.assertEqual(original_texts.count(message), 2)
    def test_en_date_detection_day_range_for_nth_week_month(
            self, mocked_get_weekdays_for_month):
        """
        Date detection for pattern 'first week of jan'
        """
        message = 'first week of jan'
        month = 1
        year = self.now_date.year
        if self.now_date.month > month:
            year += 1

        mocked_get_weekdays_for_month.return_value = [1, 2]

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name, language='en')
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)
        self.assertIn(
            {
                'end_range': False,
                'from': False,
                'normal': True,
                'start_range': False,
                'to': False,
                'value': {
                    'dd': 1,
                    'mm': month,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)
        self.assertIn(
            {
                'end_range': False,
                'from': False,
                'normal': True,
                'start_range': False,
                'to': False,
                'value': {
                    'dd': 2,
                    'mm': month,
                    'type': 'date',
                    'yy': year
                }
            }, date_dicts)
        self.assertEqual(original_texts.count(message), 2)
    def test_hi_gregorian_dd_mm_yy_format(self):
        """
        Date detection for pattern '१/३/६६'
        """
        message = u'१/३/६६'
        locale = 'hi-in'
        # If we run
        day1 = 1
        month = 3
        year1 = 1966
        past_date_referenced = True

        date_detector_object = DateAdvancedDetector(
            entity_name=self.entity_name,
            language='hi',
            locale=locale,
            past_date_referenced=past_date_referenced)
        date_dicts, original_texts = date_detector_object.detect_entity(
            message)

        self.assertIn(
            {
                'normal': True,
                'start_range': False,
                'end_range': False,
                'from': False,
                'to': False,
                'value': {
                    'dd': day1,
                    'mm': month,
                    'yy': year1,
                    'type': 'date'
                }
            }, date_dicts)

        self.assertEqual(original_texts.count(message.lower()), 1)
Example #8
0
def date(request):
    """This functionality use DateAdvanceDetector to detect date. It is called through api call

    Args:
        request (django.http.request.HttpRequest): HttpRequest object

        request params:
            message (str): natural text on which detection logic is to be run. Note if structured value is present
                                   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 present
                                   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.
            timezone (str): timezone of the user
            source_language (str): source language code (ISO 639-1)
            language_script (str): language code of script (ISO 639-1)

    Returns:
        response (django.http.response.HttpResponse): HttpResponse object

    Example:

           message = "agle mahine k 5 tarikh ko mera birthday hai"
           entity_name = 'time'
           structured_value = None
           fallback_value = None
           bot_message = None
           timezone = 'UTC'
           source_language = 'hi'
           language_script = 'en'
           output = date(request)
           print output

           >>  [{'detection': 'message', 'original_text': 'agle mahine k 5 tarikh',
                 'entity_value': {'value': {'mm': 12, 'yy': 2018, 'dd': 5, 'type': 'date'}}}]
    """
    try:
        parameters_dict = get_parameters_dictionary(request)
        timezone = parameters_dict[PARAMETER_TIMEZONE] or 'UTC'
        ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        date_past_reference = parameters_dict.get(PARAMETER_PAST_DATE_REFERENCED, "false")
        past_date_referenced = date_past_reference == 'true' or date_past_reference == 'True'
        date_detection = DateAdvancedDetector(entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
                                              language=parameters_dict[PARAMETER_SOURCE_LANGUAGE],
                                              timezone=timezone,
                                              past_date_referenced=past_date_referenced)

        date_detection.set_bot_message(bot_message=parameters_dict[PARAMETER_BOT_MESSAGE])

        entity_output = date_detection.detect(message=parameters_dict[PARAMETER_MESSAGE],
                                              structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE],
                                              fallback_value=parameters_dict[PARAMETER_FALLBACK_VALUE])

        ner_logger.debug('Finished %s : %s ' % (parameters_dict[PARAMETER_ENTITY_NAME], entity_output))
    except TypeError as e:
        ner_logger.exception('Exception for date: %s ' % e)
        return HttpResponse(status=500)

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