-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
converters.py
299 lines (249 loc) 路 10.2 KB
/
converters.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io
import json
import logging
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Text
from rasa_nlu import utils
from rasa_nlu.tokenizers import Tokenizer
from rasa_nlu.training_data import TrainingData, Message
logger = logging.getLogger(__name__)
# Different supported file formats and their identifier
WIT_FILE_FORMAT = "wit"
API_FILE_FORMAT = "api"
LUIS_FILE_FORMAT = "luis"
RASA_FILE_FORMAT = "rasa_nlu"
UNK_FILE_FORMAT = "unk"
def load_api_data(files):
# type: (List[Text]) -> TrainingData
"""Loads training data stored in the API.ai data format."""
training_examples = []
entity_synonyms = {}
for filename in files:
with io.open(filename, encoding="utf-8-sig") as f:
data = json.loads(f.read())
# get only intents, skip the rest. The property name is the target class
if "userSays" in data:
intent = data.get("name")
for s in data["userSays"]:
text = "".join([chunk["text"] for chunk in s.get("data")])
# add entities to each token, if available
entities = []
for e in [chunk for chunk in s.get("data") if "alias" in chunk or "meta" in chunk]:
start = text.find(e["text"])
end = start + len(e["text"])
val = text[start:end]
entities.append(
{
"entity": e["alias"] if "alias" in e else e["meta"],
"value": val,
"start": start,
"end": end
}
)
data = {}
if intent:
data["intent"] = intent
if entities is not None:
data["entities"] = entities
training_examples.append(Message(text, data))
# create synonyms dictionary
if "name" in data and "entries" in data:
for entry in data["entries"]:
if "value" in entry and "synonyms" in entry:
for synonym in entry["synonyms"]:
entity_synonyms[synonym] = entry["value"]
return TrainingData(training_examples, entity_synonyms)
def load_luis_data(filename):
# type: (Text) -> TrainingData
"""Loads training data stored in the LUIS.ai data format."""
training_examples = []
regex_features = []
with io.open(filename, encoding="utf-8-sig") as f:
data = json.loads(f.read())
# Simple check to ensure we support this luis data schema version
if not data["luis_schema_version"].startswith("2"):
raise Exception("Invalid luis data schema version {}, should be 2.x.x. ".format(data["luis_schema_version"]) +
"Make sure to use the latest luis version (e.g. by downloading your data again).")
for s in data["utterances"]:
text = s.get("text")
intent = s.get("intent")
entities = []
for e in s.get("entities") or []:
start, end = e["startPos"], e["endPos"] + 1
val = text[start:end]
entities.append({"entity": e["entity"], "value": val, "start": start, "end": end})
data = {"entities": entities}
if intent:
data["intent"] = intent
training_examples.append(Message(text, data))
return TrainingData(training_examples)
def load_wit_data(filename):
# type: (Text) -> TrainingData
"""Loads training data stored in the WIT.ai data format."""
training_examples = []
with io.open(filename, encoding="utf-8-sig") as f:
data = json.loads(f.read())
for s in data["data"]:
entities = s.get("entities")
if entities is None:
continue
text = s.get("text")
intents = [e["value"] for e in entities if e["entity"] == 'intent']
intent = intents[0].strip("\"") if intents else None
entities = [e for e in entities if ("start" in e and "end" in e and e["entity"] != 'intent')]
for e in entities:
e["value"] = e["value"].strip("\"") # for some reason wit adds additional quotes around entity values
data = {}
if intent:
data["intent"] = intent
if entities is not None:
data["entities"] = entities
training_examples.append(Message(text, data))
return TrainingData(training_examples)
def rasa_nlu_data_schema():
training_example_schema = {
"type": "object",
"properties": {
"text": {"type": "string"},
"intent": {"type": "string"},
"entities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"start": {"type": "number"},
"end": {"type": "number"},
"value": {"type": "string"},
"entity": {"type": "string"}
},
"required": ["start", "end", "entity"]
}
}
},
"required": ["text"]
}
regex_feature_schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"intent": {"type": "string"},
"entity": {"type": "string"},
"pattern": {"type": "string"},
}
}
return {
"type": "object",
"properties": {
"rasa_nlu_data": {
"type": "object",
"properties": {
"regex_features": {
"type": "array",
"items": regex_feature_schema
},
"common_examples": {
"type": "array",
"items": training_example_schema
},
"intent_examples": {
"type": "array",
"items": training_example_schema
},
"entity_examples": {
"type": "array",
"items": training_example_schema
}
}
}
},
"additionalProperties": False
}
def validate_rasa_nlu_data(data):
# type: (Dict[Text, Any]) -> None
"""Validate rasa training data format to ensure proper training. Raises exception on failure."""
from jsonschema import validate
from jsonschema import ValidationError
try:
validate(data, rasa_nlu_data_schema())
except ValidationError as e:
e.message += \
". Failed to validate training data, make sure your data is valid. " + \
"For more information about the format visit " + \
"https://rasa-nlu.readthedocs.io/en/latest/dataformat.html"
raise e
def load_rasa_data(filename):
# type: (Text) -> TrainingData
"""Loads training data stored in the rasa NLU data format."""
with io.open(filename, encoding="utf-8-sig") as f:
data = json.loads(f.read())
validate_rasa_nlu_data(data)
common = data['rasa_nlu_data'].get("common_examples", list())
intent = data['rasa_nlu_data'].get("intent_examples", list())
entity = data['rasa_nlu_data'].get("entity_examples", list())
regex_features = data['rasa_nlu_data'].get("regex_features", list())
synonyms = data['rasa_nlu_data'].get("entity_synonyms", list())
# build entity_synonyms dictionary
entity_synonyms = {}
for s in synonyms:
if "value" in s and "synonyms" in s:
for synonym in s["synonyms"]:
entity_synonyms[synonym] = s["value"]
if intent or entity:
logger.warn("DEPRECATION warning: Data file contains 'intent_examples' or 'entity_examples' which will be " +
"removed in the future. Consider putting all your examples into the 'common_examples' section.")
all_examples = common + intent + entity
training_examples = []
for e in all_examples:
data = {}
if e.get("intent"):
data["intent"] = e["intent"]
if e.get("entities") is not None:
data["entities"] = e["entities"]
training_examples.append(Message(e["text"], data))
return TrainingData(training_examples, entity_synonyms, regex_features)
def guess_format(files):
# type: (List[Text]) -> Text
"""Given a set of files, tries to guess which data format is used."""
for filename in files:
with io.open(filename, encoding="utf-8-sig") as f:
file_data = json.loads(f.read())
if "data" in file_data and type(file_data.get("data")) is list:
return WIT_FILE_FORMAT
elif "luis_schema_version" in file_data:
return LUIS_FILE_FORMAT
elif "userSays" in file_data:
return API_FILE_FORMAT
elif "rasa_nlu_data" in file_data:
return RASA_FILE_FORMAT
return UNK_FILE_FORMAT
def resolve_data_files(resource_name):
# type: (Text) -> List[Text]
"""Lists all data files of the resource name (might be a file or directory)."""
try:
return utils.recursively_find_files(resource_name)
except ValueError as e:
raise ValueError("Invalid training data file / folder specified. {}".format(e))
def load_data(resource_name, fformat=None):
# type: (Text, Optional[Text]) -> TrainingData
"""Loads training data from disk. If no format is provided, the format will be guessed based on the files."""
files = resolve_data_files(resource_name)
if not fformat:
fformat = guess_format(files)
logger.info("Training data format at {} is {}".format(resource_name, fformat))
if fformat == LUIS_FILE_FORMAT:
return load_luis_data(files[0])
elif fformat == WIT_FILE_FORMAT:
return load_wit_data(files[0])
elif fformat == API_FILE_FORMAT:
return load_api_data(files)
elif fformat == RASA_FILE_FORMAT:
return load_rasa_data(files[0])
else:
raise ValueError("unknown training file format : {} for file {}".format(fformat, resource_name))