-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.php
62 lines (55 loc) · 1.52 KB
/
decoder.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
<?php
class Decoder {
public $query;
public $output_params = array();
public $target;
private $params_dict = array(
"men" => "men",
"women" => "women",
"persons" => array("men", "women"),
"people" => array("men", "women"),
"married" => "married",
"single" => "unmarried",
"unmarried" => "unmarried",
"not married" => "unmarried",
"years old" => "age",
"year old" => "age",
"old" => "age",
"age" => "age"
);
private $target_dict = array(
"how many" => "population",
"how much" => "population",
"the number" => "population"
);
function __construct($__query) {
$this->query = $__query;
$this->decodeParams();
$this->decodeTarget();
}
private function decodeParams() {
$words = explode(" ", $this->query);
foreach ($words as $word) {
if (array_key_exists($word, $this->params_dict)) {
if (is_array($this->params_dict[$word])) {
foreach ($this->params_dict[$word] as $value) {
array_push($this->output_params, $value);
}
} else {
array_push($this->output_params, $this->params_dict[$word]);
}
}
}
}
private function decodeTarget() {
$words = explode(" ", $this->query);
$lastword = "";
foreach ($words as $word) {
if (array_key_exists($lastword . " " . $word, $this->target_dict)) {
$this->target = $this->target_dict[$lastword . " " . $word];
}
$lastword = $word;
}
}
}
?>