-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathredis-indexer.php
166 lines (121 loc) · 4.16 KB
/
redis-indexer.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
<?php
// mengindeks korpus, versi flat file
// profiling
$time_start = microtime(true);
include 'lib/trigram.php';
include 'lib/predis/autoload.php';
Predis\Autoloader::register();
$redis = new Predis\Client();
if ($argc == 1) { echo 'Tambahkan argumen "V" atau "NV"' . "\n"; exit(1); }
if ($argv[1] == "V")
$bervokal = true;
else if ($argv[1] == "NV")
$bervokal = false;
else {
echo 'Tambahkan argumen "V" atau "NV"' . "\n";
exit(1);
}
if ($bervokal) {
$doc_file = "index/fonetik_vokal.txt";
$key_prefix = "vocal-";
} else {
$doc_file = "index/fonetik.txt";
$key_prefix = "nonvocal-";
}
// tahap I : mengekstrak seluruh term dari seluruh dokumen dan membangun indeks
echo "Tahap I ... ";
// baca seluruh dokumen
$docs = file($doc_file);
$docs_count = count($docs);
// array besar penyimpan indeks
$index = array();
$limit = 8000;
$i = 1;
// untuk setiap dokumen
foreach ($docs as $doc) {
// dipecah pada karakter |
list($id, $text) = explode("|", $doc);
//echo "Memproses dokumen $id : ";
// ekstrak trigram
$trigrams = trigram_frekuensi_posisi_all($text);
/*
* Resolving ambiguitas (https://github.com/lafzi/lafzi-web/issues/1)
* Jika ada trigram "AXI" atau "AXU", maka tambahkan trigram baru:
* - AXI ==> AY + karakter setelah I
* - AXU ==> AW + karakter setelah U
*
* Begitu juga sebelum dan sesudahnya.
* Misalnya "BIMAXUNZILA" akan ada trigram baru: MAW, AWN, WNZ
* Dokumen tidak dimofidikasi. Cuma relevan buat yg bervokal.
*/
$new_trigrams = array();
foreach(array("AXI" => "AY", "AXU" => "AW") as $amb => $rep) {
if (isset($trigrams[$amb])) {
$poss = $trigrams[$amb][1];
foreach ($poss as $p) {
$nextchar = $text[$p + 2];
$trigram = $rep . $nextchar;
if (!isset($new_trigrams[$trigram]))
$new_trigrams[$trigram] = array($p);
else
$new_trigrams[$trigram][] = $p;
// setelah setelahnya
if (isset($text[$p + 3]) && $text[$p + 3] != "\n") { // tidak dijamin ada
$nextchar2 = $text[$p + 3];
$trigram = $rep[1] . $nextchar . $nextchar2;
if (!isset($new_trigrams[$trigram]))
$new_trigrams[$trigram] = array($p + 2);
else
$new_trigrams[$trigram][] = $p + 2;
}
// sebelum yang ambigu
$prevchar = $text[$p - 2];
$trigram = $prevchar . $rep;
if (!isset($new_trigrams[$trigram]))
$new_trigrams[$trigram] = array($p - 1);
else
$new_trigrams[$trigram][] = $p - 1;
}
}
}
foreach($new_trigrams as $trigram => $poss) {
if (!isset($trigrams[$trigram]))
$trigrams[$trigram] = array(count($poss), $poss); // freq, pos
else {
// kalau sudah ada
$trigrams[$trigram][0] += count($poss); // freq ditambah
$trigrams[$trigram][1] = array_merge($trigrams[$trigram][1], $poss); // pos di-merge
}
}
foreach ($trigrams as $trigram => $fp) {
// $fp[0] = frekuensi, $fp[1] = posisi trigram
list($freq, $pos) = $fp;
// masukkan entri ke array indeks
$index[$trigram][] = array($id, $freq, $pos);
}
//echo "OK\n";
//echo "(". round($id/$docs_count*100) ."%)";
//echo "\n";
if ($i >= $limit) break;
$i++;
}
echo "OK\n";
unset($docs);
// tahap II : menulis inverted index
echo "Tahap II ... ";
// urutkan key pada array indeks
ksort($index);
// untuk setiap term pada indeks
foreach ($index as $term => $postings) {
// set ke Redis
$key = $key_prefix.$term;
$redis->set($key, json_encode($postings));
}
// selesai, hapus index di memory
unset($index);
echo "OK\n";
// hasil profiling waktu eksekusi
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "\nTerindeks dalam $time detik\n";
echo "Memory peak usage : " . memory_get_peak_usage() . "\n\n";