示例#1
0
 def __init__(self, culture_info: CultureInfo):
     if culture_info is None:
         culture_info = CultureInfo(Culture.French)
     super().__init__(culture_info)
     self._internal_number_extractor = FrenchNumberExtractor(NumberMode.DEFAULT)
     self._internal_number_parser = AgnosticNumberParserFactory.get_parser(ParserType.NUMBER, FrenchNumberParserConfiguration(culture_info))
     self._connector_token = FrenchNumericWithUnit.ConnectorToken
示例#2
0
 def __init__(self, culture_info: CultureInfo):
     if culture_info is None:
         culture_info = CultureInfo(Culture.Portuguese)
     super().__init__(culture_info)
     self._internal_number_extractor = PortugueseNumberExtractor(
         NumberMode.DEFAULT)
     self._internal_number_parser = AgnosticNumberParserFactory.get_parser(
         ParserType.NUMBER, PortugueseNumberParserConfiguration(culture_info))
示例#3
0
 def __init__(self, culture_info: CultureInfo):
     if culture_info is None:
         culture_info = CultureInfo(Culture.Chinese)
     super().__init__(culture_info)
     self._internal_number_extractor = ChineseNumberExtractor(
         ChineseNumberExtractorMode.EXTRACT_ALL)
     self._internal_number_parser = AgnosticNumberParserFactory.get_parser(
         ParserType.NUMBER, ChineseNumberParserConfiguration(culture_info))
示例#4
0
 def __init__(self, culture_info: CultureInfo):
     if culture_info is None:
         culture_info = CultureInfo(Culture.Chinese)
     super().__init__(culture_info)
     self._internal_number_extractor = ChineseNumberExtractor(
         ChineseNumberExtractorMode.EXTRACT_ALL)
     self._internal_number_parser = AgnosticNumberParserFactory.get_parser(
         ParserType.NUMBER, ChineseNumberParserConfiguration(culture_info))
     self.currency_name_to_iso_code_map = ChineseNumericWithUnit.CurrencyNameToIsoCodeMap
     self.currency_fraction_code_list = ChineseNumericWithUnit.FractionalUnitNameToCodeMap
class TestInitializationNumberRecognizer():
    control_model = NumberModel(
        AgnosticNumberParserFactory.get_parser(
            ParserType.NUMBER, EnglishNumberParserConfiguration()),
        EnglishNumberExtractor(NumberMode.PURE_NUMBER))
    english_culture = Culture.English
    spanish_culture = Culture.Spanish
    invalid_culture = "vo-id"

    def assert_models_equal(self, expected, actual):
        assert actual.model_type_name == expected.model_type_name
        assert isinstance(actual.extractor, type(expected.extractor))
        assert isinstance(actual.parser.config, type(expected.parser.config))

    def assert_models_distinct(self, expected, actual):
        assert actual.model_type_name == expected.model_type_name
        assert not isinstance(actual.extractor, type(expected.extractor))
        assert not isinstance(actual.parser.config, type(
            expected.parser.config))

    def test_without_culture_use_target_culture(self):
        recognizer = NumberRecognizer(self.english_culture)
        self.assert_models_equal(self.control_model,
                                 recognizer.get_number_model())

    def test_withOtherCulture_not_use_target_culture(self):
        recognizer = NumberRecognizer(self.english_culture)
        self.assert_models_distinct(
            self.control_model,
            recognizer.get_number_model(self.spanish_culture))

    def test_with_invalid_culture_use_target_culture(self):
        recognizer = NumberRecognizer(self.spanish_culture)
        self.assert_models_equal(
            self.control_model,
            recognizer.get_number_model(self.invalid_culture))

    def test_with_invalid_culture_and_without_fallback_throw_error(self):
        recognizer = NumberRecognizer()
        with pytest.raises(ValueError):
            recognizer.get_number_model(self.invalid_culture, False)

    def test_with_invalid_culture_as_target_and_without_fallback_throw_error(
            self):
        recognizer = NumberRecognizer(self.invalid_culture)
        with pytest.raises(ValueError):
            recognizer.get_number_model(None, False)

    def test_without_target_culture_and_without_culture_fallback_to_english_culture(
            self):
        recognizer = NumberRecognizer()
        self.assert_models_equal(self.control_model,
                                 recognizer.get_number_model())

    def test_initialization_with_int_option_resolve_options_enum(self):
        recognizer = NumberRecognizer(self.english_culture, NumberOptions.NONE,
                                      False)
        assert (recognizer.options & NumberOptions.NONE) == NumberOptions.NONE

    def test_initialization_with_invalid_options_throw_error(self):
        with pytest.raises(ValueError):
            NumberRecognizer(self.invalid_culture, -1)
示例#6
0
    def initialize_configuration(self):
        # region English
        self.register_model(
            'NumberModel', Culture.English, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, EnglishNumberParserConfiguration()),
                EnglishNumberExtractor(NumberMode.PURE_NUMBER)))
        self.register_model(
            'OrdinalModel', Culture.English, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, EnglishNumberParserConfiguration()),
                EnglishOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.English, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, EnglishNumberParserConfiguration()),
                EnglishPercentageExtractor()))
        # endregion

        # region German
        # self.register_model('NumberModel', Culture.German, lambda options: NumberModel(
        #    AgnosticNumberParserFactory.get_parser(
        #        ParserType.NUMBER, GermanNumberParserConfiguration()),
        #    GermanNumberExtractor(NumberMode.PURE_NUMBER)
        # ))
        # self.register_model('OrdinalModel', Culture.German, lambda options: OrdinalModel(
        #    AgnosticNumberParserFactory.get_parser(
        #        ParserType.ORDINAL, GermanNumberParserConfiguration()),
        #    GermanOrdinalExtractor()
        # ))
        # self.register_model('PercentModel', Culture.German, lambda options: PercentModel(
        #    AgnosticNumberParserFactory.get_parser(
        #        ParserType.PERCENTAGE, GermanNumberParserConfiguration()),
        #    GermanPercentageExtractor()
        # ))
        # endregion

        # region Chinese
        self.register_model(
            'NumberModel', Culture.Chinese, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, ChineseNumberParserConfiguration()),
                ChineseNumberExtractor()))
        self.register_model(
            'OrdinalModel', Culture.Chinese, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, ChineseNumberParserConfiguration()),
                ChineseOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.Chinese, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, ChineseNumberParserConfiguration()),
                ChinesePercentageExtractor()))
        # endregion

        # region Japanese
        self.register_model(
            'NumberModel', Culture.Japanese, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, JapaneseNumberParserConfiguration()),
                JapaneseNumberExtractor()))
        self.register_model(
            'OrdinalModel', Culture.Japanese, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, JapaneseNumberParserConfiguration()),
                JapaneseOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.Japanese, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, JapaneseNumberParserConfiguration()
                ), JapanesePercentageExtractor()))
        # endregion

        # region Spanish
        self.register_model(
            'NumberModel', Culture.Spanish, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, SpanishNumberParserConfiguration()),
                SpanishNumberExtractor(NumberMode.PURE_NUMBER)))
        self.register_model(
            'OrdinalModel', Culture.Spanish, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, SpanishNumberParserConfiguration()),
                SpanishOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.Spanish, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, SpanishNumberParserConfiguration()),
                SpanishPercentageExtractor()))
        # endregion

        # region Portuguese
        self.register_model(
            'NumberModel', Culture.Portuguese, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, PortugueseNumberParserConfiguration()),
                PortugueseNumberExtractor(NumberMode.PURE_NUMBER)))
        self.register_model(
            'OrdinalModel', Culture.Portuguese, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, PortugueseNumberParserConfiguration()),
                PortugueseOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.Portuguese, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, PortugueseNumberParserConfiguration(
                    )), PortuguesePercentageExtractor()))
        # endregion

        # region French
        self.register_model(
            'NumberModel', Culture.French, lambda options: NumberModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.NUMBER, FrenchNumberParserConfiguration()),
                FrenchNumberExtractor(NumberMode.PURE_NUMBER)))
        self.register_model(
            'OrdinalModel', Culture.French, lambda options: OrdinalModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.ORDINAL, FrenchNumberParserConfiguration()),
                FrenchOrdinalExtractor()))
        self.register_model(
            'PercentModel', Culture.French, lambda options: PercentModel(
                AgnosticNumberParserFactory.get_parser(
                    ParserType.PERCENTAGE, FrenchNumberParserConfiguration()),
                FrenchPercentageExtractor()))