forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.cpp
91 lines (75 loc) · 1.74 KB
/
predict.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
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "maxent.h"
using namespace std;
using namespace maxent;
vector<string> split(const string & line)
{
vector<string> vs;
istringstream is(line);
string w;
while (is >> w)
{
vs.push_back(w);
}
return vs;
}
void validate(const ME_Model & model,
const string & input_file_name,
const string & output_file_name)
{
ifstream ifile(input_file_name.c_str());
ofstream ofile(output_file_name.c_str());
if (!ifile)
{
cerr << "error: cannot open " << input_file_name << endl;
exit(1);
}
if (!ofile)
{
cerr << "error: cannot open " << output_file_name << endl;
exit(1);
}
int n_correct = 0;
int n_total = 0;
string line;
while (getline(ifile, line))
{
vector<string> vs = split(line);
ME_Sample mes(vs, true);
model.predict(mes);
ofile << mes.label << endl;
if (mes.label == vs[0]) n_correct++;
n_total++;
}
double accuracy = (double)n_correct / n_total;
cout << "accuracy = " << n_correct << " / " << n_total
<< " = " << accuracy << endl;
}
void exit_with_help()
{
cerr << "Usage: test_exe model_file input_file output_file" << endl;
exit(1);
}
int main(int argc, char** argv)
{
/*
* Params: model_file_name, input_file_name, output_file_name
*
*/
if (argc < 4)
{
exit_with_help();
}
string model_path = argv[1];
string input_path = argv[2];
string output_path = argv[3];
ME_Model m;
m.load(model_path);
validate(m, input_path, output_path);
return 0;
}