-
Notifications
You must be signed in to change notification settings - Fork 4
/
printer.py
74 lines (60 loc) · 2.09 KB
/
printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import typing
from typing import Any, Optional, Text, Dict, List, Type
import numpy as np
from rasa.nlu.components import Component
from rasa.nlu.config import RasaNLUModelConfig
from rasa.nlu.training_data import Message, TrainingData
from rasa.nlu.tokenizers.tokenizer import Token
if typing.TYPE_CHECKING:
from rasa.nlu.model import Metadata
def _is_list_tokens(v):
if isinstance(v, List):
if len(v) > 0:
if isinstance(v[0], Token):
return True
return False
class Printer(Component):
"""A new component"""
@classmethod
def required_components(cls) -> List[Type[Component]]:
"""Specify which components need to be present in the pipeline."""
return []
defaults = {"alias": None}
language_list = None
def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:
super().__init__(component_config)
def train(
self,
training_data: TrainingData,
config: Optional[RasaNLUModelConfig] = None,
**kwargs: Any,
) -> None:
pass
def process(self, message: Message, **kwargs: Any) -> None:
if self.component_config["alias"]:
print("\n")
print(self.component_config["alias"])
print(f"text : {message.text}")
for k, v in message.data.items():
if _is_list_tokens(v):
print(f"{k}: {[t.text for t in v]}")
elif isinstance(v, np.ndarray):
print(f"{k}: Dense array with shape {v.shape}")
else:
print(f"{k}: {v.__repr__()}")
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
pass
@classmethod
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional["Metadata"] = None,
cached_component: Optional["Component"] = None,
**kwargs: Any,
) -> "Component":
"""Load this component from file."""
if cached_component:
return cached_component
else:
return cls(meta)