当前位置: 首页>>代码示例>>Python>>正文


Python ExactLanguages.iterate_applicable_languages方法代码示例

本文整理汇总了Python中dateparser.languages.detection.ExactLanguages.iterate_applicable_languages方法的典型用法代码示例。如果您正苦于以下问题:Python ExactLanguages.iterate_applicable_languages方法的具体用法?Python ExactLanguages.iterate_applicable_languages怎么用?Python ExactLanguages.iterate_applicable_languages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dateparser.languages.detection.ExactLanguages的用法示例。


在下文中一共展示了ExactLanguages.iterate_applicable_languages方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestExactLanguages

# 需要导入模块: from dateparser.languages.detection import ExactLanguages [as 别名]
# 或者: from dateparser.languages.detection.ExactLanguages import iterate_applicable_languages [as 别名]
class TestExactLanguages(BaseLanguageDetectorTestCase):
    __test__ = True

    @parameterized.expand([
        param("01-01-12", ['en', 'fr']),
        param("01-01-12", ['tr', 'ar']),
        param("01-01-12", ['ru', 'fr', 'en', 'pl']),
        param("01-01-12", ['en']),
    ])
    def test_exact_languages(self, datetime_string, shortnames):
        self.given_string(datetime_string)
        self.given_known_languages(shortnames)
        self.given_detector()
        self.when_using_exact_languages()
        self.then_exact_languages_were_filtered(shortnames)

    def given_known_languages(self, shortnames):
        self.known_languages = [default_language_loader.get_language(shortname)
                                for shortname in shortnames]

    def given_detector(self):
        self.assertIsInstance(self.known_languages, list, "Require a list of languages to initialize")
        self.assertGreaterEqual(len(self.known_languages), 1, "Could only be initialized with one or more languages")
        self.detector = ExactLanguages(languages=self.known_languages)

    def when_using_exact_languages(self):
        self.exact_languages = self.detector.iterate_applicable_languages(self.datetime_string, modify=True, settings=settings)

    def then_exact_languages_were_filtered(self, shortnames):
        self.assertEqual(set(shortnames), set([lang.shortname for lang in self.exact_languages]))
开发者ID:Sorseg,项目名称:dateparser,代码行数:32,代码来源:test_languages.py

示例2: ExactLanguagesTest

# 需要导入模块: from dateparser.languages.detection import ExactLanguages [as 别名]
# 或者: from dateparser.languages.detection.ExactLanguages import iterate_applicable_languages [as 别名]
class ExactLanguagesTest(BaseTestCase):
    def setUp(self):
        super(ExactLanguagesTest, self).setUp()
        self.parser = NotImplemented
        self.detected_languages = NotImplemented

    def test_languages_passed_in_constructor_should_not_be_none(self):
        self.when_parser_is_constructed(languages=None)
        self.then_error_was_raised(ValueError, ['language cannot be None for ExactLanguages'])

    @parameterized.expand([
        param(languages=['fr'], date_strings=["04-decembre-2015", "13 aou, 2014"]),
    ])
    def test_missing_diacritical_marks(self, languages, date_strings):
        settings.NORMALIZE = True
        self.given_parser(languages)
        self.when_languages_are_detected(date_strings)
        self.then_detected_languages_are(languages)

    @parameterized.expand([
        param(languages=['es'], date_strings=["13 Ago, 2014"]),
        param(languages=['es'], date_strings=["13 Septiembre, 2014"]),
        param(languages=['es'], date_strings=["13/03/2014"]),
        param(languages=['es'], date_strings=["11/03/2014"]),
    ])
    def test_parse_date_in_exact_language(self, languages, date_strings):
        self.given_parser(languages)
        self.when_languages_are_detected(date_strings)
        self.then_detected_languages_are(languages)

    @parameterized.expand([
        param(languages=['es'], date_strings=["13 Setembro, 2014"]),
    ])
    def test_reject_dates_in_other_languages(self, languages, date_strings):
        self.given_parser(languages=languages)
        self.when_languages_are_detected(date_strings)
        self.then_detected_languages_are([])

    def given_parser(self, languages):
        language_map = default_language_loader.get_language_map()
        languages = [language_map[language]
                     for language in languages]
        self.parser = ExactLanguages(languages)

    def when_languages_are_detected(self, date_strings, modify=False):
        assert not isinstance(date_strings, six.string_types)
        for date_string in date_strings:
            detected_languages = list(self.parser.iterate_applicable_languages(date_string, modify=modify, settings=settings))
        self.detected_languages = detected_languages

    def when_parser_is_constructed(self, languages):
        try:
            ExactLanguages(languages)
        except Exception as error:
            self.error = error

    def then_detected_languages_are(self, expected_languages):
        shortnames = map(attrgetter('shortname'), self.detected_languages)
        six.assertCountEqual(self, expected_languages, shortnames)
开发者ID:brechmos,项目名称:dateparser,代码行数:61,代码来源:test_date_parser.py


注:本文中的dateparser.languages.detection.ExactLanguages.iterate_applicable_languages方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。