-
Notifications
You must be signed in to change notification settings - Fork 3
/
seql_mkmodel.cpp
141 lines (112 loc) · 3.87 KB
/
seql_mkmodel.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
/*
* Authors: Georgiana Ifrim (georgiana.ifrim@gmail.com)
* Thach Le Nguyen (thach.lenguyen@insight-centre.org)
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation.
*
*/
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <map>
#include "unistd.h"
#include "darts.h"
#include "common.h"
#include "seql_mkmodel.h"
//#define OPT " [-i model_file] [-o binary_model_file] [-O predictors_file]"
template <typename T1, typename T2>
struct pair_2nd_cmp: 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;
}
};
int mkmodel (std::string file, std::string index, std::string ofile)
{
std::istream *is;
if (file == "-") is = &std::cin;
else is = new std::ifstream (file.c_str());
if (! *is) {
std::cerr << "Cannot Open: " << file << std::endl;
return -1;
}
std::vector <Darts::DoubleArray::key_type *> ary;
std::vector <std::pair<const char *, double> > ary2;
std::vector <double> alpha;
std::map<std::string, double> rules;
char buf[8192];
char *column[2];
double bias = 0.0;
double alpha_sum = 0.0;
double l1_norm = 0.0;
double l2_norm = 0.0;
while (is->getline (buf, 8192)) {
if (buf[strlen(buf) - 1] == '\r') {
buf[strlen(buf) - 1] = '\0';
}
//cout << "\nline:" << no_cr_line;
//cout.flush();
if (2 != tokenize (buf, "\t ", column, 2)) {
std::cerr << "FATAL: Format Error: " << buf << std::endl;
return -1;
}
// Ignore rules containing only 1 character.
//if (strlen(column[1]) <= 1) continue;
double a = atof (column[0]);
bias -= a;
alpha_sum += std::abs (a);
rules[column[1]] += 2 * a;
}
bias /= alpha_sum;
//bias = 0;
l1_norm = alpha_sum;
for (std::map<std::string, double>::iterator it = rules.begin(); it != rules.end(); ++it) {
double a = it->second / alpha_sum;
l2_norm += pow(it->second, 2);
ary2.push_back (std::make_pair (it->first.c_str(), a));
ary.push_back ((Darts::DoubleArray::key_type *)it->first.c_str());
alpha.push_back (a);
}
l2_norm = pow(l2_norm, 0.5);
//std::cout << "Total: " << alpha.size() << " rule(s)" << std::endl;
//std::cout << "l1_norm: " << l1_norm << ", l2_norm: " << l2_norm << std::endl;
if (ary.empty()) {
std::cerr << "FATAL: no feature is added" << std::endl;
return -1;
}
if (file != "-") delete is;
Darts::DoubleArray da;
if (da.build (ary.size(), &ary[0], 0, 0, 0) != 0) {
std::cerr << "Error: cannot build double array " << file << std::endl;
return -1;
}
std::ofstream ofs (index.c_str(), std::ios::binary|std::ios::out);
if (!ofs) {
std::cerr << "Error: cannot open " << index << std::endl;
return -1;
}
unsigned int s = da.size() * da.unit_size();
ofs.write ((char *)&s, sizeof (unsigned));
ofs.write ((char *)da.array (), s);
ofs.write ((char *)&bias, sizeof (double));
ofs.write ((char *)&alpha[0], sizeof (double) * alpha.size());
ofs.close ();
if (! ary2.empty() && ! ofile.empty()) {
std::ofstream ofs2 (ofile.c_str());
if (! ofs2) {
std::cerr << "Cannot Open: " << ofile << std::endl;
return -1;
}
ofs2.precision (24);
ofs2 << bias << std::endl;
std::sort (ary2.begin(), ary2.end(), pair_2nd_cmp <const char*, double>());
for (unsigned int i = 0; i < ary2.size (); ++i) ofs2 << ary2[i].second << " " << ary2[i].first << std::endl;
ofs2.close();
}
return 0;
}