Source code for dialogy.types.slots

"""
Type definition for Slots. Read :ref:`Intent<Intent>` and :ref:`Slot Filling<Slot Filling>` for details.
"""
from __future__ import annotations

from typing import Any, Dict, List

import attr

from dialogy.types import BaseEntity


[docs]@attr.s class Slot: """ Slot Type .. _Slot: Keys are: - `name` of the slot - `type` is a list of types that can fill this slot - `values` list of entities extracted """ name: str = attr.ib(kw_only=True, order=True) types: List[str] = attr.ib(kw_only=True, factory=list, order=False) values: List[BaseEntity] = attr.ib(kw_only=True, factory=list, order=False)
[docs] def add(self, entity: BaseEntity) -> Slot: """ Insert the `BaseEntity` within the current `Slot` instance. """ self.values.append(entity) return self
[docs] def clear(self) -> Slot: """ Remove all `BaseEntity` within the current `Slot` instance. """ self.values = [] return self
[docs] def json(self) -> Dict[str, Any]: """ Convert the object to a dictionary. Returns: Dict[str, Any] """ self.values = sorted( self.values, key=lambda parse: parse.score or 0, reverse=True, ) return { "name": self.name, "types": self.types, "values": [entity.json() for entity in self.values], }
Rule = Dict[str, Dict[str, Any]]