dream
105 строк · 3.6 Кб
1import logging
2from typing import Dict, List
3
4logger = logging.getLogger(__name__)
5
6LAST_N_TURNS = 5 # number of turns to consider in annotator/skill.
7
8
9def catcher_formatter(dialog: Dict) -> Dict:
10return [{"x": [dialog["utterances"][-1]["text"]]}]
11
12
13def last_utt_dialog(dialog: Dict) -> Dict:
14return [{"sentences": [dialog["utterances"][-1]["text"]]}]
15
16
17def base_response_selector_formatter_service(payload: List) -> Dict:
18if len(payload) == 3:
19return {"skill_name": payload[0], "text": payload[1], "confidence": payload[2]}
20elif len(payload) == 5:
21return {
22"skill_name": payload[0],
23"text": payload[1],
24"confidence": payload[2],
25"human_attributes": payload[3],
26"bot_attributes": payload[4],
27}
28
29
30def full_dialog(dialog: Dict):
31return [{"dialogs": [dialog]}]
32
33
34def base_skill_formatter(payload: Dict) -> Dict:
35return [{"text": payload[0], "confidence": payload[1]}]
36
37
38def simple_formatter_service(payload: List):
39logging.info("answer " + str(payload))
40return payload
41
42
43def entity_linking_formatter(payload: List):
44response = []
45for entity_name, wikidata_ids, id_types in zip(*payload):
46item = {
47"entity_name": entity_name,
48"wikidata_ids": [{"id": id, "instance_of": instance_of} for id, instance_of in zip(wikidata_ids, id_types)],
49}
50response.append(item)
51return response
52
53
54def hypotheses_list(dialog: Dict) -> Dict:
55hypotheses = dialog["utterances"][-1]["hypotheses"]
56hypots = [h["text"] for h in hypotheses]
57return [{"sentences": hypots}]
58
59
60def programy_formatter_dialog(dialog: Dict) -> List:
61return [{"sentences_batch": [[u["text"] for u in dialog["utterances"][-5:]]]}]
62
63
64def skill_with_attributes_formatter_service(payload: Dict):
65"""
66Formatter should use `"state_manager_method": "add_hypothesis"` in config!!!
67Because it returns list of hypothesis even if the payload is returned for one sample!
68
69Args:
70payload: if one sample, list of the following structure:
71(text, confidence, ^human_attributes, ^bot_attributes, attributes) [by ^ marked optional elements]
72if several hypothesis, list of lists of the above structure
73
74Returns:
75list of dictionaries of the following structure:
76{"text": text, "confidence": confidence_value,
77^"human_attributes": {}, ^"bot_attributes": {},
78**attributes},
79by ^ marked optional elements
80"""
81# Used by: skill_with_attributes_formatter, news_skill, meta_script_skill, dummy_skill
82if isinstance(payload[0], list) and isinstance(payload[1], list):
83result = [{"text": hyp[0], "confidence": hyp[1]} for hyp in zip(*payload)]
84else:
85result = [{"text": payload[0], "confidence": payload[1]}]
86
87if len(payload) >= 4:
88if isinstance(payload[2], dict) and isinstance(payload[3], dict):
89result[0]["human_attributes"] = payload[2]
90result[0]["bot_attributes"] = payload[3]
91elif isinstance(payload[2], list) and isinstance(payload[3], list):
92for i, hyp in enumerate(zip(*payload)):
93result[i]["human_attributes"] = hyp[2]
94result[i]["bot_attributes"] = hyp[3]
95
96if len(payload) == 3 or len(payload) == 5:
97if isinstance(payload[-1], dict):
98for key in payload[-1]:
99result[0][key] = payload[-1][key]
100elif isinstance(payload[-1], list):
101for i, hyp in enumerate(zip(*payload)):
102for key in hyp[-1]:
103result[i][key] = hyp[-1][key]
104
105return result
106