-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.cpp
More file actions
75 lines (57 loc) · 1.88 KB
/
Copy pathindex.cpp
File metadata and controls
75 lines (57 loc) · 1.88 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
#include <math.h>
#include "header.h"
#include "index.h"
extern int n; //number of records
extern int tokenNum; //total number of tokens
extern int *token[STRING_NUM]; //record content
extern int len[STRING_NUM]; //record length
extern int freq[TOKEN_NUM]; //frequency of token within the document
extern int vector_id[STRING_NUM]; //record id within original txt file
elem_index* list[TOKEN_NUM]; //inverted lists
int indexElemNum, indexTokenNum; //stats for inverted lists
int indexStart[ELEM_NUM], indexEnd[ELEM_NUM]; //start/end pointer for inverted lists
//generating inverted lists
void generate_index()
{
int i, j, tok;
indexElemNum = 0;
memset(freq, 0, tokenNum * sizeof(int));
for (i = 0; i < n; i++) {
for (j = 0; j < len[i]; j++)
++freq[token[i][j]];
}
//inserting into inverted lists
for (i = 0; i < tokenNum; i++)
if (freq[i] >= FREQ_LIMIT) //FREQ_LIMIT is 2 here to avoid inserting widow tokens
list[i] = new elem_index[freq[i]];
memset(indexStart, -1, tokenNum * sizeof(int));
memset(indexEnd, -1, tokenNum * sizeof(int));
// Need to indexing in a reverse order.
// Log changes made in version 2 by jianbin Qin.
for (i = n-1; i >= 0; i--)
for (j = 0; j < len[i]; j++) {
tok = token[i][j];
if (freq[tok] < FREQ_LIMIT) continue;
if (indexStart[tok] < 0) indexStart[tok] = indexEnd[tok] = 0;
list[tok][indexEnd[tok]].str = i;
++indexEnd[tok];
++indexElemNum;
}
for (i = 0, j = 0; i < tokenNum; i++) if (freq[i]) ++j;
std::cerr << "# Distinct Indexed Tokens: " << j << std::endl;
std::cerr << "# Inverted Index Entries: " << indexElemNum << std::endl;
}
void freeToken()
{
int i;
for (i = 0; i < n; i++)
delete [] token[i];
return;
}
void freeIndex()
{
int i;
for (i = 0; i < tokenNum; i++)
if (indexEnd[i] >= 0)
delete [] list[i];
}