semantic.dates

Used to extract dates and times described as text.

class semantic.dates.DateService(tz=None, now=None)

Initialize a DateService for extracting dates from text.

Args:
tz: An optional Pytz timezone. All datetime objects returned will
be relative to the supplied timezone, or timezone-less if none is supplied.
now: The time to which all returned datetime objects should be
relative. For example, if the text is “In 5 hours”, the datetime returned will be now + datetime.timedelta(hours=5). Uses datetime.datetime.now() if none is supplied.
Returns:
A DateService which uses tz and now for all of its computations.
convertDate(date, prefix='', weekday=False)

Convert a datetime object representing into a human-ready string that can be read, spoken aloud, etc. In effect, runs both convertDay and convertTime on the input, merging the results.

Args:

date (datetime.date): A datetime object to be converted into text. prefix (str): An optional argument that prefixes the converted

string. For example, if prefix=”in”, you’d receive “in two days”, rather than “two days”, while the method would still return “tomorrow” (rather than “in tomorrow”).
weekday (bool): An optional argument that returns “Monday, Oct. 1”
if True, rather than “Oct. 1”.
Returns:
A string representation of the input day and time.
convertDay(day, prefix='', weekday=False)

Convert a datetime object representing a day into a human-ready string that can be read, spoken aloud, etc.

Args:

day (datetime.date): A datetime object to be converted into text. prefix (str): An optional argument that prefixes the converted

string. For example, if prefix=”in”, you’d receive “in two days”, rather than “two days”, while the method would still return “tomorrow” (rather than “in tomorrow”).
weekday (bool): An optional argument that returns “Monday, Oct. 1”
if True, rather than “Oct. 1”.
Returns:
A string representation of the input day, ignoring any time-related information.
convertTime(time)

Convert a datetime object representing a time into a human-ready string that can be read, spoken aloud, etc.

Args:
time (datetime.date): A datetime object to be converted into text.
Returns:
A string representation of the input time, ignoring any day-related information.
extractDate(input)

Returns the first date found in the input string, or None if not found.

extractDates(input)

Extract semantic date information from an input string. In effect, runs both parseDay and parseTime on the input string and merges the results to produce a comprehensive datetime object.

Args:
input (str): Input string to be parsed.
Returns:
A list of datetime objects containing the extracted dates from the input snippet, or an empty list if not found.
extractDay(input)

Returns the first time-related date found in the input string, or None if not found.

extractDays(input)

Extracts all day-related information from an input string. Ignores any information related to the specific time-of-day.

Args:
input (str): Input string to be parsed.
Returns:
A list of datetime objects containing the extracted date from the input snippet, or an empty list if none found.
extractTime(input)

Returns the first time-related date found in the input string, or None if not found.

extractTimes(input)

Extracts time-related information from an input string. Ignores any information related to the specific date, focusing on the time-of-day.

Args:
input (str): Input string to be parsed.
Returns:
A list of datetime objects containing the extracted times from the input snippet, or an empty list if none found.
semantic.dates.extractDates(input, tz=None, now=None)

Extract semantic date information from an input string. This is a convenience method which would only be used if you’d rather not initialize a DateService object.

Args:

input (str): The input string to be parsed. tz: An optional Pytz timezone. All datetime objects returned will

be relative to the supplied timezone, or timezone-less if none is supplied.
now: The time to which all returned datetime objects should be
relative. For example, if the text is “In 5 hours”, the datetime returned will be now + datetime.timedelta(hours=5). Uses datetime.datetime.now() if none is supplied.
Returns:
A list of datetime objects extracted from input.

Previous topic

Documentation

Next topic

semantic.numbers

This Page