-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.cpp
executable file
·384 lines (311 loc) · 8.79 KB
/
dataset.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
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
#include "constants.h"
#include "strtokenizer.h"
#include "dataset.h"
using namespace std;
// write the mapword2id into a file
// input : filename and mapword2id
int dataset::write_wordmap(string wordmapfile, mapword2id * pword2id) {
ofstream fout;
fout.open(wordmapfile.c_str(), ofstream::out);
if (!fout.is_open()) {
cout << "Cannot open file " << wordmapfile << " to write!" << endl;
return 1;
}
fout << pword2id->size() << endl;
for (mapword2id::iterator it = pword2id->begin(); it != pword2id->end(); it++) {
fout << it->first << " " << it->second << endl;
}
fout.close();
return 0;
}
// write data of file into a pword2id
int dataset::read_wordmap(string wordmapfile, mapword2id * pword2id) {
pword2id->clear();
strtokenizer strtok;
ifstream fin;
fin.open(wordmapfile.c_str(), ifstream::in);
if (!fin.is_open()) {
cout << "Cannot open file " << wordmapfile << " to read!" << endl;
return 1;
}
string line;
getline(fin, line);
int nwords = atoi(line.c_str());
for (long i = 0; i < nwords; i++) {
getline(fin, line);
strtok.split(line, " \t\r\n", false);
if (strtok.count_tokens() != 2) {
continue;
}
pword2id->insert(pair<string, int>(strtok.token(0), atoi(strtok.token(1).c_str())));
strtok.clear();
}
fin.close();
return 0;
}
int dataset::read_wordmap(string wordmapfile, mapid2word * pid2word) {
pid2word->clear();
strtokenizer strtok;
ifstream fin;
fin.open(wordmapfile.c_str(), ifstream::in);
if (!fin.is_open()) {
cout << "Cannot open file " << wordmapfile << " to read!" << endl;
return 1;
}
string line;
getline(fin, line);
int nwords = atoi(line.c_str());
for (long i = 0; i < nwords; i++) {
getline(fin, line);
strtok.split(line, " \t\r\n", false);
if (strtok.count_tokens() != 2) {
continue;
}
pid2word->insert(pair<int, string>(atoi(strtok.token(1).c_str()), strtok.token(0)));
strtok.clear();
}
fin.close();
return 0;
}
int dataset::read_trndata(string classfile, string dfile, string wordmapfile, vector<pair<string, int> >& movie_classes) {
mapword2id word2id;
ifstream fin;
string line;
strtokenizer strtok;
fin.open(classfile.c_str(), ifstream::in);
if (fin.is_open()) {
while(!fin.eof()) {
getline(fin, line);
strtok.parse(line, " \t\r\n");
movie_classes.push_back(pair<string, int>(strtok.token(0), atoi(strtok.token(1).c_str())));
cout << movie_classes[movie_classes.size()-1].first << " " << movie_classes[movie_classes.size()-1].second << endl;
strtok.clear();
}
}
fin.close();
fin.open(dfile.c_str(), ifstream::in);
if (!fin.is_open()) {
cout << "Cannot open file " << dfile << " to read!" << endl;
return 1;
}
mapword2id::iterator it;
// get the number of documents
getline(fin, line);
M = atoi(line.c_str());
if (M <= 0) {
cout << "No document available!" << endl;
return 1;
}
// allocate memory for corpus
if (docs) {
deallocate();
} else {
docs = new document*[M];
}
// set number of words to zero
V = 0;
for (int i = 0; i < M; i++) {
getline(fin, line);
cout << i << endl;
strtok.split(line, " \t\r\n", false);
int length = strtok.count_tokens();
if (length <= 0) {
cout << "Invalid (empty) document!" << endl;
deallocate();
M = V = 0;
return 1;
}
// allocate new document
document * pdoc = new document(length);
for (int j = 0; j < length; j++) {
it = word2id.find(strtok.token(j));
if (it == word2id.end()) {
// word not found, i.e., new word
pdoc->words[j] = word2id.size();
word2id.insert(pair<string, int>(strtok.token(j), word2id.size()));
} else {
pdoc->words[j] = it->second;
}
}
// add new doc to the corpus
add_doc(pdoc, i);
strtok.clear();
}
fin.close();
// write word map to file
if (write_wordmap(wordmapfile, &word2id)) {
return 1;
}
// update number of words
V = word2id.size();
if (write_all(M, V, docs)) {
return 1;
}
return 0;
}
int dataset::write_all(int M, int V, document ** docs) {
ofstream fout;
fout.open("model/alldata.dat", ofstream::out);
if (!fout.is_open()) {
cout << "can't create data file." << endl;
return 1;
}
fout << M << endl;
fout << V << endl;
int length = 0, m = 0;
for (int i = 0; i < M; i++) {
length = docs[i]->length;
for (int j = 0; j < length; j++) {
fout << docs[i]->words[j]+1 << " ";
}
fout << endl;
m++;
}
assert(m == M);
fout.close();
return 0;
}
int dataset::read_newdata(string dfile, string wordmapfile) {
mapword2id word2id;
map<int, int> id2_id;
read_wordmap(wordmapfile, &word2id);
if (word2id.size() <= 0) {
cout << "No word map available!" << endl;
return 1;
}
ifstream fin;
fin.open(dfile.c_str(), ifstream::in);
if (!fin.is_open()) {
cout << "Cannot open file " << dfile << " to read!" << endl;
return 1;
}
mapword2id::iterator it;
map<int, int>::iterator _it;
string line;
// get number of new documents
getline(fin, line);
M = atoi(line.c_str());
if (M <= 0) {
cout << "No document available!" << endl;
return 1;
}
// allocate memory for corpus
if (docs) {
deallocate();
} else {
docs = new document*[M];
}
_docs = new document*[M];
// set number of words to zero
V = 0;
strtokenizer strtok;
for (int i = 0; i < M; i++) {
getline(fin, line);
strtok.split(line, " \t\r\n",false);
int length = strtok.count_tokens();
vector<int> doc;
vector<int> _doc;
for (int j = 0; j < length; j++) {
it = word2id.find(strtok.token(j));
if (it == word2id.end()) {
// word not found, i.e., word unseen in training data
// do anything? (future decision)
} else {
int _id;
_it = id2_id.find(it->second);
if (_it == id2_id.end()) {
_id = id2_id.size();
id2_id.insert(pair<int, int>(it->second, _id));
_id2id.insert(pair<int, int>(_id, it->second));
} else {
_id = _it->second;
}
doc.push_back(it->second);
_doc.push_back(_id);
}
}
// allocate memory for new doc
document * pdoc = new document(doc);
document * _pdoc = new document(_doc);
// add new doc
add_doc(pdoc, i);
_add_doc(_pdoc, i);
strtok.clear();
}
fin.close();
// update number of new words
V = id2_id.size();
return 0;
}
int dataset::read_newdata_withrawstrs(string dfile, string wordmapfile) {
mapword2id word2id;
map<int, int> id2_id;
read_wordmap(wordmapfile, &word2id);
if (word2id.size() <= 0) {
cout << "No word map available!" << endl;
return 1;
}
ifstream fin;
fin.open(dfile.c_str(), ifstream::in);
if (!fin.is_open()) {
cout << "Cannot open file " << dfile << " to read!" << endl;
return 1;
}
mapword2id::iterator it;
map<int, int>::iterator _it;
string line;
// get number of new documents
getline(fin, line);
M = atoi(line.c_str());
if (M <= 0) {
cout << "No document available!" << endl;
return 1;
}
// allocate memory for corpus
if (docs) {
deallocate();
} else {
docs = new document*[M];
}
_docs = new document*[M];
// set number of words to zero
V = 0;
strtokenizer strtok;
for (int i = 0; i < M; i++) {
getline(fin, line);
strtok.split(line, " \t\r\n", false);
int length = strtok.count_tokens();
vector<int> doc;
vector<int> _doc;
for (int j = 0; j < length - 1; j++) {
it = word2id.find(strtok.token(j));
if (it == word2id.end()) {
// word not found, i.e., word unseen in training data
// do anything? (future decision)
} else {
int _id;
_it = id2_id.find(it->second);
if (_it == id2_id.end()) {
_id = id2_id.size();
id2_id.insert(pair<int, int>(it->second, _id));
_id2id.insert(pair<int, int>(_id, it->second));
} else {
_id = _it->second;
}
doc.push_back(it->second);
_doc.push_back(_id);
}
}
// allocate memory for new doc
document * pdoc = new document(doc, line);
document * _pdoc = new document(_doc, line);
// add new doc
add_doc(pdoc, i);
_add_doc(_pdoc, i);
strtok.clear();
}
fin.close();
// update number of new words
V = id2_id.size();
return 0;
}