-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimSearcher.cpp
More file actions
330 lines (300 loc) · 10.6 KB
/
SimSearcher.cpp
File metadata and controls
330 lines (300 loc) · 10.6 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "SimSearcher.h"
using namespace std;
SimSearcher::SimSearcher()
{
}
SimSearcher::~SimSearcher()
{
}
int SimSearcher::createIndex(const char *filename, unsigned q)
{
this->qq = q;
ifstream fin(filename);
string line;
int id = 0;
if(!fin){cout<<"could not open file!";return FAILURE;}
//invertList.rehash(BUCKET_COUNT);
smin = 1023;
while(getline(fin,line)){
records.push_back(line);
updateList(line,id);
int word_size = updateWordList(line,id);
word_counter.push_back(word_size);
if(smin > word_size)smin = word_size;
id++;
}
recordSize = records.size();
// for(unordered_map<string,vector<int> >::iterator it=invertList.begin();it!=invertList.end();it++){
// listSize.push_back(pair<string,int>(it->first,it->second.size()));
// }
//
// sort(listSize.begin(),listSize.end(),asdf);
// for(int i = 0;i<listSize.size();i++){
// position[listSize[i].first] = i;
// }
fin.close();
return SUCCESS;
}
int SimSearcher::searchJaccard(const char *query, double threshold, vector<pair<unsigned, double> > &result)
{
result.clear();
string q = query;
vector<string> words;
split(q,' ',words);
//words 去重
sort(words.begin(),words.end());
words.erase(unique(words.begin(),words.end()),words.end());
vector<int> candidate;
int word_size = words.size();
int T = my_max(threshold*word_size,(smin+word_size)*threshold/(threshold+1));
scanCount(this->word_invertList,words,T,candidate);
sort(candidate.begin(),candidate.end());
if(T>=1){
int size = candidate.size();
for(int i = 0;i<size;i++){
int id = candidate[i];
double jaccard = double(counters[id])/double(word_counter[id]+word_size-counters[id]);
if(jaccard>=threshold) result.push_back(pair<unsigned,double>(id,jaccard));
}
}
else{
for(int i = 0;i<recordSize;i++){
double jaccard = double(counters[i])/double(word_counter[i]+word_size-counters[i]);
if(jaccard>=threshold) result.push_back(pair<unsigned,double>(i,jaccard));
}
}
return SUCCESS;
}
int SimSearcher::searchED(const char *query, unsigned threshold, vector<pair<unsigned, unsigned> > &result)
{
result.clear();
int thrs = threshold;
string q = query;
vector<string> grams;
splitIntoGram(q,qq,grams);
//grams 去重
sort(grams.begin(),grams.end());
grams.erase(unique(grams.begin(),grams.end()),grams.end());
int len1 = q.length();
int T = len1-qq+1-thrs*qq;
if(T>=1){
vector<int>candidate;
scanCount(this->gram_invertList,grams,T,candidate);
//divideSkip(grams,T,candidate);
int size = candidate.size();
sort(candidate.begin(),candidate.end());
for(int i = 0;i<size;i++){
int id = candidate[i];
int len2 = records[id].length();
if(abs(len1-len2)>thrs) continue;
int distance = 0;
if(len1<len2){
distance = calED(q,records[id],thrs);
}
else{
distance = calED(records[id],q,thrs);
}
if(distance == -1) continue;
if(distance<=thrs) result.push_back(pair<unsigned,unsigned>(id,distance));
}
}
else{
for(int i = 0;i<recordSize;i++){
int len2 = records[i].length();
if(abs(len1-len2)>thrs) continue;
int distance = 0;
if(len1<len2){
distance = calED(q,records[i],thrs);
}
else{
distance = calED(records[i],q,thrs);
}
if(distance == -1) continue;
if(distance<=thrs) result.push_back(pair<unsigned,unsigned>(i,distance));
}
}
return SUCCESS;
}
int SimSearcher::calED(const string& s1,const string& s2,int threshold) { //s2 must be longer thant s1!
int row = s1.length();
int col = s2.length();
for(int i =0;i<=threshold&&i<=col;i++)d[0][i]=i;
for(int i = 0;i<=threshold&&i<=row;i++)d[i][0]=i;
int start;int end;int last_end;
int count = 0;
for(int i = 1;i<=row;i++){
count = 0;
last_end = end;
start = (i-threshold>1)?(i-threshold):1;
end = (i+threshold)<col?(i+threshold):col;
for(int j = start;j<=end;j++){
if(j == start){
int cost = (s1[i-1]==s2[j-1])? 0:1;
int insertion = d[i-1][j]+1;
int match = d[i-1][j-1]+cost;
d[i][j] = min(insertion,match);
if(d[i][j]>threshold) count++;
}
else if(j == end){
if(last_end == col){
int cost = s1[i-1]==s2[j-1]? 0:1;
int insertion = d[i-1][j]+1;
int deletion = d[i][j-1]+1;
int match = d[i-1][j-1]+cost;
d[i][j] = min3(deletion,insertion,match);
if(d[i][j]>threshold) count++;
}
else{
int cost = s1[i-1]==s2[j-1]? 0:1;
int deletion = d[i][j-1]+1;
int match = d[i-1][j-1]+cost;
d[i][j] = min(deletion,match);
if(d[i][j]>threshold) count++;
}
}
else{
int cost = s1[i-1]==s2[j-1]? 0:1;
int insertion = d[i-1][j]+1;
int deletion = d[i][j-1]+1;
int match = d[i-1][j-1]+cost;
d[i][j] = min3(deletion,insertion,match);
if(d[i][j]>threshold) count++;
}
}
if(count == end-start+1) return -1;
}
return d[row][col];
}
int SimSearcher::splitIntoGram(const string& s,int q,vector<string>& result){
int len = s.length();
if(len<q) return FAILURE;
for(int i = 0;i<=len-q;i++){
result.push_back(string(s,i,q));
}
return SUCCESS;
}
void SimSearcher::updateList(const string& s,const int id){
vector<string>grams;
if(splitIntoGram(s,qq,grams)==SUCCESS){
//sort(grams.begin(),grams.end());
//grams.erase(unique(grams.begin(),grams.end()),grams.end());
int gsize = grams.size();
for(int i = 0;i<gsize;i++){
gram_invertList[grams[i]].push_back(id);
}
}
}
int SimSearcher::updateWordList(const string& s,int id){
vector<string> words;
split(s,' ',words);
sort(words.begin(),words.end());
words.erase(unique(words.begin(),words.end()),words.end());
int size = words.size();
for(int i = 0;i<size;i++){
word_invertList[words[i]].push_back(id);
}
return size;
}
void SimSearcher::scanCount(unordered_map<string,vector<int> >&invertList,const vector<string>& grams,int T,vector<int>& candidate){
int gsize = grams.size();
for(int i = 0;i<recordSize;i++) counters[i] = 0;
unordered_map<string,vector<int> >::iterator it;
for(int i = 0;i<gsize;i++){
it = invertList.find(grams[i]);
if(it!=invertList.end()){
for(int j =0;j<it->second.size();j++){
counters[it->second[j]]++;
if(counters[it->second[j]] == T)
candidate.push_back(it->second[j]);
}
}
}
}
void SimSearcher::divideSkip(unordered_map<string,vector<int> >&invertList,const vector<string>& grams,int T,vector<int>& candidate){
vector<vector<int> > selected;
for(int i = 0;i<grams.size();i++){
if(invertList.find(grams[i])!=invertList.end())
selected.push_back(invertList[grams[i]]);
}
sort(selected.begin(),selected.end(),comp);
vector<vector<int> >::iterator tmp =selected.begin();
int L = T/2;
tmp=tmp+L;
vector<vector<int> >::iterator border = tmp;
vector<vector<int> >shortLists(border,selected.end());
unordered_map<int,int> records; //第一个int 是id,第二个是出现的次数
mergeSkip(T-L,shortLists,records);
for(unordered_map<int,int>::iterator it = records.begin();it!=records.end();it++){
int count = 0;
for(tmp = selected.begin();tmp!=border;tmp++){
if(binary_search(tmp->begin(),tmp->end(),it->first)){
count++;
}
}
if((it->second+count)>=T){candidate.push_back(it->first);}
}
}
void SimSearcher::mergeSkip(int T,vector<vector<int>>&lists,unordered_map<int,int>&result){
cout<<"start"<<endl;
int len = lists.size();
vector<vector<int>::iterator >pointers;
vector<int>popedList;
vector<pair<int,int> > heap; // 第一个int代表list的编号,第二个int代表当前首元素id
for(int i =0;i<len;i++){
heap.push_back(pair<int,int>(i,lists[i][0]));
pointers.push_back(lists[i].begin()); // 初始化 pointer
}
make_heap(heap.begin(),heap.end(),heap_comp);//建立最小堆
while(!heap.empty()){
popedList.clear();
int n = 0;
int t = heap.front().second;
while(t == heap.front().second){
n++;
popedList.push_back(heap.front().first);
pop_heap(heap.begin(),heap.end(),heap_comp);
heap.pop_back();
}
if(n>=T){
result[t] = n;
for(int j = 0;j<popedList.size();j++){
int listId = popedList[j];
pointers[listId]++;
if(pointers[listId]!=lists[listId].end()){
heap.push_back(pair<int,int>(listId,*pointers[listId]));
push_heap(heap.begin(),heap.end(),heap_comp);
}
}
}
else{
for(int j = 0;j<T-1-n;j++){
popedList.push_back(heap.front().first);
pop_heap(heap.begin(),heap.end(),heap_comp);
heap.pop_back();
}
int tp = heap.front().second;
for(int i = 0;i<popedList.size();i++){
int listId = popedList[i];
pointers[listId] = lower_bound(lists[listId].begin(),lists[listId].end(),tp);
if(pointers[listId]!=lists[listId].end()){
heap.push_back(pair<int,int>(listId,*pointers[listId]));
push_heap(heap.begin(),heap.end(),heap_comp);
}
}
}
}
}
void SimSearcher::split(const string& str,char s,vector<string>&words){
int len = str.length();
int start = 0;
for(int i = 0;i<len;i++){
if(str[i] == s){
words.push_back(str.substr(start,i-start));
while(str[i+1] == s){
i++;
}
start = i+1;
}
}
words.push_back(str.substr(start,len-start));
}