本文整理汇总了Python中dateparser.languages.detection.ExactLanguages类的典型用法代码示例。如果您正苦于以下问题:Python ExactLanguages类的具体用法?Python ExactLanguages怎么用?Python ExactLanguages使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExactLanguages类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestExactLanguages
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]))
示例2: ExactLanguagesTest
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)
示例3: test_parse_date_in_exact_language
def test_parse_date_in_exact_language(self):
date_fixtures = [
(u'13 Ago, 2014', datetime(2014, 8, 13)),
(u'13 Septiembre, 2014', datetime(2014, 9, 13)),
(u'13/03/2014', datetime(2014, 3, 13)),
# TODO: make the following test pass
# in this case, it should have detected spanish as the
# language, and so it should use d/m/Y instead of d/m/Y
# (u'11/03/2014', datetime(2014, 3, 11)),
]
spanish = LanguageDataLoader().get_language('es')
parser = ExactLanguages([spanish])
for date_string, correct_date in date_fixtures:
parsed_date = parser.parse(date_string, None)
self.assertEqual(correct_date.date(), parsed_date.date())
with self.assertRaisesRegexp(ValueError, 'Invalid date'):
portuguese_date = u'13 Setembro, 2014'
parser.parse(portuguese_date, None)
示例4: given_detector
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)
示例5: given_parser
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)