-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngweight.cpp
241 lines (220 loc) · 5.96 KB
/
ngweight.cpp
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
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <utility>
#include <unordered_map>
#include "cmdline.h"
#include "esa.hxx"
#include "wat_array.hpp"
using namespace std;
int getID(const string& str, unordered_map<string, int>& word2id){
unordered_map<string, int>::const_iterator it = word2id.find(str);
if (it == word2id.end()){
int newID = (int)word2id.size();
word2id[str] = newID;
return newID;
} else {
return it->second;
}
}
string getTerm(const vector<int>& T, const int beg, const int len,
const vector<string>& id2word){
string term = "";
for (int i = 0; i < len; ++i){
int c = T[beg + i];
if (id2word.size() > 0){
term += id2word[c] + " ";
} else {
term += (isspace((char)c) ? '_' : (char)c);
}
}
return term;
}
int main(int argc, char* argv[]){
cmdline::parser p;
p.add("word", 'w', "word type");
p.add("threshold", 't', "min freq to reserve N-grams (for N>1)", false, 2);
p.add("sdfthreshold", 's', "max df to exactly count (set 0 for exact count)", false, 0);
p.add("maxlength", 'l', "max length of N-grams", false, 10);
p.add("rand", 'r', "initial value for random", false, 1);
if (!p.parse(argc, argv)){
cerr << p.error() << endl
<< p.usage() << endl;
return -1;
}
if (p.rest().size() > 0){
cerr << p.usage() << endl;
return -1;
}
vector<int> T;
vector<int> Doc;
bool isWord = p.exist("word");
int threshold = p.get<int>("threshold");
int sdfthreshold = p.get<int>("sdfthreshold");
int maxlength = p.get<int>("maxlength");
int randini = p.get<int>("rand");
unordered_map<string, int> word2id;
istreambuf_iterator<char> isit(cin);
istreambuf_iterator<char> end;
size_t origLen = 0;
int docid = -1;
int lfid = getID("\n", word2id);
if (isWord){
cerr << "Word mode:" << endl;
string word;
while (isit != end){
char c = *isit++;
if (c == 0x02){
c = *isit++;
string s = "";
while (c != 0x03) {
s += c;
c = *isit++;
}
docid++;
//docid = atoi(s.c_str());
c = *isit++;
}
if (!isspace(c) && !ispunct(c)){
word += tolower(c);
} else{
if (word.size() > 0){
T.push_back(getID(word, word2id));
Doc.push_back(docid);
word = "";
}
if (isspace(c) && c != ' '){
T.push_back(lfid);
Doc.push_back(docid);
}
}
++origLen;
}
if (word.size() > 0){
T.push_back(getID(word, word2id));
Doc.push_back(docid);
}
} else {
cerr << "Char mode: not supported yet! sorry!" << endl;
return -1;
}
docid++;
vector<string> id2word(word2id.size());
for (unordered_map<string, int>::const_iterator it = word2id.begin();
it != word2id.end(); ++it){
id2word[it->second] = it->first;
}
int n = T.size();
vector<int> SA(n);
int k = (isWord) ? (int)id2word.size() : 0x100;
if (isWord){
cerr << "origN:" << origLen << endl;
}
cerr << " n:" << n << endl;
cerr << "alpha:" << k << endl;
cerr << "docid:" << docid << endl;
if (sais_xx(T.begin(), SA.begin(), n, k) == -1){
return -1;
}
// Randomize Doc
vector<int> Drand(docid);
for (int i = 0; i < docid; ++i){
Drand[i] = i;
}
mt19937 mt(randini);
for (int i = docid-1; i > 0; --i){
uniform_int_distribution<> rnd(0, i);
int r = rnd(mt);
int x = Drand[r];
Drand[r] = Drand[i];
Drand[i] = x;
}
int preword = T[SA[0]];
int prepos = 0;
vector<uint64_t> DA(n);
unordered_map<int, pair<int, int> > w2lr;
for(int i = 1; i < n; ++i){
DA[i] = Drand[Doc[SA[i]]];
int word = T[SA[i]];
if (word != preword){
w2lr[preword] = pair<int, int>(prepos, i);
preword = word;
prepos = i;
}
}
vector<int>().swap(Doc);
w2lr[preword] = pair<int, int>(prepos, n);
wat_array::WatArray wa;
wa.Init(DA);
vector<uint64_t>().swap(DA);
vector<int> L (n);
vector<int> R (n);
vector<int> D (n);
int nodeNum = 0;
if (esa_xx(T.begin(), SA.begin(),
L.begin(), R.begin(), D.begin(),
n, k, nodeNum) == -1){
return -1;
}
cerr << " node:" << nodeNum << endl;
vector<int> Rn(n);
Rn[0] = 0;
int rank = 0;
for (int i = 1; i < n; ++i){
if (SA[i]-1 < 0 || SA[i-1]-1 < 0){
rank++;
}
else if (T[SA[i]-1] != T[SA[i-1]-1]){
rank++;
}
Rn[i] = rank;
}
for (int i = nodeNum - 2; i >= 0; --i){
if (D[i] > 1 && R[i] - L[i] < threshold) continue;
if (D[i] > maxlength) continue;
if (Rn[R[i]-1] - Rn[L[i]] > 0){
int beg = SA[L[i]];
int len = D[i];
bool skip = false;
int gtf = R[i] - L[i];
for (int k = 0; k < len; ++k){
if (T[beg+k] == lfid){
skip = true;
break;
}
}
if (skip) continue;
vector<uint64_t> beg_pos;
vector<uint64_t> end_pos;
vector<size_t> nums;
unordered_map<int, int> word2num;
for (int k = 0; k < len; ++k){
unordered_map<int, int>::const_iterator it = word2num.find(T[beg+k]);
if (it == word2num.end()){
word2num[T[beg+k]] = 1;
} else {
word2num[T[beg+k]]++;
}
}
for (unordered_map<int, int>::const_iterator it = word2num.begin();
it != word2num.end(); ++it){
beg_pos.push_back(w2lr[it->first].first);
end_pos.push_back(w2lr[it->first].second);
nums.push_back(it->second);
}
string term = getTerm(T, beg, len, id2word);
int df = wa.Count(L[i], R[i], 0, n, 0);
int sdf = 0;
if (sdfthreshold <= 0){
sdf = wa.Count(beg_pos, end_pos, nums, 0, n, 0);
} else {
sdf = wa.ApproxCount(beg_pos, end_pos, nums, 0, n, 0, sdfthreshold);
}
//double ngw = log2((double)docid/sdf);
//double ngw2 = log2((double)docid*df/(sdf*sdf));
cout << i << "\t" << len << "\t" << gtf << "\t" << df << "\t" << sdf << "\t" << term << endl;
}
}
return 0;
}