dialogy.types.entity.numerical package¶
Module contents¶
Numerical Entity¶
Provides the entity class for representing numbers 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=["number"],
...: locale="en_IN",
...: timezone="Asia/Kolkata",
...: )
...:
In [3]: duckling_plugin.parse("12")
Out[3]: [NumericalEntity(body='12', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value=12, origin='value', entity_type='number')]
In [4]: duckling_plugin.parse("twenty two")
Out[4]: [NumericalEntity(body='twenty two', type='value', parsers=['DucklingPlugin'], score=1.0, alternative_index=0, alternative_indices=[0], latent=False, value=22, origin='value', entity_type='number')]
Workflow Integration¶
In [5]: from dialogy.base import Input
...: from dialogy.plugins import DucklingPlugin
...: from dialogy.workflow import Workflow
...:
In [6]: duckling_plugin = DucklingPlugin(
...: dest="output.entities",
...: dimensions=["number"],
...: locale="en_IN",
...: timezone="Asia/Kolkata",
...: )
...:
In [7]: workflow = Workflow([duckling_plugin])
In [8]: _, output = workflow.run(Input(utterances=["seventy two", "4 fruits"]))
In [9]: output
Out[9]:
{'intents': [],
'entities': [{'range': {'start': 0, 'end': 11},
'body': 'seventy two',
'type': 'value',
'parsers': ['DucklingPlugin'],
'score': 0.5,
'alternative_index': 0,
'value': 72,
'entity_type': 'number'},
{'range': {'start': 0, 'end': 1},
'body': '4',
'type': 'value',
'parsers': ['DucklingPlugin'],
'score': 0.5,
'alternative_index': 1,
'value': 4,
'entity_type': 'number'}],
'original_intent': {}}
CASTING¶
We have noticed that there is a need to cast numerical values to time. The as_time method can be used to do this.
The default assumption is to use the number as day
but we can also replace the month
and hour
.
In [1]: import pytz
...: from datetime import datetime
...: from dialogy.workflow import Workflow
...: from dialogy.base import Input
...: from dialogy.types import Intent
...: from dialogy.plugins import DucklingPlugin
...: from dialogy.utils import dt2timestamp
...:
In [2]: duckling_plugin = DucklingPlugin(
...: dest="output.entities",
...: dimensions=["number"],
...: locale="en_IN",
...: timezone="Asia/Kolkata",
...: )
...:
In [3]: reference_dt = datetime(2022, 1, 1, 12, 0, 0, tzinfo=pytz.timezone("Asia/Kolkata"))
...: reference_time = dt2timestamp(reference_dt)
...:
In [4]: entities = duckling_plugin.parse("12")
In [5]: [entity.as_time(reference_time, "Asia/Kolkata") for entity in entities]
Out[5]: [TimeEntity(body='12', type='value', parsers=[], score=1.0, alternative_index=0, alternative_indices=None, latent=False, value='2022-01-12T11:37:00+05:30', entity_type='date', origin='value', dim='time', grain='day')]
- class NumericalEntity(body, dim=None, parsers=NOTHING, score=None, alternative_index=None, alternative_indices=None, latent=False, values=NOTHING, value=None, origin='value', entity_type='number', *, range, type='value')[source]¶
Bases:
dialogy.types.entity.base_entity.BaseEntity
Numerical Entity Type
Use this type for handling all entities that can be parsed to obtain: - numbers - date - time - datetime
- Attributes:
dim dimension of the entity from duckling parser
type is the type of the entity which can have values in [“value”, “interval”]
- as_time(reference_unix_ts, timezone, replace='day')[source]¶
Converts a duration entity to a time entity.
- Return type
- entity_type: Optional[str]¶