dialogy.types.entity.duration package¶
Module contents¶
Duration Entity¶
Provides the entity class for durations 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=["duration"],
...: locale="en_IN",
...: timezone="Asia/Kolkata",
...: )
...:
In [3]: duckling_plugin.parse("she said 2h.")
Out[3]: [DurationEntity(body='2h', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value=7200, unit='hour', normalized={'value': 7200, 'unit': 'second'}, _meta={}, entity_type='duration')]
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=["duration"],
...: locale="en_IN",
...: timezone="Asia/Kolkata",
...: )
...:
In [3]: workflow = Workflow([duckling_plugin])
In [4]: _, output = workflow.run(Input(utterances="she said 2h"))
In [5]: output
Out[5]:
{'intents': [],
'entities': [{'range': {'start': 9, 'end': 11},
'body': '2h',
'type': 'value',
'parsers': ['DucklingPlugin'],
'score': 1.0,
'alternative_index': 0,
'value': 7200,
'unit': 'hour',
'normalized': {'value': 7200, 'unit': 'second'},
'_meta': {},
'entity_type': 'duration'}],
'original_intent': {}}
- class DurationEntity(body, dim=None, parsers=NOTHING, score=None, alternative_index=None, alternative_indices=None, latent=False, values=NOTHING, value=None, normalized=NOTHING, meta=NOTHING, *, range, type='value', unit, entity_type='duration')[source]¶
Bases:
dialogy.types.entity.base_entity.BaseEntity
This entity type expects a normalized attribute. This provides the duration normalized to seconds.
Helpful in cases where we wish to operate on time like: “I want a booking in 2 days.”
We can tell the time at which the sentence was said, but we need to make the booking after two days.
This entity parses this information and also provides us the number of seconds to add to the current timestamp to get to a date that’s 2 days ahead.
- as_time(reference_unix_ts, timezone, duration_cast_operator)[source]¶
Converts a duration entity to a time entity.
- Return type
- entity_type: str¶
- classmethod from_duckling(d, alternative_index, reference_time=None, timezone=None, duration_cast_operator=None, **kwargs)[source]¶
- Return type
Union
[DurationEntity
,TimeEntity
]
- normalized: Dict[str, Any]¶
- unit: str¶