This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
forked from avm99963/graf-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistant_callback.php
248 lines (208 loc) · 7.92 KB
/
assistant_callback.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
<?php
require_once("config.php");
class write {
public static function do($json) {
echo json_encode($json)."\n";
exit();
}
}
class conv {
public static function ask($msg_array) {
$items = array();
foreach ($msg_array as $msg) {
$items[] = array("simpleResponse" => array("textToSpeech" => $msg));
}
self::ask_custom($items);
}
public static function ask_followup($msg, &$i18n) {
self::ask([$msg, $i18n->msg("followup_".mt_rand(1, 3))]);
}
public static function ask_custom($items) {
$json = array();
$json["payload"] = array();
$json["payload"]["google"] = array();
$json["payload"]["google"]["expectUserResponse"] = true;
$json["payload"]["google"]["richResponse"] = array();
$json["payload"]["google"]["richResponse"]["items"] = $items;
write::do($json);
}
public static function has($item, &$json) {
foreach ($json["originalDetectIntentRequest"]["payload"]["surface"]["capabilities"] as $cap) {
if ($cap["name"] == $item) {
return true;
}
}
return false;
}
}
class i18n {
public static $hllist = array("en", "es");
public $i18n_strings = null;
public $language = null;
function __construct($lang) {
global $_GET;
global $conf;
if (empty($this->i18n_strings)) {
$this->i18n_strings = array();
}
$this->language = $lang;
$this->i18n_strings = json_decode(file_get_contents("assistant/".$this->language.".json"), true);
return true;
}
function msg($message, $strings = null) {
if (!isset($this->i18n_strings[$message])) {
return false;
}
$string = $this->i18n_strings[$message];
if ($strings != null && is_array($strings)) {
foreach ($strings as $i => $subst) {
$string = str_replace("{".$i."}", $subst, $string);
}
}
return $string;
}
}
function get_graph() {
global $conf;
return json_decode(file_get_contents($conf["apiurl"]), true);
}
function comma($array, $wait=false) {
global $i18n;
if (count($array) == 0) {
return "";
}
if (count($array) == 1) {
return $array[0];
}
$break = ($wait ? '<break time="0.5s"/>' : "");
return implode($break.", ", array_slice($array, 0, -1)).$break.", ".$i18n->msg("and")." ".$array[count($array)-1];
}
$json = json_decode(file_get_contents('php://input'), true);
if ($json === NULL || !isset($json["originalDetectIntentRequest"]) || !isset($json["originalDetectIntentRequest"]["source"]) || $json["originalDetectIntentRequest"]["source"] != "google") {
exit();
}
$graph = get_graph();
$lang = (isset($json["queryResult"]["languageCode"]) && in_array(substr($json["queryResult"]["languageCode"], 0, 2), i18n::$hllist) ? substr($json["queryResult"]["languageCode"], 0, 2) : "en");
$i18n = new i18n($lang);
switch ($json["queryResult"]["intent"]["displayName"]) {
case "showVertex":
if (!isset($json["queryResult"]["parameters"]["Vertex"])) {
exit();
}
$shortest = -1;
$closest = -1;
$closest_id = -1;
foreach ($graph["nodes"] as $id => $node) {
$lev = levenshtein(strtolower($json["queryResult"]["parameters"]["Vertex"]), strtolower($node["name"]));
if ($lev == 0) {
$closest = $node["name"];
$shortest = $lev;
$closest_id = $id;
}
if (($lev <= $shortest || $shortest < 0) && $lev <= 3) {
$closest = $node["name"];
$shortest = $lev;
$closest_id = $id;
}
}
if ($shortest != -1) {
$neighbors = array();
// We're suposing each vertex has a different name. If not, this would get rid of some of the edges, but if two vertexs had the same name there would be no way to differentiate them anyway, so I think it's ok.
foreach ($graph["edges"] as $edge) {
if ($edge["a"] == $closest_id) {
$neighbors[$graph["nodes"][$edge["b"]]["name"]] = $edge["votes"];
} elseif ($edge["b"] == $closest_id) {
$neighbors[$graph["nodes"][$edge["a"]]["name"]] = $edge["votes"];
}
}
if (count($neighbors) == 0) {
conv::ask_followup($i18n->msg("new_to_graph", array("person" => $closest)), $i18n);
} else {
arsort($neighbors);
$params = array("person" => $closest, "count" => (count($neighbors) == 1 ? $i18n->msg("count_singular") : $i18n->msg("count_plural", array("count" => count($neighbors)))));
/*if (conv::has("actions.capability.SCREEN_OUTPUT", $json)) {
$items = [
array(
"simpleResponse" => array(
"textToSpeech" => $i18n->msg("edges_display", $params)
)
),
array(
"tableCard" => array(
"columnProperties" => [
array(
"header" => "Adjacent vertex"
),
array("header" => "Votes")
],
"rows" => []
)
),
array(
"simpleResponse" => array(
"textToSpeech" => $i18n->msg("followup_".mt_rand(1, 3))
)
)
];
foreach ($neighbors as $neighbor => $votes) {
$items[1]["tableCard"]["rows"][] = array("cells" => [array("text" => $neighbor), array("text" => (string)$votes)]);
}
conv::ask_custom($items);
} else {*/ // This code shows a table if the user has a screen, but
// unfortunately tables are not public yet.
// @TODO: Uncomment when tables are out of developer preview.
$people = array_keys($neighbors);
$people_string = comma($people);
$params["edges"] = $people_string;
$num = mt_rand(1, 3);
conv::ask_followup($i18n->msg("edges_".$num, $params), $i18n);
/*}*/
}
} else {
conv::ask_followup($i18n->msg("not_found", array("person" => $json["queryResult"]["parameters"]["Vertex"])), $i18n);
}
break;
case "randomFact":
$rand = mt_rand(0, 5);
switch ($rand) {
case 0: // Last edge in the graph
$last = array_values(array_slice($graph["edges"], -1))[0];
conv::ask_followup($i18n->msg("random_fact", array("fact" => $i18n->msg("didyouknow_last_edge", array("person1" => $graph["nodes"][$last["a"]]["name"], "person2" => $graph["nodes"][$last["b"]]["name"])))), $i18n);
break;
case 1: // Num vertices
conv::ask_followup($i18n->msg("didyouknow_num_vertices", array("count" => count($graph["nodes"]))), $i18n);
break;
case 2: // Num edges
conv::ask_followup($i18n->msg("didyouknow_num_edges", array("count" => count($graph["edges"]))), $i18n);
break;
case 3: // First 3 vertices K3
conv::ask_followup($i18n->msg("random_fact", array("fact" => $i18n->msg("didyouknow_k3"))), $i18n);
break;
case 4: // Creator
conv::ask_followup($i18n->msg("didyouknow_creator"), $i18n);
break;
case 5: // Groph
conv::ask_followup($i18n->msg("random_fact", array("fact" => $i18n->msg("didyouknow_groph"))), $i18n);
break;
}
break;
case "numVertexs":
conv::ask_followup($i18n->msg("num_vertices", array("count" => count($graph["nodes"]))), $i18n);
break;
case "numEdges":
conv::ask_followup($i18n->msg("num_edges", array("count" => count($graph["edges"]))), $i18n);
break;
case "latestNews":
$param = (isset($json["queryResult"]["parameters"]["numEdges"]) ? $json["queryResult"]["parameters"]["numEdges"] : null);
$num = (isset($param) && !empty($param) ? (int)$param : 4);
$last = array_values(array_slice($graph["edges"], -$num));
$edges = [];
foreach ($last as $edge) {
$edges[] = $graph["nodes"][$edge["a"]]["name"]." - ".$graph["nodes"][$edge["b"]]["name"];
}
$edges_string = comma($edges, true);
conv::ask_followup($i18n->msg((isset($param) && !empty($param) ? "latest_news_count" : "latest_news"), array("edges" => $edges_string, "count" => $num)), $i18n);
break;
default:
exit();
}