-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtxt2bin.cpp
More file actions
235 lines (196 loc) · 5.71 KB
/
Copy pathtxt2bin.cpp
File metadata and controls
235 lines (196 loc) · 5.71 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
#include <iostream>
#include <fstream>
#include <map>
#include <list>
#include <string>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "header.h"
char g_version[]=VERSION;
using namespace std;
typedef list<int> listtype;
typedef list<int>::iterator listtypeit;
typedef map<string, listtype> maptype;
typedef map<string, listtype>::iterator maptypeit;
maptype invert;
typedef struct _order_node_t{
int id;
int len;
}order_node_t;
// We generate a token list as a stop words.
// but We eliminate the duplicate did from
// the inverted list to simplify this problem
// to a certain level.
int tokenize_stop(string s, int did)
{
char cstr[STRING_LEN];
char sep[] = " _.,?!\t";
char *brkb;
char *token;
strcpy(cstr, s.c_str());
for (token = strtok_r(cstr, sep, &brkb);
token;
token = strtok_r(NULL, sep, &brkb)){
maptypeit mapit = invert.find(token);
if (mapit != invert.end()){
listtype *ivlistp = &mapit->second;
if(ivlistp->back() != did)
ivlistp->push_back(did);
}else{
listtype *ivlistp = new listtype();
ivlistp->push_back(did);
invert[token] = *ivlistp;
}
}
return 0;
}
// Most time we use q-gram as tokenizing method.
// This method will generate too much redundency.
// We also eliminate duplicate index did to simplify
// our problem.
int tokenize_qgram(string s, int did, int q)
{
char token[STRING_LEN];
int len = s.length();
int l = 0;
for (int i = 0; i < len - q; i++){
l = s.copy(token, q, i);
token[l] = '\0';
maptypeit mapit = invert.find(token);
if (mapit != invert.end()){
listtype *ivlistp = &mapit->second;
if(ivlistp->back() != did)
ivlistp->push_back(did);
}else{
listtype *ivlistp = new listtype();
ivlistp->push_back(did);
invert[token] = *ivlistp;
}
}
return 0;
}
int cmpOrder(const void *a, const void *b)
{
if (((order_node_t*)a)->len < ((order_node_t*)b)->len)
return -1;
else if (((order_node_t*)a)->len == ((order_node_t*)b)->len
&& ((order_node_t*)a)->id < ((order_node_t*)b)->id)
return -1;
else
return 1;
}
int output_invert(char *filename){
int ntoken = invert.size();
listtype ** tivlist = new listtype* [ntoken];
string * tstrlist = new string[ntoken];
int i = 0;
for (maptypeit mapit = invert.begin(); mapit != invert.end(); mapit ++){
if ( mapit->second.size() < 5 ) continue;
tivlist[i] = &mapit->second; // make a map into a list
tstrlist[i] = mapit->first;
i ++;
}
ntoken = i;
order_node_t *order = new order_node_t[ntoken];
for (i = 0; i < ntoken; i++) {order[i].id = i; order[i].len = tivlist[i]->size();}
qsort(order, ntoken, sizeof(order_node_t), cmpOrder);
for (i = 0; i < ntoken; i++){
cout << "tid_" << i+1 << " "<< order[i].len << " "<< tstrlist[order[i].id];
for (listtypeit listit=tivlist[order[i].id]->begin();
listit != tivlist[order[i].id]->end();
listit ++){
cout << " " << *listit;
}
cout << endl;
}
FILE *fp = fopen(filename, "wb");
for(i = 0; i < ntoken; i++){
int tid = order[i].id;
int slen = tstrlist[tid].length() + 1;
int p = 0;
char sbuf[1024];
strcpy (sbuf, tstrlist[tid].c_str());
sbuf[slen] = '\0';
if (order[i].len == 0) continue;
int k = i + 1;
fwrite(&k, sizeof(int), 1, fp); // Token number
fwrite(&slen, sizeof(int), 1, fp); // Token string length
fwrite(&order[i].len, sizeof(int), 1, fp); // Token inverted list length
fwrite(&p, sizeof(int), 1, fp); // Token Top-k list length
fwrite(sbuf, sizeof(char), slen, fp); // Token string content
for (listtypeit listit=tivlist[tid]->begin();
listit != tivlist[tid]->end();
listit ++){
k = *listit;
// cout << "output k " << k;
fwrite(&k, sizeof(int), 1, fp);
}
}
fclose(fp);
delete [] tivlist;
delete [] tstrlist;
delete [] order;
return 0;
}
void print_version(){
fprintf(stderr, "Version: %s\n", g_version);
exit(0);
}
void print_usage(){
fprintf(stderr, "usage: <-q <qgram length> defaut is tokenize>\n");
fprintf(stderr, " <-o <output index name>>\n");
print_version();
exit(0);
}
int main(int argc, char* argv[])
{
int q = 0;
char *output = NULL;
char c;
string i;
int n = 0;
while ((c = getopt(argc,argv, "hvq:o:")) != -1)
switch (c){
case 'q':
q = atoi(optarg);
break;
case 'o':
output = optarg;
break;
case 'h':
print_usage();
break;
case 'v':
print_version();
break;
case '?':
if ( optopt == 'q' || optopt == 'o' )
cerr << "Error: Option -" << optopt << "requires an argument." << endl;
else if ( isprint(optopt))
cerr << "Error: Unknown Option -" << optopt << endl;
else
cerr << "Error: Unknown Option character" <<endl;
return 1;
default:
print_usage();
}
// output name check.
if ( output == NULL ){
cerr << "Need out put file name" <<endl;
print_usage();
}
while (cin){
getline(cin, i);
n++;
if ( q <= 0 )
tokenize_stop(i,n);
else
tokenize_qgram(i,n,q);
if ( n % 10000 == 0 )
cerr << n << endl;
}
cerr << " output now" <<endl;
output_invert(output);
return 0;
}