-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_json.php
134 lines (113 loc) · 2.99 KB
/
generate_json.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
<?php
$type = 'gen';
$options = getopt('t::');
if (isset($options['t']) && $options['t'] !== null) {
$type = $options['t'];
if (!$type) {
die('-t option must be provided');
}
}
$_GET['type'] = $type;
const DIR_PREFIX = 'bmpm' . DIRECTORY_SEPARATOR;
$files = glob(DIR_PREFIX . "$type/*.php");
if (!file_exists(DIR_PREFIX . $type)) {
die(sprintf('invalid type "%s"', $type));
}
require_once DIR_PREFIX . "phoneticengine.php";
require_once DIR_PREFIX . "phoneticutils.php";
foreach ($files as $file) {
if (($file === DIR_PREFIX ."$type/languagenames1.php") || ($file === DIR_PREFIX ."$type/languagenames2.php")) {
continue;
}
require_once($file);
}
//$vars = get_defined_vars();
//ksort($vars);
//echo json_encode($vars); die();
/**
* @param array $data
* @return array
*/
function makeRulePatterns(array $data) {
$result = [];
/** @var string[] $patterns */
foreach ($data as $patterns) {
if (count($patterns) < 4) {
continue;
}
$result[] = [
'patterns' => $patterns
];
}
return $result;
}
$finalRules = [
'approx' => [
'first' => makeRulePatterns($approxCommon),
],
'exact' => [
'first' => makeRulePatterns($exactCommon),
],
];
$approxResult = [];
foreach ($approx as $langIndex => $ruleSet) {
$approxResult[] = [
'lang' => $langIndex,
'rules' => makeRulePatterns($ruleSet),
];
}
$finalRules['approx']['second'] = $approxResult;
$exactResult = [];
foreach ($exact as $langIndex => $ruleSet) {
$exactResult[] = [
'lang' => $langIndex,
'rules' => makeRulePatterns($ruleSet),
];
}
$finalRules['exact']['second'] = $exactResult;
$ruleResult = [];
foreach ($rules as $langIndex => $ruleSet) {
$langCode = $languages[$langIndex];
$ruleResult[$langCode] = makeRulePatterns($ruleSet);
}
$languageRulesResult = [];
foreach ($languageRules as $rule) {
$languageRulesResult[] = [
"pattern" => $rule[0],
"langs" => $rule[1],
"accept" => $rule[2],
];
}
$discards = [];
switch ($type) {
case 'ash': {
$discards = [
"ben", "bar", "ha"
];
break;
}
case 'gen': {
$discards = [
"abe", "aben", "abi", "abou", "abu", "al", "bar", "ben", "bou", "bu",
"d", "da", "dal", "de", "del", "dela","della", "des", "di", "dos", "du",
"el", "la", "le", "ibn", "van", "von", "ha", "vanden", "vander"
];
break;
}
case 'sep': {
$discards = [
"abe", "aben", "abi", "abou", "abu", "al", "bar", "ben", "bou", "bu",
"d", "da", "dal", "de","del", "dela","della", "des", "di",
"el", "la", "le", "ibn", "ha"
];
break;
}
}
$data = array(
'languages' => $languages,
'rules' => $ruleResult,
'finalRules' => $finalRules,
'langRules' => $languageRulesResult,
'discards' => $discards,
);
file_put_contents("$type.json", json_encode($data));