dialogy.types.entity.time package

Module contents

Time Entity

Provides the entity class for representing time in natural language. This entity is obtained via DucklingPlugin.

Plugin Walkthrough

In [1]: from dialogy.plugins import DucklingPlugin

In [2]: duckling_plugin = DucklingPlugin(
   ...:     dest="output.entities",
   ...:     dimensions=["time"],
   ...:     locale="en_IN",
   ...:     timezone="Asia/Kolkata",
   ...: )
   ...: 

In [3]: duckling_plugin.parse("tomorrow")
Out[3]: [TimeEntity(body='tomorrow', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value='2022-08-25T00:00:00.000+05:30', entity_type='date', origin='value', dim='time', grain='day')]

In [4]: duckling_plugin.parse("monday")
Out[4]: [TimeEntity(body='monday', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value='2022-08-29T00:00:00.000+05:30', entity_type='date', origin='value', dim='time', grain='day')]

In [5]: duckling_plugin.parse("27th jan")
Out[5]: [TimeEntity(body='27th jan', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value='2023-01-27T00:00:00.000+05:30', entity_type='date', origin='value', dim='time', grain='day')]

Workflow Integration

In [1]: from dialogy.base import Input
   ...: from dialogy.plugins import DucklingPlugin
   ...: from dialogy.workflow import Workflow
   ...: 

In [2]: duckling_plugin = DucklingPlugin(
   ...:     dest="output.entities",
   ...:     dimensions=["time"],
   ...:     locale="en_IN",
   ...:     timezone="Asia/Kolkata",
   ...: )
   ...: 

In [3]: workflow = Workflow([duckling_plugin])

In [4]: _, output = workflow.run(Input(utterances=["tomorrow", "monday", "27th jan"]))

In [5]: output
Out[5]: 
{'intents': [],
 'entities': [{'range': {'start': 0, 'end': 8},
   'body': 'tomorrow',
   'type': 'value',
   'parsers': ['DucklingPlugin'],
   'score': 0.3333333333333333,
   'alternative_index': 0,
   'value': '2022-08-25T00:00:00.000+05:30',
   'entity_type': 'date',
   'grain': 'day'},
  {'range': {'start': 0, 'end': 6},
   'body': 'monday',
   'type': 'value',
   'parsers': ['DucklingPlugin'],
   'score': 0.3333333333333333,
   'alternative_index': 1,
   'value': '2022-08-29T00:00:00.000+05:30',
   'entity_type': 'date',
   'grain': 'day'},
  {'range': {'start': 0, 'end': 8},
   'body': '27th jan',
   'type': 'value',
   'parsers': ['DucklingPlugin'],
   'score': 0.3333333333333333,
   'alternative_index': 2,
   'value': '2023-01-27T00:00:00.000+05:30',
   'entity_type': 'date',
   'grain': 'day'}],
 'original_intent': {}}
class TimeEntity(body, parsers=NOTHING, score=None, alternative_index=None, alternative_indices=None, latent=False, values=NOTHING, value=None, entity_type=None, origin='value', dim='time', grain=None, *, range, type='value')[source]

Bases: dialogy.types.entity.base_entity.BaseEntity

Entities that can be parsed to obtain date, time or datetime values.

Example sentences that contain the entity are: - “I have a flight at 6 am.” - “I have a flight at 6th December.” - “I have a flight at 6 am today.”

Attributes: - grain tells us the smallest unit of time in the utterance

classmethod apply_constraint(date_time, constraint)[source]

Check if date_time is inside constraint (LOWER_BOUND < date_time < UPPER_BOUND) :type date_time: datetime :param date_time: An instance of datetime. :type date_time: datetime :type constraint: Dict[str, Dict[str, int]] :param constraint: {'gte': {'hour': 9, 'minute': 0}, 'lte': {'hour': 20, 'minute': 59}}

Returns

True if date_time > constraint[LOWER_BOUND] and date_time < constraint[UPPER_BOUND]

Return type

bool

collect_datetime_values()[source]

Collect all datetime values from the entity

Returns

List of datetime values

Return type

List[str]

property day: int

Returns the day of the month for the first datetime value.

Returns

Day of the month

Return type

int

dim: str
classmethod from_duckling(d, alternative_index, constraints=None, **kwargs)[source]
Return type

Optional[TimeEntity]

get_value()[source]

Return the date string in ISO format from the dictionary passed

date = {
    "value": "2021-04-17T16:00:00.000+05:30",
    "grain": "hour",
    "type": "value"
}
Parameters

date (Dict[str, str]) – Dictionary which stores the datetime in ISO format, grain and type

Returns

date["value"]

Return type

Optional[datetime]

grain: str
property hour: int

Returns the hour for the first datetime value.

Returns

Hour

Return type

int

is_uniq_date_from_values()[source]

Check a list has a unique day

Where day is the date number for a month.

Returns

True if there is a unique day in all datetime values

Return type

bool

is_uniq_day_from_values()[source]

Check a list has a unique weekday

Where weekday ranges from 0 for Monday till 6 for Sunday Ex: for “2021-04-17T16:00:00.000+05:30”, the weekday is 5 (Saturday)

Duckling may return 3 datetime values in chronological order. For cases where the utterance is “Monday”, (or any weekday for that matter) we will get 3 values, each value a week apart. We pair these dates and check the difference is 1 week long (7 days).

For example:

# Say, our 3 date values are:
date values = [21-04-2021, 28-04-2021, 05-05-2021]

# then we generate pairs as:
[(21-04-2021, 28-04-2021), (28-04-2021, 05-05-2021)]

If the difference between these dates is divisible by seven, then the input was a weekday.

Returns

True if there is a unique weekday in all datetime values

Return type

bool

property minute: int

Returns the minute for the first datetime value.

Returns

Minute

Return type

int

property month: int

Returns the day of the month for the first datetime value.

Returns

Day of the month

Return type

int

origin: str
classmethod pick_value(d_values, grain, constraints)[source]
Return type

List[Dict[str, Any]]

property second: int

Returns the second for the first datetime value.

Returns

Second

Return type

int

set_entity_type()[source]

Returns the type for this entity

To pinpoint a time for any action, we need both DATE (either explicit or inferred) and TIME. We can get these in 3 possible manners:

  • both DATE and TIME, the entity type here will be DATETIME
    • Positive examples: tonight, tomorrow 4pm, 7th april 2019 6pm, last 3 hours (Because it tells us about the date as well, that is today)

    • Negative examples: Yesterday I went to park at 3pm (because the user did not say yesterday and 3pm together)

  • only DATE, the entity type here will be DATE
    • Positive examples: last month, last year, last 3 months, last 6 months, tomorrow, 27th oct 1996, 1st October 2019 till yesterday, yesterday, today, tomorrow, October, 2019, 27th October 1996 to 29th October 1996 etc

    • Negative examples: month, year, 6 months, 3 months (Since these do not have a fixed start and end date, it could have been 6 months from today, 6 months from tomorrow, previous 6 months. Therefore it only tells us a duration.)

  • only TIME, the entity type here will be TIME
    • Positive examples: 3pm, 5pm, night, morning, etc

    • Negative examples: tonight (because the date part today is in it), tomorrow night, 6th April 5pm, 7 hours (duration because they didn’t mention a start/end time. It could be 7 hours from any time) etc

if we get the grain as one of (“day”, “week”, “month”, “quarter”, “year”), then the entity type is DATE if we get grain as one of (“hour”, “second”, “minute”) and we only have 1 value in self.values, then the entity type is DATETIME. if we get grain as one of (“hour”, “second”, “minute”) and we have either a unique date or a unique weekday, then the entity type is DATETIME. else the entity type is TIME.

Returns

one of (date, time, datetime)

Return type

str

property year: int

Returns the day of the month for the first datetime value.

Returns

Day of the month

Return type

int

dt_from_isoformat(iso_datetime)[source]

Converts an ISO-formatted datetime string to a datetime object.

Return type

datetime