-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.php
155 lines (142 loc) · 4.9 KB
/
convert.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
<?php
require_once(dirname(__FILE__) . "/utils.php");
$_SESSION['json'] = [];
$_SESSION['msg'] = [];
$invocationName = $_POST['invocationName'] ?? false;
if (!$invocationName) bye("Please specify an invocation name.");
$dir = extractAgent();
$samples = [];
$utterances = file("$dir/SampleUtterances.txt", FILE_IGNORE_NEW_LINES);
foreach ($utterances as $line) {
$values = explode("\t", $line);
$key = $values[0];
$string = $values[1] ?? "";
$string = preg_replace("/(?![{}])\p{P}/u", "", $string);
if (!isset($samples[$key])) $samples[$key] = [];
array_push($samples[$key], $string);
}
$intents = $slotTypes = [];
$schema = json_decode(file_get_contents($dir . "/IntentSchema.json"), true);
if ($schema) {
foreach ($schema['intents'] as $intent) {
$name = $intent['intent'];
$data = [
"name" => $name
];
if (isset($samples[$name])) $data['samples'] = $samples[$name];
$slots = $intent['slots'] ?? false;
if ($slots) {
foreach ($slots as $slot) {
array_push($slotTypes, $slot['type']);
}
$data['slots'] = $slots;
}
array_push($intents, $data);
}
}
$invocationName = strtolower($invocationName);
$json = [
"interactionModel" => [
"languageModel" => [
"invocationName" => $invocationName,
"intents" => $intents
]
]
];
$slots = file_exists("$dir/customSlotTypes");
$types = [];
if ($slots) {
$files = glob("$dir/customSlotTypes/*");
$customs = [];
foreach ($files as $file) {
$name = explode("customSlotTypes/", $file)[1];
$data = file($file, FILE_IGNORE_NEW_LINES);
if (is_array($data)) {
$push = [
'name' => $name,
'data' => $data
];
array_push($customs, $push);
}
}
foreach ($customs as $custom) {
$slotTypes = array_unique($slotTypes);
foreach ($slotTypes as $search) {
$string = strtoupper($search);
if ($custom['name'] == $string) {
$values = [];
$datas = $custom['data'];
foreach ($datas as $data) {
$value = [
"name" => [
"value" => $data
]
];
array_push($values, $value);
}
$push = [
"name" => $search,
"values" => $values
];
array_push($types, $push);
}
}
}
}
if (count($types)) $json['interactionModel']['languageModel']['types'] = $types;
msg("Conversion complete.");
$_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 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 Conversion Tools</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>