Search code examples
pythonregexexpressiontemporal

Temporal Expression recognition in Python


Does anybody know a Python module to easily extract temporal expressions such as 12/01/2011 and Monday, 12/3/1987 or others (in English format)?

Would like to avoid to build a huge set of regex.


Solution

  • In the standard library you won't find something comprehensive, but if you install dateutils, you can do this:

    >>> from dateutils import parser
    >>> parser.parse('January 12, 2012').strftime('%s')
    '1326315600'
    >>> parser.parse('01/12/2012').strftime('%s')
    '1326315600'
    >>> parser.parse('Sunday, 16/09/2012').strftime('%s')
    '1347742800'
    

    Martijn had a point, so here is the normal representation - which is datetime object:

    >>> parser.parse('Sunday, 16/09/2012')
    datetime.datetime(2012, 9, 16, 0, 0)
    >>> parser.parse('01/12/2012')
    datetime.datetime(2012, 1, 12, 0, 0)
    >>> parser.parse('January 12, 2012')
    datetime.datetime(2012, 1, 12, 0, 0)