forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeaturevec.h
117 lines (97 loc) · 2.22 KB
/
featurevec.h
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
#ifndef __LTP_PARSER_FEATURE_VECTOR_H__
#define __LTP_PARSER_FEATURE_VECTOR_H__
#include <iostream>
#include <fstream>
namespace ltp {
namespace parser {
struct FeatureVector {
FeatureVector() : n(0), idx(0), val(0), loff(0) {}
~FeatureVector() {
// clear();
}
int n;
int * idx;
double * val;
int loff;
/*
* clear the FeatureVector
*/
void clear() {
if (idx) {
delete [](idx);
idx = 0;
}
if (val) {
delete [](val);
val = 0;
}
}
};
struct FeatureVectorDB : public FeatureVector {
FeatureVectorDB() : offset(-1) {}
~FeatureVectorDB() {
}
long long offset;
/*
* Write the feature vector to file, return offset of the featurevec
* This method is discarded because of the low performance of file
* operation.
*
* @param[in] ofs the output filestream
* @return int offset of the feature.
*/
long long write(std::ostream & ofs) {
//
if (n <= 0 || idx == 0) {
offset = -1;
return -1;
}
char ch = (val == 0 ? 0 : 1);
offset = ofs.tellp();
ofs.write(&ch, 1);
ofs.write(reinterpret_cast<const char *>(&n), sizeof(int));
ofs.write(reinterpret_cast<const char *>(idx), sizeof(int) * n);
if (val) {
ofs.write(reinterpret_cast<const char *>(val), sizeof(double) * n);
}
return offset;
}
/*
* Read the feature vector from filestream, This method is discarded
* because of the low performance of file operation.
*
* @param[in] ifs the input filestream
*/
int read(std::istream & ifs) {
if (offset < 0) {
return -1;
}
ifs.seekg(offset);
char ch = 0;
ifs.read(&ch, 1);
ifs.read(reinterpret_cast<char *>(&n), sizeof(int));
idx = new int[n];
ifs.read(reinterpret_cast<char *>(idx), sizeof(int) * n);
if (ch) {
val = new double[n];
ifs.read(reinterpret_cast<char *>(val), sizeof(double) * n);
}
return 0;
}
/*
* free memory of feature vector.
*/
void nice() {
if (idx) {
delete [](idx);
idx = 0;
}
if (val) {
delete [](val);
val = 0;
}
}
};
} // end for namespace parser
} // end for namespace ltp
#endif // end for __LTP_PARSER_FEATURE_VECTOR_H__