-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_assemble_model.cpp
94 lines (75 loc) · 2.41 KB
/
mpi_assemble_model.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
#include <fstream>
#include <iostream>
#include "common.h"
#include "feature_index.h"
#include "tagger.h"
using namespace CRFPP;
bool readFile(const char *template_file, const char *train_file,
EncoderFeatureIndex *index);
int main(int argc, char *argv[]) {
if (5 != argc) {
std::cerr << "usage: " << argv[0] << " template_file train_file param_file model_file\n";
return -1;
}
EncoderFeatureIndex feature_index;
// read template/train file
if (!readFile(argv[1], argv[2], &feature_index)) {
std::cerr << "Read file [" << argv[1] << "] failed!\n";
return -1;
}
// read param file
std::ifstream ifs(argv[3]);
if (!ifs.is_open()) {
std::cerr << "Open param file [" << argv[3] << "] failed!\n";
return -1;
}
double param = 0.0;
std::vector<double> alpha;
while (ifs >> param) {
alpha.push_back(param);
}
ifs.close();
feature_index.set_alpha(&alpha[0]);
if (!feature_index.save(argv[4], true)) {
std::cerr << "Save assembled crf model failed!\n";
return -1;
}
return 0;
}
bool readFile(const char *template_file, const char *train_file,
EncoderFeatureIndex *feature_index) {
Allocator allocator;
std::vector<TaggerImpl* > x;
whatlog what_; // for CHECK_FALSE
#define WHAT_ERROR(msg) do { \
for (std::vector<TaggerImpl *>::iterator it = x.begin(); \
it != x.end(); ++it) \
delete *it; \
std::cerr << msg << std::endl; \
return false; } while (0)
CHECK_FALSE(feature_index->open(template_file, train_file))
<< feature_index->what();
{
std::ifstream ifs(train_file);
CHECK_FALSE(ifs) << "cannot open: " << train_file;
while (ifs) {
TaggerImpl *_x = new TaggerImpl();
_x->open(feature_index, &allocator);
if (!_x->read(&ifs) || !_x->shrink()) {
WHAT_ERROR(_x->what());
}
if (!_x->empty()) {
x.push_back(_x);
} else {
delete _x;
continue;
}
}
ifs.close();
}
for (std::vector<TaggerImpl *>::iterator it = x.begin();
it != x.end(); ++it) {
delete *it;
}
return true;
}