forked from lnthach/SAX-SEQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseql_classify.cpp
303 lines (242 loc) · 7.35 KB
/
seql_classify.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
/*
* seql_classify.cpp
*
* Created on: 19 Jul 2016
* Author: thachln
*/
/*
* seql_classify.h
*
* Created on: 7 Jul 2016
* Author: thachln
*/
#include <limits>
#include <vector>
#include <string>
#include <map>
#include "mmap.h"
#include <algorithm>
#include <cstdio>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <iterator>
#include <cmath>
//#include "common.h"
#include "common_string_symbol.h"
#include "darts.h"
#include "sys/time.h"
#include "seql_classify.h"
static inline char *read_ptr (char **ptr, size_t size)
{
char *r = *ptr;
*ptr += size;
return r;
}
template <class T> static inline void read_static (char **ptr, T& value)
{
char *r = read_ptr (ptr, sizeof (T));
memcpy (&value, r, sizeof (T));
}
template <typename T1, typename T2>
struct pair_2nd_cmp_alt: public std::binary_function<bool, T1, T2> {
bool operator () (const std::pair <T1, T2>& x1, const std::pair<T1, T2> &x2)
{
return x1.second > x2.second;
}
};
void SEQLClassifier::project (std::string prefix,
unsigned int pos,
size_t trie_pos,
size_t str_pos,
bool token_type)
{
if (pos == doc.size() - 1) return;
// Check traversal with both the next actual unigram in the doc and the wildcard *.
string next_unigrams[2];
next_unigrams[0] = doc[pos + 1].key();
next_unigrams[1] = "*";
for (int i = 0; i < 2; ++i) {
string next_unigram = next_unigrams[i];
std::string item;
if (!token_type) { //word-level token
item = prefix + " " + next_unigram;
} else { // char-level token
item = prefix + next_unigram;
}
//cout << "\nitem: " << item.c_str();
size_t new_trie_pos = trie_pos;
size_t new_str_pos = str_pos;
int id = da.traverse (item.c_str(), new_trie_pos, new_str_pos);
//cout <<"\nid: " << id;
//if (id == -2) return;
if (id == -2) {
if (i == 0) continue;
else return;
}
if (id >= 0) {
if (userule) {
//cout << "\nnew rule: " << item;
rules.insert (std::make_pair (item, alpha[id]));
rules_and_ids.insert (std::make_pair (item, id));
}
result.push_back (id);
}
project (item, pos + 1, new_trie_pos, new_str_pos, token_type);
}
}
double SEQLClassifier::getBias() {
return bias;
}
int SEQLClassifier::getOOVDocs() {
return oov_docs;
}
void SEQLClassifier::setRule(bool t)
{
userule = t;
}
bool SEQLClassifier::open (const char *file, double threshold)
{
if (! mmap.open (file)) return false;
char *ptr = mmap.begin ();
unsigned int size = 0;
read_static<unsigned int>(&ptr, size);
da.set_array (ptr);
ptr += size;
read_static<double>(&ptr, bias); // this bias from the model file is not used for classif; it is automatically obtained by summing
// up the features of the model and it is used for info only
bias = -threshold; //set bias to minus user-provided-thereshold
alpha = (double *)ptr;
//for(int i = 0; i < 56;i++) std::cout << alpha[i] << std::endl;
return true;
}
bool SEQLClassifier::load_mytrie (const char *file, double threshold){
trie.load_seql_trie(file);
bias = -threshold;
}
// Compute the area under the ROC curve.
double SEQLClassifier::calcROC( std::vector< std::pair<double, int> >& forROC )
{
//std::sort( forROC.begin(), forROC.end() );
double area = 0;
double x=0, xbreak=0;
double y=0, ybreak=0;
double prevscore = - numeric_limits<double>::infinity();
for( vector< pair<double, int> >::reverse_iterator ritr=forROC.rbegin(); ritr!=forROC.rend(); ritr++ )
{
double score = ritr->first;
int label = ritr->second;
//cout << "\nscore: " << score << " label: " << label;
if( score != prevscore ) {
//cout << "\nx: " << x << " xbreak: " << xbreak << " y: " << y << " ybreak: " << ybreak;
area += (x-xbreak)*(y+ybreak)/2.0;
//cout << "\narea: " << area;
xbreak = x;
ybreak = y;
prevscore = score;
}
if( label > 0) y ++;
else x ++;
}
area += (x-xbreak)*(y+ybreak)/2.0; //the last bin
if( 0==y || x==0 ) area = 0.0; // degenerate case
else area = 100.0 * area /( x*y );
//cout << "\narea: " << area;
return area;
}
// Compute the area under the ROC50 curve.
// Fixes the number of negatives to 50.
// Stop computing curve after seeing 50 negatives.
double SEQLClassifier::calcROC50( std::vector< std::pair<double, int> >& forROC )
{
//std::sort( forROC.begin(), forROC.end() );
double area50 = 0;
double x=0, xbreak=0;
double y=0, ybreak=0;
double prevscore = - numeric_limits<double>::infinity();
for( vector< pair<double, int> >::reverse_iterator ritr=forROC.rbegin(); ritr!=forROC.rend(); ritr++ )
{
double score = ritr->first;
int label = ritr->second;
if( score != prevscore && x < 50) {
area50 += (x-xbreak)*(y+ybreak)/2.0;
xbreak = x;
ybreak = y;
prevscore = score;
}
if( label > 0) y ++;
else if (x < 50) x ++;
}
area50 += (x-xbreak)*(y+ybreak)/2.0; //the last bin
if( 0==y || x==0 ) area50 = 0.0; // degenerate case
else area50 = 100.0 * area50 /( 50*y );
return area50;
}
double SEQLClassifier::classify (const char *line, bool token_type)
{
result.clear ();
doc.clear ();
rules.clear ();
double r = bias;
// Prepare instance as a vector of string_symbol
str2node (line, doc, token_type);
for (unsigned int i = 0; i < doc.size(); ++i) {
std::string item = doc[i].key();
int id;
da.exactMatchSearch (item.c_str(), id);
//int id = da.exactMatchSearch (doc[i].key().c_str());
if (id == -2) continue;
if (id >= 0) {
if (userule) {
rules.insert (std::make_pair (doc[i].key(), alpha[id]));
rules_and_ids.insert (std::make_pair (doc[i].key(), id));
}
result.push_back (id);
}
project (doc[i].key(), i, 0, 0, token_type);
}
std::sort (result.begin(), result.end());
// Binary frequencies, erase the duplicate feature ids, features count only once.
result.erase (std::unique (result.begin(), result.end()), result.end());
if (result.size() == 0) {
if (userule)
cout << "\n Test doc out of vocabulary\n";
oov_docs++;
}
for (unsigned int i = 0; i < result.size(); ++i) r += alpha[result[i]];
//std::cout << "BIAS " << r << std::endl;
return r;
}
double SEQLClassifier::classify_with_mytrie(const char *line,double max_distance){
result.clear ();
doc.clear ();
rules.clear ();
double r = bias + trie.naive_search(line,max_distance, result);
//std::cout << "BIAS " << r << std::endl;
// Prepare instance as a vector of string_symbol
// str2node (line, doc, token_type);
//trie.naive_search(line,0.0, result);
return r;
}
std::ostream &SEQLClassifier::printRules (std::ostream &os)
{
std::vector <std::pair <std::string, double> > tmp;
for (std::map <std::string, double>::iterator it = rules.begin();
it != rules.end(); ++it)
tmp.push_back (std::make_pair (it->first, it->second));
std::sort (tmp.begin(), tmp.end(), pair_2nd_cmp_alt<std::string, double>());
os << "\nrule: " << bias << " __DFAULT__" << std::endl;
// for (std::vector <std::pair <std::string, double> >::iterator it = tmp.begin();
// it != tmp.end(); ++it)
for (std::map <std::string, double>::iterator it = rules.begin();
it != rules.end(); ++it)
//os << "rule: " << rules_and_ids[it->first] << " " << it->second << " " << it->first << std::endl;
os << "rule: " << it->first << " " << it->second << std::endl;
return os;
}
std::ostream &SEQLClassifier::printIds (std::ostream &os) {
for (std::map <std::string, int>::iterator it = rules_and_ids.begin(); it != rules_and_ids.end(); ++it)
os << (it->second + 1) << ":1.0 ";
os << "\n";
return os;
}