dateparser.languages package

Submodules

dateparser.languages.dictionary module

class dateparser.languages.dictionary.Dictionary(locale_info: dict[str, Any], settings: Settings | None = None)[source]

Bases: object

Class that modifies and stores translations and handles splitting of date string.

Parameters:
  • locale_info – Locale info (translation data) of the locale.

  • settings (dict) – Configure customized behavior using settings defined in dateparser.conf.Settings.

Returns:

a Dictionary instance.

are_tokens_valid(tokens: Iterable[str]) → bool[source]

Check if tokens are valid tokens for the locale.

Parameters:

tokens (list) – a list of string tokens.

Returns:

True if tokens are valid, False otherwise.

split(string: str, keep_formatting: bool = False) → list[str][source]

Split the date string using translations in locale info.

Parameters:
  • string (str) – Date string to be splitted.

  • keep_formatting (bool) – If True, retain formatting of the date string.

Returns:

A list of string tokens formed after splitting the date string.

class dateparser.languages.dictionary.NormalizedDictionary(locale_info: dict[str, Any], settings: Settings | None = None)[source]

Bases: Dictionary

exception dateparser.languages.dictionary.UnknownTokenError[source]

Bases: Exception

dateparser.languages.loader module

class dateparser.languages.loader.LocaleDataLoader[source]

Bases: object

Class that handles loading of locale instances.

get_locale(shortname: str) → Locale[source]

Get a locale instance.

Parameters:

shortname (str) – A locale code, e.g. ‘fr-PF’, ‘qu-EC’, ‘af-NA’.

Returns:

locale instance

get_locale_map(languages: Iterable[str] | None = None, locales: Iterable[str] | None = None, region: str | None = None, use_given_order: bool = False, allow_conflicting_locales: bool = False) → OrderedDict[str, Locale][source]

Get an ordered mapping with locale codes as keys and corresponding locale instances as values.

Parameters:
  • languages (list) – A list of language codes, e.g. [‘en’, ‘es’, ‘zh-Hant’]. If locales are not given, languages and region are used to construct locales to load.

  • locales (list) – A list of codes of locales which are to be loaded, e.g. [‘fr-PF’, ‘qu-EC’, ‘af-NA’]

  • region (str) – A region code, e.g. ‘IN’, ‘001’, ‘NE’. If locales are not given, languages and region are used to construct locales to load.

  • use_given_order (bool) – If True, the returned mapping is ordered in the order locales are given.

  • allow_conflicting_locales (bool) – if True, locales with same language and different region can be loaded.

Returns:

ordered locale code to locale instance mapping

get_locales(languages: Iterable[str] | None = None, locales: Iterable[str] | None = None, region: str | None = None, use_given_order: bool = False, allow_conflicting_locales: bool = False) → Iterator[Locale][source]

Yield locale instances.

Parameters:
  • languages (list) – A list of language codes, e.g. [‘en’, ‘es’, ‘zh-Hant’]. If locales are not given, languages and region are used to construct locales to load.

  • locales (list) – A list of codes of locales which are to be loaded, e.g. [‘fr-PF’, ‘qu-EC’, ‘af-NA’]

  • region (str) – A region code, e.g. ‘IN’, ‘001’, ‘NE’. If locales are not given, languages and region are used to construct locales to load.

  • use_given_order (bool) – If True, the returned mapping is ordered in the order locales are given.

  • allow_conflicting_locales (bool) – if True, locales with same language and different region can be loaded.

Yield:

locale instances

dateparser.languages.locale module

class dateparser.languages.locale.Locale(shortname: str, language_info: Mapping[str, Any], _letter_variants: bool = True)[source]

Bases: object

Class that deals with applicability and translation from a locale.

Parameters:
  • shortname (str) – A locale code, e.g. ‘fr-PF’, ‘qu-EC’, ‘af-NA’.

  • language_info (dict) – Language info (translation data) of the language the locale belongs to.

Returns:

A Locale instance

static clean_dictionary(dictionary: dict[str, _T], threshold: int = 2) → dict[str, _T][source]
count_applicability(text: str, strip_timezone: bool = False, settings: Settings | None = None) → list[int][source]
get_wordchars_for_detection(settings: Settings | None) → set[str][source]
is_applicable(date_string: str, strip_timezone: bool = False, settings: Settings | None = None, ignore_surrounding_text: bool = False) → bool[source]

Check if the locale is applicable to translate date string.

Parameters:
  • date_string (str) – A string representing date and/or time in a recognizably valid format.

  • strip_timezone (bool) – If True, timezone is stripped from date string.

  • ignore_surrounding_text (bool) – If True, tokens that the locale does not recognise are ignored at the edges of the date string (IGNORE_SURROUNDING_TEXT setting).

Returns:

boolean value representing if the locale is applicable for the date string or not.

translate(date_string: str, keep_formatting: bool = False, settings: Settings | None = None, ignore_surrounding_text: bool = False) → str[source]

Translate the date string to its English equivalent.

Parameters:
  • date_string (str) – A string representing date and/or time in a recognizably valid format.

  • keep_formatting (bool) – If True, retain formatting of the date string after translation.

  • ignore_surrounding_text (bool) – If True, tokens that the locale does not recognise are dropped from the edges of the date string before translation (IGNORE_SURROUNDING_TEXT setting).

Returns:

translated date string.

dateparser.languages.validation module

class dateparser.languages.validation.LanguageValidator[source]

Bases: object

VALID_KEYS: ClassVar[list[str]] = ['name', 'skip', 'pertain', 'simplifications', 'no_word_spacing', 'ago', 'in', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'early_morning', 'morning', 'afternoon', 'evening', 'night', 'sentence_splitter_group']
classmethod get_logger() → Logger[source]
logger: Logger | None = None
classmethod validate_info(language_id: str, info: Any) → bool[source]

Module contents