Пример #1
0
    def test_time_detection_from_csv(self):
        """
        Run time detection tests defined in the csv
        """
        # columns = ['language', 'text', 'bot_message', 'hh', 'mm', 'nn', 'range', 'time_type', 'original_text']
        df = pd.read_csv(self.csv_path,
                         encoding='utf-8',
                         dtype=six.text_type,
                         keep_default_na=False)

        for language, language_tests_df in df.groupby(
                by=[TimeDetectorTest.LANGUAGE]):
            print('Running tests for language {}'.format(language))  # pylint: disable=E1601
            time_detector = TimeDetector(language=language)
            for index, row in language_tests_df.iterrows():
                if not self.is_nan(row[TimeDetectorTest.BOT_MESAGE]):
                    time_detector.set_bot_message(
                        bot_message=row[TimeDetectorTest.BOT_MESAGE])
                range_enabled = not self.is_nan(row[TimeDetectorTest.RANGE])
                expected_times, expected_original_texts = self._make_expected_output(
                    row_dict=row)
                expected_output = list(
                    zip(expected_times, expected_original_texts))
                detected_times, original_texts = time_detector.detect_entity(
                    text=row[TimeDetectorTest.TEXT],
                    range_enabled=range_enabled)

                for detected_output_pair in zip(detected_times,
                                                original_texts):
                    self.assertIn(detected_output_pair, expected_output)
Пример #2
0
        def run_test(self):
            message = testcase["message"]
            range_enabled = testcase.get("range_enabled")
            time_detector = TimeDetector(entity_name='time',
                                         language=language,
                                         timezone=timezone)

            time_dicts, spans = time_detector.detect_entity(
                text=message, range_enabled=range_enabled, **kwargs)

            for time_dict in time_dicts:
                # TODO: This is not correct! actual output should not modified instead expected output should be
                if "range" not in time_dict:
                    time_dict.update({"range": None})
                if "time_type" not in time_dict:
                    time_dict.update({"time_type": None})

            expected_time_dicts, expected_spans = parse_expected_outputs(
                testcase["outputs"])
            expected_outputs = list(
                six.moves.zip(expected_time_dicts, expected_spans))

            prefix = failure_string_prefix.format(message=message,
                                                  language=language)

            self.assertEqual(
                len(time_dicts), len(spans), prefix +
                u"Returned times and original_texts have different lengths")
            self.assertEqual(
                len(spans), len(expected_outputs), prefix +
                u"Returned times and expected_outputs have different lengths")

            for output in six.moves.zip(time_dicts, spans):

                self.assertIn(
                    output, expected_outputs,
                    prefix + u"{got} not in {expected_outputs}".format(
                        got=output, expected_outputs=expected_outputs))
Пример #3
0
def time(request):
    """This functionality use TimeDetector to detect time. 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 = "kal subah 5 baje mujhe jaga dena"
           entity_name = 'time'
           structured_value = None
           fallback_value = None
           bot_message = None
           timezone = 'UTC'
           source_language = 'hi'
           language_script = 'en'
           output = time(request)
           print output

           >>  [{'detection': 'message', 'original_text': '12:30 pm',
                'entity_value': {'mm': 30, 'hh': 12, 'nn': 'pm'}}]
    """
    try:
        parameters_dict = get_parameters_dictionary(request)
        timezone = parameters_dict[PARAMETER_TIMEZONE] or 'UTC'
        form_check = True if parameters_dict[PARAMETER_STRUCTURED_VALUE] else False
        ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
        time_detection = TimeDetector(entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
                                      language=parameters_dict[PARAMETER_SOURCE_LANGUAGE],
                                      timezone=timezone,
                                      form_check=form_check)

        time_detection.set_bot_message(bot_message=parameters_dict[PARAMETER_BOT_MESSAGE])
        entity_output = time_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 time: %s ' % e)
        return HttpResponse(status=500)

    return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')
Пример #4
0
def get_time(text,detected_lang):
    from ner_v2.detectors.temporal.time.time_detection import TimeDetector
    detector = TimeDetector(entity_name='time', language=detected_lang)  # here language will be ISO 639-1 code
    return detector.detect_entity(text)