-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathccInvertedFile.cpp
More file actions
754 lines (652 loc) · 19.7 KB
/
ccInvertedFile.cpp
File metadata and controls
754 lines (652 loc) · 19.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include <algorithm>
#include <cmath>
#include <fstream>
#include <string>
#include <iterator>
#include "ccCommon.hpp"
#include "ccInvertedFile.hpp"
//------------------------------------------------------------------------
template <class T>
void ivFillFile(ivFile& ivf, T* wlabel, T* dlabel, uint ntokens,
uint nwords, uint ndocs)
{
//allocate vectors
ivf.words.resize(nwords);
ivf.docs.resize(ndocs);
//now loop on the tokens and fill in the hdata
uint i, j, k;
for (i=0; i<ntokens; ++i)
{
//get the labels
if (wlabel[i]==0 || wlabel[i]>nwords || dlabel[i]==0 || dlabel[i]>ndocs)
continue;
uint wl = (uint)wlabel[i]-1;
uint dl = (uint)dlabel[i]-1;
//get word
ivWord* w = &ivf.words[wl];
//increment word count
w->wf++;
//make a new wordDoc
ivWordDoc newdoc;
newdoc.doc = dl;
//find position of this document id in the list
ivWordDocIt wdit = lower_bound (w->docs.begin(), w->docs.end(), newdoc);
//check if not found, then add it in the correct place
if (wdit == w->docs.end() || newdoc<(*wdit))
{
wdit = w->docs.insert(wdit, newdoc);
w->ndocs++;
}
//increment word count for this word's doc
wdit->count++;
//increment document count of tokens
ivf.docs[dl].ntokens++;
}
//put sizes
ivf.nwords = nwords;
ivf.ndocs = ndocs;
}
//fill the inverted file with input counts
//
// data - the input data, with one data vector per input consisting of
// all the word labels for its tokens
// nwords - the total number of words
template <class T>
void ivFillFile(ivFile& ivf, Data<T>& data, uint nwords, uint idshift)
{
//number of documents
uint ndocs = data.size();
//allocate vectors
ivf.words.resize(nwords);
ivf.docs.resize(ndocs);
//loop on the documents and fill in
for (uint d=0; d<ndocs; ++d)
{
// cout << "doc:" << d << endl;
//get the document data
pair<T*,uint> doc = data.getPoint(d);
//document label
uint dl = d + idshift;
//update ntokens for this document
ivf.docs[dl].ntokens += doc.second;
//loop and update
for (uint t=0; t<doc.second; ++t)
{
//word label
if (doc.first[t]==0 || doc.first[t]>nwords) continue;
uint wl = (uint)(doc.first[t]-1);
//get word
ivWord* w = &ivf.words[wl];
//increment word count
w->wf++;
//make a new wordDoc
ivWordDoc newdoc;
newdoc.doc = dl;
//find position of this document id in the list
// cout << "wordid" << wl << " size" << w->docs.size() << endl;
ivWordDocIt wdit = lower_bound (w->docs.begin(), w->docs.end(), newdoc);
//check if not found, then add it in the correct place
if (wdit == w->docs.end() || newdoc<(*wdit))
{
wdit = w->docs.insert(wdit, newdoc);
w->ndocs++;
}
//increment word count for this word's doc
wdit->count++;
}
}
//put sizes
ivf.nwords = nwords;
ivf.ndocs = ndocs;
}
//------------------------------------------------------------------------
void ivFile::computeStats(ivFile::Weight wt, ivFile::Norm norm)
{
uint i;
//reset documents
for (i=0; i<this->ndocs; ++i)
{
this->docs[i].norml0 = 0;
this->docs[i].norml1 = 0;
this->docs[i].norml2 = 0;
this->docs[i].nwords = 0;
this->docs[i].words.clear();
}
//now loop and compute word frequency for every document and norms
for (i=0; i<this->nwords; ++i)
{
//get the word
ivWord* w = &this->words[i];
//get number of documents for this word
w->ndocs = w->docs.size();
//loop on its documents
ivWordDocIt wdit;
for (wdit = w->docs.begin(); wdit != w->docs.end(); wdit++)
{
//get the word document id
uint doc = wdit->doc;
//get the value and weight it
wdit->val = this->weightVal(wdit->count, *w, this->docs[doc], wt);
//update norms
this->docs[doc].norml0 += 1;
this->docs[doc].norml1 += wdit->val;
this->docs[doc].norml2 += (wdit->val * wdit->val);
//add this word to list of words of this document
this->docs[doc].words.push_back(i);
this->docs[doc].nwords++;
}
}
//now normalize vals
for (i=0; i<this->nwords; ++i)
{
//get the word
ivWord* w = &this->words[i];
//loop on its documents
for (ivWordDocIt wdit = w->docs.begin(), wditend = w->docs.end();
wdit != wditend; wdit++)
//normalize
wdit->val = this->normVal(wdit->val, this->docs[wdit->doc], norm);
}
// for (i=0; i<this->docs[5].nwords; ++i)
// {
// ivWordDocIt wdit;
// for (wdit=this->words[(uint)this->docs[5].words[i]].docs.begin();
// wdit!=this->words[(uint)this->docs[5].words[i]].docs.end(); wdit++)
// {
// if (wdit->doc!=5)
// continue;
//
// cout << wdit->val << " ";
// }
// }
}
//------------------------------------------------------------------------
float ivFile::weightVal(float val, ivWord& word, ivDoc& doc, ivFile::Weight wt)
{
switch(wt)
{
case WEIGHT_BIN:
val = val>0 ? 1 : 0;
break;
case WEIGHT_TF:
val /= doc.ntokens ? doc.ntokens : 1;
break;
case WEIGHT_TFIDF:
val /= doc.ntokens ? doc.ntokens : 1;
val *= log2(this->ndocs / (word.ndocs + EPS));
break;
}
return val;
}
//------------------------------------------------------------------------
float ivFile::normVal(float val, ivDoc& doc, ivFile::Norm norm)
{
switch(norm)
{
case NORM_L0:
val /= (EPS + doc.norml0);
break;
case NORM_L1:
val /= (EPS + doc.norml1);
break;
case NORM_L2:
val /= (EPS + sqrt(doc.norml2));
break;
}
return val;
}
//------------------------------------------------------------------------
float ivFile::dist(float val1, float val2, ivFile::Dist dist)
{
switch(dist)
{
case DIST_L1:
val1 -= val2;
val1 = val1>0 ? val1 : -val1;
break;
case DIST_L2:
val1 -= val2;
val1 *= val1;
break;
case DIST_HAM:
val1 = (float) ((uint)val1 ^ (uint)val2);
break;
case DIST_KL:
break;
case DIST_COS:
val1 *= val2;
break;
//there is intersection, return 1 if neither is 0
case DIST_JAC:
val1 = (val1==0 || val2==0) ? 0 : 1;
break;
//histogram intersection: return the minimum
case DIST_HISTINT:
val1 = (val1 <= val2) ? val1 : val2;
break;
}
return val1;
}
//------------------------------------------------------------------------
float ivFile::dist2Norm(ivDoc& doc, ivFile::Dist dist, ivFile::Norm norm)
{
float ret = 0;
//if not normalizing, then return the actual norm
if (norm == NORM_NONE)
{
switch(dist)
{
case DIST_L1:
ret = doc.norml1;
break;
case DIST_L2:
ret = doc.norml2;
break;
case DIST_HAM:
ret = doc.norml0;
break;
case DIST_KL:
break;
case DIST_COS:
ret = 0;
break;
//return 0: which is the value with no intersection
case DIST_JAC:
ret = 0;
break;
//return 0: which is the value with no intersection
case DIST_HISTINT:
ret = 0;
break;
}
}
//otherwise the norm is 1
else
{
switch (dist)
{
case DIST_COS:
ret = 0;
break;
//return 0: which is the value with no intersection
case DIST_JAC:
ret = 0;
break;
//return 0: which is the value with no intersection
case DIST_HISTINT:
ret = 0;
break;
default:
ret = 1;
break;
}
}
return ret;
}
//------------------------------------------------------------------------
template <class T>
void ivSearchFile(ivFile& iv, T* wlabel, uint ntokens,
ivFile::Weight weight, ivFile::Norm norm, ivFile::Dist dist,
bool overlapOnly, uint k, ivNodeList& scorelist)
// float* scores, uint* docs, uint& nret)
{
//return if empty
if (iv.words.empty()) return;
uint i;
//allocate array for storing scores for all documents
// ivNodeList scorelist;
//compute counts for words first
ivNodeList wordcount;
// ivNodeIt ni;
for (i=0; i<ntokens; ++i)
{
//skip word if invalid
uint wr = (uint) wlabel[i];
if (wr==0 || wr>iv.nwords) continue;
//get that word
ivNode w;
w.id = wr - 1;
ivNodeIt wt = lower_bound(wordcount.begin(), wordcount.end(), w);
if (wt==wordcount.end() || w<(*wt)) wt = wordcount.insert(wt, w);
//increment
wt->val += 1;
}
//this doc stats: norms, weights, ...
ivDoc doc;
doc.ntokens = ntokens;
doc.nwords = wordcount.size();
for (ivNodeIt ni=wordcount.begin(), niend = wordcount.end(); ni!=niend; ni++)
{
//weight the value
ni->val = iv.weightVal(ni->val, iv.words[(uint)ni->id], doc, weight);
//compute norms
doc.norml0++;
doc.norml1 += ni->val;
doc.norml2 += (ni->val * ni->val);
}
// cout << "Doc: nwords=" << doc.nwords << " ntokens=" << doc.ntokens <<
// " norml0=" << doc.norml0 << " norml1=" << doc.norml1 <<
// " norml2=" << doc.norml2 << "\n";
// cout << "Doc6: nwords=" << iv.docs[5].nwords << " ntokens=" << iv.docs[5].ntokens <<
// " norml0=" << iv.docs[5].norml0 << " norml1=" << iv.docs[5].norml1 <<
// " norml2=" << iv.docs[5].norml2 << "\n";
//get this documents's norm
float docNorm = iv.dist2Norm(doc, dist, norm);
//if computing distance to all documents, update score list with this word's
// norm. The initial one has twice the norm of every document
if (!overlapOnly && norm==ivFile::NORM_NONE)
{
//loop on the documents and add to score list
for (ivNodeIt dsit = scorelist.begin(), dsend = scorelist.end(); dsit!=dsend; dsit++)
//update norm to include this docs norm
dsit->val = (dsit->val / 2) + docNorm;
}
//now loop on the document words and update scores
for (ivNodeIt wit=wordcount.begin(), witend = wordcount.end(); wit!=witend; wit++)
{
//get the word
ivWord& word = iv.words[(uint)wit->id];
// cout << "\nword: " << wit->id << "\n";
//normalize this doc val
wit->val = iv.normVal(wit->val, doc, norm);
// cout << iv.words.size() << endl;
//loop on the documents
for (ivWordDocIt wdit = word.docs.begin(), wditend = word.docs.end();
wdit != wditend; wdit++)
{
// cout << " doc: "<< wdit->doc+1;
//get that document in the score list
ivNodeIt dsit;
//if not overlap or scorelist has equal length to docs
if (!overlapOnly || scorelist.size()==iv.docs.size())
{
dsit = scorelist.begin() + wdit->doc;
}
//if overlap only, then search for that document in the score list and
//insert if not already there
else
{
ivNode ds;
ds.id = wdit->doc;
dsit = lower_bound(scorelist.begin(), scorelist.end(), ds);
//check if to insert to the list of scores (new doc)
if (dsit==scorelist.end() || ds<(*dsit))
{
//init with sum of norms
ds.val = docNorm + iv.dist2Norm(iv.docs[ds.id], dist, norm);
//insert
dsit = scorelist.insert(dsit, ds);
}
}
// if (dsit->id == 5) //(wdit->doc == 5)
// cout << "1-> " << wit->val << " 2->" << wdit->val << "\n";
//compute distance
dsit->val -= iv.dist(wit->val,0,dist) + iv.dist(wdit->val,0,dist);
dsit->val += iv.dist(wit->val, wdit->val, dist);
}
}
//further processing
if (dist == ivFile::DIST_JAC)
{
//we already have the intersections, so update to compute the Jac distance
// dist = 1 - intersection / union
// where intersection = dsit->val
// and union = nwords1 + nwords2 - intersection
for (ivNodeIt dsit=scorelist.begin(), dsitEnd=scorelist.end(); dsit!=dsitEnd; dsit++)
dsit->val = 1 - dsit->val / (iv.docs[dsit->id].norml0 + doc.norml0 - dsit->val);
}
else if (dist == ivFile::DIST_COS)
{
//update cosine distance by changing the range from -1 -> 1
// (least similar -> most similar)
// to 0 -> 2 (most similar -> least similar)
// where newval = -oldval + 1
for (ivNodeIt dsit=scorelist.begin(), dsitEnd=scorelist.end(); dsit!=dsitEnd; dsit++)
dsit->val = 1 -dsit->val;
}
else if (dist == ivFile::DIST_HISTINT)
{
//update histogram intersection kernel by inverting and adding 1
//(assuming normalization)
for (ivNodeIt dsit=scorelist.begin(), dsitEnd=scorelist.end(); dsit!=dsitEnd; dsit++)
dsit->val = 1 - dsit->val;
}
//sort ascendingly
ivNodeCmpValAsc ncv;
sort(scorelist.begin(), scorelist.end(), ncv);
//keep only k
if (k>0 && k<scorelist.size())
scorelist.resize(k);
//clean memory
wordcount.clear();
}
//------------------------------------------------------------------------
template <class T>
void ivSearchFile(ivFile& iv, Data<T>& data,
ivFile::Weight weight, ivFile::Norm norm, ivFile::Dist dist,
bool overlapOnly, uint k, ivNodeLists& scorelists, bool verbose=false)
{
//number of documents
uint ndocs = data.size();
//resize the scorelists
scorelists.resize(ndocs);
// cout << "we have " << ndocs << " docs" << endl;
//initialize score list with full list of documents, and just copy it
//afterwords. Init it with twice the norm
ivNodeList tscorelist;
if (!overlapOnly)
{
tscorelist.resize(iv.docs.size());
//loop on the documents and add to score list
ivDocIt dit = iv.docs.begin(), dend = iv.docs.end();
ivNodeIt dsit = tscorelist.begin();
for (uint i=0; dit!=dend; dit++, dsit++, i++)
{
//put the document id
dsit->id = i;
//put the initial distance: sum of norms
dsit->val = 2 * iv.dist2Norm(*dit, dist, norm);
}
}
//now loop
for (uint d=0; d<ndocs; d++)
{
if (verbose && (d==0 || (d % (uint)round(0.1 * ndocs) == 0)) )
{
cout << " doc:" << d << " / " << ndocs << endl;
cout.flush();
}
//get data for this document
pair<T*, uint> doc = data.getPoint(d);
//make a copy of tscorelist
ivNodeList ttscorelist = tscorelist;
//search for this document
ivSearchFile(iv, doc.first, doc.second, weight, norm, dist, overlapOnly,
k, ttscorelist);
//put back into scorelists
scorelists[d] = ttscorelist;
//clear
ttscorelist.clear();
}
}
//------------------------------------------------------------------------
void ivFile::clear()
{
//clears the docs array
this->docs.clear();
//clear words array
this->words.clear();
}
//------------------------------------------------------------------------
//Stream overloads for ivWord
ostream& operator<<(ostream& os, ivWord& ivw)
{
os.write((char*)&ivw.ndocs, sizeof(ivw.ndocs));
os.write((char*)&ivw.wf, sizeof(ivw.wf));
}
istream& operator>>(istream& is, ivWord& ivw)
{
is.read((char*)&ivw.ndocs, sizeof(ivw.ndocs));
is.read((char*)&ivw.wf, sizeof(ivw.wf));
}
//------------------------------------------------------------------------
//Stream overloads for ivWordDoc
ostream& operator<<(ostream& os, ivWordDoc& ivwd)
{
os.write((char*)&ivwd.count, sizeof(ivwd.count));
os.write((char*)&ivwd.doc, sizeof(ivwd.doc));
}
istream& operator>>(istream& is, ivWordDoc& ivwd)
{
is.read((char*)&ivwd.count, sizeof(ivwd.count));
is.read((char*)&ivwd.doc, sizeof(ivwd.doc));
}
//------------------------------------------------------------------------
//Stream overloads for ivDoc
ostream& operator<<(ostream& os, ivDoc& ivdoc)
{
os.write((char*)&ivdoc.ntokens, sizeof(ivdoc.ntokens));
// os.write((char*)&ivdoc.nwords, sizeof(ivdoc.nwords));
}
istream& operator>>(istream& is, ivDoc& ivdoc)
{
is.read((char*)&ivdoc.ntokens, sizeof(ivdoc.ntokens));
// is.read((char*)&ivdoc.nwords, sizeof(ivdoc.nwords));
}
//------------------------------------------------------------------------
//Stream overloads for ivFile
ostream& operator<<(ostream& os, ivFile& ivf)
{
uint i;
//save the document array
//
//put the size
os.write((const char*)&ivf.ndocs, sizeof(ivf.ndocs));
//put the array
for (i=0; i<ivf.ndocs; ++i)
os << ivf.docs[i];
//save the words array
//
//put the size
os.write((const char*)&ivf.nwords, sizeof(ivf.nwords));
//put the array
for (i=0; i<ivf.nwords; ++i)
os << ivf.words[i];
//save the word docs arrays
ivWordDocIt wdit;
for (i=0; i<ivf.nwords; ++i)
{
//write for this word
for (wdit=ivf.words[i].docs.begin(); wdit != ivf.words[i].docs.end();
wdit++)
os << *wdit;
}
}
istream& operator>>(istream& is, ivFile& ivf)
{
uint i;
//read the document array
//
//read the size & allocate
is.read((char*)&ivf.ndocs, sizeof(ivf.ndocs));
ivf.docs.resize(ivf.ndocs);
//put the array
for (i=0; i<ivf.ndocs; ++i)
is >> ivf.docs[i];
//read the words array
//
//read the size & allocate
is.read((char*)&ivf.nwords, sizeof(ivf.nwords));
ivf.words.resize(ivf.nwords);
// cout << "Words: " << this->words.size() << "\n";
//put the array
for (i=0; i<ivf.nwords; ++i)
is >> ivf.words[i];
//read the word docs arrays
ivWordDocIt wdit;
for (i=0; i<ivf.nwords; ++i)
{
//get the size of this list
uint s = ivf.words[i].ndocs;
ivf.words[i].docs.resize(s);
//read for this word
for (wdit=ivf.words[i].docs.begin(); wdit != ivf.words[i].docs.end();
wdit++)
is >> *wdit;
}
}
//------------------------------------------------------------------------
void ivFile::save(string filename)
{
//open the file for opening
ofstream of;
of.open(filename.c_str(), ios_base::binary);
//save
of << *this;
//close file
of.close();
}
//------------------------------------------------------------------------
void ivFile::load(string filename)
{
//open the file for opening
ifstream inf;
inf.open(filename.c_str(), ofstream::binary);
inf >> *this;
//close file
inf.close();
}
//------------------------------------------------------------------------
void ivFile::display()
{
uint i, j;
//display the document array
//
cout << "Documents:\n----------\n";
for (i=0; i<this->docs.size(); ++i)
{
cout << "Doc " << i << ": ntokens=" << this->docs[i].ntokens <<
" nwords=" << this->docs[i].nwords <<
" norml0=" << this->docs[i].norml0 <<
" norml1=" << this->docs[i].norml1 <<
" norml2=" << this->docs[i].norml2 << "\n";
}
//display the words array
cout << "\nWords:\n--------";
for (i=0; i<this->words.size(); ++i)
{
cout << "\nWord " << i << ": (ndocs=" << this->words[i].ndocs <<
", wf=" << this->words[i].wf << ")\n\t";
ivWordDocIt wdit;
for (wdit=this->words[i].docs.begin(); wdit != this->words[i].docs.end();
wdit++)
{
cout << "<doc=" << wdit->doc << ", count=" << wdit->count <<
", val=" << wdit->val << ">";
}
}
}
//------------------------------------------------------------------------
// #define TEMPLATE(F) \
// F(float) \
// F(double) \
// F(char) \
// F(int) \
// F(unsigned int) \
// F(unsigned char)
#define IVFILLFILE(T) \
template void ivFillFile(ivFile&, T*, T*, uint, uint, uint);
#define IVFILLFILE2(T) \
template void ivFillFile(ivFile&, Data<T>&, uint, uint);
//#define IVSEARCHFILE(T) \
// template void ivSearchFile(ivFile&, T*, uint, ivFile::Weight, \
// ivFile::Norm, ivFile::Dist,ivNodeList&);
#define IVSEARCHFILE(T) \
template void ivSearchFile(ivFile&, Data<T>&, ivFile::Weight, ivFile::Norm, \
ivFile::Dist, bool, uint, ivNodeLists&, bool);
TEMPLATE(IVFILLFILE)
TEMPLATE(IVFILLFILE2)
TEMPLATE(IVSEARCHFILE)
#undef IVFILLFILE
#undef IVFILLFILE2
#undef IVSEARCHFILE