-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertV2.php
253 lines (233 loc) · 8.46 KB
/
convertV2.php
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
<?php
$dir = extractAgent();
$_SESSION['msg'] = [];
$_SESSION['json'] = "";
#TODO: GET LANGUAGE FROM POST
$lang = $_POST['language'] ?? "en-US";
msg("Language set to $lang.");
$agent = json_decode(file_get_contents("$dir/agent.json"), true);
$package = json_decode(file_get_contents("$dir/package.json"), true);
$entitiesDir = "$dir/entities";
$intentsDir = "$dir/intents";
$intentFiles = glob("$intentsDir/*");
$entityFiles = glob("$entitiesDir/*");
$invocationName = strtolower($agent['googleAssistant']['invocationName']);
$customs = [];
$slots = [];
$intents = [];
$skipped = [];
$finalSlots = [];
$finalTypes = [];
$toCreate = [];
foreach ($intentFiles as $file) if (!preg_match("/usersays/", $file)) {
$anyName = $data = $params = false;
$intentSlots = [];
$slots = array_values(array_unique($slots));
$data = json_decode(file_get_contents($file), true);
$intentName = $data['name'] ?? false;
$params = $data['responses'][0]['parameters'] ?? false;
if ($params) foreach ($params as $param) {
$type = str_replace("@", "", $param['dataType']);
$paramName = cleanString($param['name']);
$skip = false;
$new = false;
if (preg_match("/sys./", $type)) {
$skip = true;
$replace = [
"sys.any" => "AMAZON.SearchQuery",
"sys.date" => "AMAZON.DATE",
"sys.time" => "AMAZON.TIME",
"sys.duration" => "AMAZON.DURATION",
"sys.language" => "AMAZON.Language",
"sys.number" => "AMAZON.NUMBER",
"sys.ordinal" => "AMAZON.NUMBER",
"sys.percentage" => "AMAZON.NUMBER",
"sys.time-period" => "AMAZON.NUMBER",
"sys.age" => "AMAZON.NUMBER",
"sys.music-artist" => "AMAZON.Artist",
"sys.given-name" => "AMAZON.US_FIRST_NAME"
];
$new = $replace[$type] ?? false;
if ($new) {
if ($new === "AMAZON.SearchQuery") {
msg("String has a searchQuery Slot: $paramName");
$anyName = $paramName;
}
array_push($toCreate, $new);
$skip = false;
$type = $new;
}
}
if (!$skip) {
if (!$new) {
array_push($finalSlots, $type);
$type = cleanString($type);
}
$item = [
'name' => $paramName,
'type' => $type
];
array_push($intentSlots, $item);
} else {
array_push($skipped, $paramName);
msg("Skipping system slot '$type' - " . $paramName);
};
}
$samples = [];
$locale = explode("-",$_SESSION['lang'])[0];
$path = str_replace(".json", "_usersays_$locale.json", $file);
if (!file_exists($path)) msg("Warning, cannot find utterances file - '$path''");
$sayingsFile = json_decode(file_get_contents($path), true);
foreach ($sayingsFile as $saying) {
$aliasCount = 0;
$saying = $saying['data'];
$string = "";
foreach ($saying as $word) {
$alias = $word['alias'] ?? false;
if ($alias) {
$alias = cleanString($alias);
$aliasCount++;
}
$type = $word['meta'] ?? false;
$string .= ($alias ? '{' . $alias . '}' : $word['text']);
if ($type && $alias) {
$json = json_encode(['name' => $alias, 'type' => str_replace("@", "", $type)]);
array_push($slots, $json);
}
}
$string = preg_replace("/(?![{}])\p{P}/u", "", strtolower($string));
$ok = true;
foreach ($skipped as $check) {
if (preg_match('/{' . $check . '}/', $string)) {
msg("Skipping sample '$string' because it contains a removed param '$check'");
$ok = false;
}
}
if ($aliasCount >= 2 && $anyName) {
if (preg_match("/$anyName/",$string)) {
msg("Skipping utterance because it contains a searchQuery (phrase slot) and custom slot and is not allowed. - '$string'");
$ok = false;
}
}
if ($ok) array_push($samples, $string);
}
if ($intentName) {
$intentName = cleanString($intentName);
$intent = [
'name' => $intentName
];
if (count($intentSlots)) $intent['slots'] = $intentSlots;
if (count($samples)) $intent['samples'] = array_values(array_unique($samples));
array_push($intents, $intent);
} else {
msg("ERROR, no fucking name, idiot.");
}
}
$builtIns = [
['name' => "AMAZON.CancelIntent", 'samples' => []],
['name' => "AMAZON.HelpIntent", 'samples' => []],
['name' => "AMAZON.StopIntent", 'samples' => []]
];
$intents = array_merge($builtIns, $intents);
$types = [];
foreach ($entityFiles as $entityFile) {
$data = json_decode(file_get_contents($entityFile), true);
$entityName = $data['name'] ?? false;
$strings = [];
if ($entityName) {
$enter = [];
$locale = explode("-",$_SESSION['lang'])[0];
$path = $entitiesDir . "/$entityName" . "_entries_$locale.json";
$entityName = cleanString($entityName);
if (!file_exists($path)) msg("Warning, cannot find the file named '$path'. This is not necessarily an error.");
$entries = json_decode(file_get_contents($path), true);
foreach ($entries as $entry) {
$value = strtolower($entry['value']);
if (!in_array($value, $strings)) {
array_push($enter, ['name' => $entry]);
array_push($strings, $value);
} else {
msg("Skipping duplicate value '$value' in $entityName");
}
}
$items = [];
$type = [
"name" => $entityName,
"values" => $enter
];
array_push($types, $type);
array_push($finalTypes, $entityName);
}
}
$toCreate = array_values(array_unique($toCreate));
msg("Please add the following builtins before importing : " . json_encode($toCreate));
$json = [
"interactionModel" => [
"languageModel" => [
"invocationName" => $invocationName,
"intents" => $intents,
'types' => $types
]
]
];
$_SESSION['json'] = json_encode($json, JSON_PRETTY_PRINT);
recurseRmdir($randomPath);
function recurseRmdir($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? recurseRmdir("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function cleanString($string) {
$string = str_replace(" ", "",$string);
$string = preg_replace("/(?![{}])\p{P}/u", "", strtolower($string));
return $string;
}
function msg($msg)
{
$msg .= "<BR>";
$array = $_SESSION['msg'] ?? [];
array_push($array, $msg);
$_SESSION['msg'] = $array;
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>DialogFlow to Alexa Converter</title>
<link rel="stylesheet" href="./style.css">
<link href="https://assets.dialogflow.com/common/favicon.png" type="image/png" rel="shortcut icon">
</head>
<body>
<script type='text/javascript'>
function copyJson() {
/* Get the text field */
var copyText = document.getElementById('jsonpre');
var data = copyText.innerHTML;
var dummy = document.createElement('input');
document.body.appendChild(dummy);
dummy.setAttribute('id', 'dummy_id');
data = data.replace(/(\r\n\t|\n|\r\t)/gm,"");
data = data.replace(/\s+/g," ");
document.getElementById('dummy_id').value=data;
dummy.select();
document.execCommand('copy');
document.body.removeChild(dummy);
alert('Data copied to clipboard.');
}
</script>
<div id="header"></div>
<div class='wrapper'>
<div class='prewrap msg'>
<pre class='messages'><?PHP echo join(" ", $_SESSION['msg']);?></pre>
</div>
<div class='prewrap jsonwrap'>
<pre class='json' id='jsonpre'><?PHP echo $_SESSION['json'];?></pre>
<button class='hidden' onclick='copyJson()'>Copy JSON</button>
</div>
</div>
</body>
</html>