-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.h
executable file
·157 lines (133 loc) · 3.5 KB
/
dataset.h
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
#ifndef _DATASET_H
#define _DATASET_H
#include <string>
#include <vector>
#include <map>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// map of words/terms [string => int]
typedef map<string, int> mapword2id;
// map of words/terms [int => string]
typedef map<int, string> mapid2word;
class document {
public:
int * words;
string rawstr;
int length;
document() {
words = NULL;
rawstr = "";
length = 0;
}
document(int length) {
this->length = length;
rawstr = "";
words = new int[length];
}
document(int length, int * words) {
this->length = length;
rawstr = "";
this->words = new int[length];
for (int i = 0; i < length; i++) {
this->words[i] = words[i];
}
}
document(int length, int * words, string rawstr) {
this->length = length;
this->rawstr = rawstr;
this->words = new int[length];
for (int i = 0; i < length; i++) {
this->words[i] = words[i];
}
}
document(vector<int> & doc) {
this->length = doc.size();
rawstr = "";
this->words = new int[length];
for (int i = 0; i < length; i++) {
this->words[i] = doc[i];
}
}
document(vector<int> & doc, string rawstr) {
this->length = doc.size();
this->rawstr = rawstr;
this->words = new int[length];
for (int i = 0; i < length; i++) {
this->words[i] = doc[i];
}
}
~document() {
if (words) {
delete words;
}
}
};
class dataset {
public:
document ** docs;
document ** _docs; // used only for inference
map<int, int> _id2id; // also used only for inference
int M; // number of documents
int V; // number of words
dataset() {
docs = NULL;
_docs = NULL;
M = 0;
V = 0;
}
dataset(int M) {
this->M = M;
this->V = 0;
docs = new document*[M];
_docs = NULL;
}
~dataset() {
if (docs) {
for (int i = 0; i < M; i++) {
delete docs[i];
}
}
delete docs;
if (_docs) {
for (int i = 0; i < M; i++) {
delete _docs[i];
}
}
delete _docs;
}
void deallocate() {
if (docs) {
for (int i = 0; i < M; i++) {
delete docs[i];
}
}
delete docs;
docs = NULL;
if (_docs) {
for (int i = 0; i < M; i++) {
delete _docs[i];
}
}
delete _docs;
_docs = NULL;
}
void add_doc(document * doc, int idx) {
if (0 <= idx && idx < M) {
docs[idx] = doc;
}
}
void _add_doc(document * doc, int idx) {
if (0 <= idx && idx < M) {
_docs[idx] = doc;
}
}
static int write_wordmap(string wordmapfile, mapword2id * pword2id);
static int read_wordmap(string wordmapfile, mapword2id * pword2id);
static int read_wordmap(string wordmapfile, mapid2word * pid2word);
int read_trndata(string classfile, string dfile, string wordmapfile, vector<pair<string, int> >& movie_classes);
int read_newdata(string dfile, string wordmapfile);
int read_newdata_withrawstrs(string dfile, string wordmapfile);
int write_all(int M, int V, document ** docs);
};
#endif