forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeaturespace.cpp
101 lines (79 loc) · 2.16 KB
/
featurespace.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
#include "featurespace.h"
#include "extractor.h"
namespace ltp {
namespace postagger {
FeatureSpace::FeatureSpace(int num_labels) :
_num_labels(num_labels),
_offset(0) {
_num_dicts = Extractor::num_templates();
dicts = new utility::SmartMap<int>[_num_dicts];
}
FeatureSpace::~FeatureSpace(void) {
delete [](dicts);
}
int FeatureSpace::retrieve(int tid, const char * key, bool create) {
int val;
if (dicts[tid].get(key, val)) {
return val;
} else {
if (create) {
val = _offset;
dicts[tid].set(key, val);
++ _offset;
return val;
}
}
return -1;
}
int FeatureSpace::index(int tid, const char * key, int lid) {
int idx = retrieve(tid, key, false);
if (idx < 0) {
return -1;
}
return idx * _num_labels + lid;
}
int FeatureSpace::index(int prev_lid, int lid) {
return _offset * _num_labels + prev_lid * _num_labels + lid;
}
int FeatureSpace::num_features() {
return _offset;
}
int FeatureSpace::dim() {
return _offset * _num_labels + _num_labels * _num_labels;
}
void FeatureSpace::set_num_labels(int num_labels) {
_num_labels = num_labels;
}
void FeatureSpace::dump(std::ostream & ofs) {
char chunk[16];
unsigned sz = _num_dicts;
strncpy(chunk, "featurespace", 16);
ofs.write(chunk, 16);
ofs.write(reinterpret_cast<const char *>(&_offset), sizeof(int));
ofs.write(reinterpret_cast<const char *>(&sz), sizeof(unsigned int));
for (int i = 0; i < _num_dicts; ++ i) {
dicts[i].dump(ofs);
}
}
bool FeatureSpace::load(int num_labels, std::istream & ifs) {
_num_labels = num_labels;
char chunk[16];
unsigned int sz;
ifs.read(chunk, 16);
if (strcmp(chunk, "featurespace")) {
return false;
}
ifs.read(reinterpret_cast<char *>(&_offset), sizeof(int));
ifs.read(reinterpret_cast<char *>(&sz), sizeof(unsigned int));
if (sz != _num_dicts) {
return false;
}
for (unsigned i = 0; i < sz; ++ i) {
if (!dicts[i].load(ifs)) {
return false;
}
}
return true;
}
} // end for namespace postagger
} // end for namespace ltp