dialogy.types.entity.time_interval package

Module contents

Time Interval Entity

Provides the entity class for representing time intervals 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("weekend")
Out[3]: [TimeIntervalEntity(body='weekend', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, entity_type='datetime', origin='value', dim='time', grain='hour', type='interval', from_value='2022-08-26T18:00:00.000+05:30', to_value='2022-08-29T00:00:00.000+05:30', values=[{'to': {'value': '2022-08-29T00:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-08-26T18:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}, {'to': {'value': '2022-09-05T00:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-09-02T18:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}, {'to': {'value': '2022-09-12T00:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-09-09T18:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}], value={'from': '2022-08-26T18:00:00.000+05:30', 'to': '2022-08-29T00:00:00.000+05:30'})]

In [4]: duckling_plugin.parse("between 4 to 5pm")
Out[4]: [TimeIntervalEntity(body='between 4 to 5pm', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, entity_type='time', origin='value', dim='time', grain='hour', type='interval', from_value='2022-08-25T16:00:00.000+05:30', to_value='2022-08-25T18:00:00.000+05:30', values=[{'to': {'value': '2022-08-25T18:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-08-25T16:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}, {'to': {'value': '2022-08-26T18:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-08-26T16:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}, {'to': {'value': '2022-08-27T18:00:00.000+05:30', 'grain': 'hour'}, 'from': {'value': '2022-08-27T16:00:00.000+05:30', 'grain': 'hour'}, 'type': 'interval'}], value={'from': '2022-08-25T16:00:00.000+05:30', 'to': '2022-08-25T18:00:00.000+05:30'})]

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=["weekend", "between 4 to 5pm"]))

In [5]: output
Out[5]: 
{'intents': [],
 'entities': [{'range': {'start': 0, 'end': 7},
   'body': 'weekend',
   'parsers': ['DucklingPlugin'],
   'score': 0.5,
   'alternative_index': 0,
   'entity_type': 'datetime',
   'grain': 'hour',
   'type': 'interval',
   'from_value': '2022-08-26T18:00:00.000+05:30',
   'to_value': '2022-08-29T00:00:00.000+05:30',
   'value': {'from': '2022-08-26T18:00:00.000+05:30',
    'to': '2022-08-29T00:00:00.000+05:30'}},
  {'range': {'start': 0, 'end': 16},
   'body': 'between 4 to 5pm',
   'parsers': ['DucklingPlugin'],
   'score': 0.5,
   'alternative_index': 1,
   'entity_type': 'time',
   'grain': 'hour',
   'type': 'interval',
   'from_value': '2022-08-25T16:00:00.000+05:30',
   'to_value': '2022-08-25T18:00:00.000+05:30',
   'value': {'from': '2022-08-25T16:00:00.000+05:30',
    'to': '2022-08-25T18:00:00.000+05:30'}}],
 'original_intent': {}}
class TimeIntervalEntity(body, parsers=NOTHING, score=None, alternative_index=None, alternative_indices=None, latent=False, entity_type=None, origin='value', dim='time', grain=None, type='value', from_value=None, to_value=None, *, range, values=None, value=None)[source]

Bases: dialogy.types.entity.time.TimeEntity

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

  • “I need a flight between 6 am to 10 am.”

  • “I have a flight at 6 am to 5 pm today.”

Attributes: - origin - value is a Dictionary which has either keys ‘from’ and ‘to’ or both

collect_datetime_values()[source]

Collect all datetime values from the entity

Returns

List of datetime values

Return type

List[str]

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

TimeEntity

from_value: Optional[datetime.datetime]
get_value()[source]

Return the date string in ISO format from the dictionary passed

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

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

Returns

date["value"]

Return type

Optional[datetime]

origin: str = 'interval'
classmethod pick_value(d_values, grain, constraints)[source]

Filters interval datetime values outside of timerange constraint

Filter logic apply constraint on both “from” and “to” individually: - both FROM and TO present and inside constraint

  • no filtering

  • both FROM and TO present but only FROM inside constraint
    • filter TO

  • both FROM and TO present but only TO inside constraint
    • replace FROM value with constraint lowerbound datetime value

  • both FROM and TO present but outside constraint
    • filter both TO and FROM

  • only FROM present and inside constraint
    • no filtering

  • only FROM present and outside constraint
    • filter FROM

Return type

List[Dict[str, Any]]

to_value: Optional[datetime.datetime]
type: str
value: Dict[str, Any]
value_keys = {'from', 'to', 'type'}
values: List[Dict[str, Any]]