szegedai / hun-date-parser

A tool for extracting datetime intervals from Hungarian sentences and turning datetime objects into Hungarian text.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hungarian Date Parser

A tool for extracting datetime intervals from Hungarian sentences and turning datetime objects into Hungarian text.

PyPI version Stars Badge Issues Badge License Badge Coverage Status

Install and try the package with pip install hun-date-parser

🔥 Usage

from hun_date_parser import text2datetime
from datetime import datetime

text2datetime('találkozzunk jövő kedd délután!', now=datetime(2020, 12, 27))
# [{'start_date': datetime.datetime(2020, 12, 29, 12, 0), 'end_date': datetime.datetime(2020, 12, 29, 17, 59, 59)}]

text2datetime('találkozzunk jövő héten szombaton háromnegyed nyolc előtt két perccel', now=datetime(2020, 12, 27))
# [{'start_date': datetime.datetime(2021, 1, 2, 7, 43), 'end_date': datetime.datetime(2021, 1, 2, 7, 43, 59)}]

text2datetime('találkozzunk jövő héten szombaton este háromnegyed nyolc előtt két perccel', now=datetime(2020, 12, 27))
# [{'start_date': datetime.datetime(2021, 1, 2, 19, 43), 'end_date': datetime.datetime(2021, 1, 2, 19, 43, 59)}]

The date parser is also capable of parsing explicit intervals from the text even when only one side of the interval is specified.

from hun_date_parser import text2datetime
from datetime import datetime

text2datetime('2020 decemberétől 2021 januárig', now=datetime(2020, 12, 27))
# [{'start_date': datetime.datetime(2020, 12, 1, 0, 0), 'end_date': datetime.datetime(2021, 1, 31, 23, 59, 59)}]

text2datetime('2021 januárig', now=datetime(2020, 12, 27))
# [{'start_date': None, 'end_date': datetime.datetime(2021, 1, 31, 23, 59, 59)}]

If not specified otherwise, relative dates (eg.: tomorrow, next week, etc.) are calculated relative to the current datetime, at the time when the function is called. The now parameter can be used for parsing relative datetimes relative to any timestamp other than the current time.

Supported formats

Our parser implements a rule-based strategy to interpret a diverse array of date and time formats, utilizing grammatical inflection to parse intervals.

The following formats are currently supported:

Date Formats:

  • ISO Standard Dates: dates formatted to the ISO 8601 standard.
    • Examples: 2020-01-15, 2020-12-30-án, 2020.12.29.
  • Named Months: months indicated by name, optionally with day numbers and/or year. Day numbers may be expressed lexically.
    • Examples: tavaly február, 2020 február 3, jövĹ‘ március, jövĹ‘ február 12-Ă©n, március elsejĂ©n.
  • Relative Time References: relative days, weeks, months, or years.
    • Examples: tegnap, ma, holnap, idĂ©n, tavaly, mĂşlt hĂ©ten, a mĂşlt hĂłnapban, idei esemĂ©nyek.
  • Named Days of the Week: references to specific weekdays, accounting for past, present, and future contexts.
    • Examples: mĂşlt vasárnap, kedden, ezen a heten hĂ©tfĹ‘n, jövĹ‘ hĂ©ten szerdán.
  • Counted Time Frames: expressions indicating a number of days or weeks ago or in the future.
    • Examples: 1 hĂ©ttel ezelĹ‘tt, 6 nappal ezelĹ‘tt, 5 nap mĂşlva.
  • Historical Periods: periods up to the present date, defined by days, weeks, months, or years.
    • Examples: az elĹ‘zĹ‘ kĂ©t hĂ©tben, az elĹ‘zĹ‘ kĂ©t Ă©vi adatok, az elĹ‘zĹ‘ 10 nap eredmĂ©nye.

Time Formats:

  • Digital Clock Format: time expressed in digital clock notation.
    • Examples: 18:12-kor, 06:45.
  • Natural Language Time: time described in conversational terms.
    • Examples: este fĂ©l 8, reggel nyolc elĹ‘tt hat perccel, nyolc Ăłra nyolc perckor, 20 Ăłra 49 perckor, este negyed 8 elĹ‘tt 6 perccel.
  • Abbreviated Time Formats: certain abbreviated forms of time expression.
    • Examples: 16h-kor.

Interval Formats:

  • Inflection-Implied Ranges: intervals detected through grammatical inflections, indicating a start and end point.
    • Examples: február 13-tĂłl 17-ig, keddtĹ‘l pĂ©ntekig, januártĂłl februárig, 2020-10-12-tĹ‘l 2020-11-01-ig.
  • Open-Ended Intervals: expressions where the start or end of the interval is unspecified, using inflections to imply boundaries.
    • Examples: keddtĹ‘l, február elsejĂ©ig.

Setting search scope in case of ambiguous input

For the function text2datetime the parameter search_scope is used to inform what is the desired time interval to parse the inputs.

  • The default value SearchScopes.NOT_RESTRICTED doesn't restrict the scope of the search.
    • i.e.: when Tuesday is parsed the date for the Tuesday on the given week is going to be returned, not considering whether that given date is in the past or the future
  • To prefer future dates in case of ambiguity, use the value SearchScopes.FUTURE_DAY
    • In this case, when Tuesday is parsed, the function will return the closest Tuesday in the future, not necessarily the current week's Tuesday.
  • Similarly to search in the future, nudging the library to prefer past dates is possible with the value SearchScopes.PAST_SEARCH
    • For instance, given a scenario when May is parsed by the function, with this setting, if this year's May is still in the future, last year's May will be returned.
    • Please note, when there's no ambiguity, the function can still return future/past dates, even when a different preference is specified.

An example:

from hun_date_parser import text2datetime
from datetime import datetime
from hun_date_parser.utils import SearchScopes

text2datetime('augusztus', now=datetime(2023, 6, 7), search_scope=SearchScopes.PAST_SEARCH)
# [{'start_date': datetime.datetime(2022, 8, 1, 0, 0),
#   'end_date': datetime.datetime(2022, 8, 31, 23, 59, 59)}]

text2datetime('péntek', now=datetime(2023, 6, 7), search_scope=SearchScopes.PAST_SEARCH)
# [{'start_date': datetime.datetime(2023, 6, 2, 0, 0),
#   'end_date': datetime.datetime(2023, 6, 2, 23, 59, 59)}]

text2datetime('péntek', now=datetime(2023, 6, 7), search_scope=SearchScopes.NOT_RESTRICTED)
# [{'start_date': datetime.datetime(2023, 6, 9, 0, 0),
#   'end_date': datetime.datetime(2023, 6, 9, 23, 59, 59)}]

Datetime to text

The library is also capable of turning datetime objects into their Hungarian text representation.

from hun_date_parser import datetime2text
from datetime import datetime

datetime2text(datetime(2020, 12, 20, 18, 34), now=datetime(2020, 12, 27), time_precision=2)
# {'dates': ['múlt héten vasárnap', '2020 december 20'],
#  'times': ['tizennyolc óra harmincnégy perc', '18:34', 'este hat óra harmincnégy perc', 'este fél 7 után 4 perccel']}

đź“ť License

This project is licensed under MIT license. Feel free to use it in your own projects.

🔧 Contribute

Any help or feedback in further developing the library is welcome!

About

A tool for extracting datetime intervals from Hungarian sentences and turning datetime objects into Hungarian text.

License:MIT License


Languages

Language:Python 99.8%Language:Shell 0.2%