Repository navigation
Expand file tree
/
Copy pathFeatureExtractor.cpp
More file actions
259 lines (200 loc) · 7.39 KB
/
Copy pathFeatureExtractor.cpp
File metadata and controls
259 lines (200 loc) · 7.39 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "FeatureExtractor.h"
#include <cctype>
#include <cstdint>
enum CharType {
ALPHA,
DIGIT,
DELIMITER
};
inline CharType get_char_type(char c) {
if (c >= '0' && c <= '9') {
return DIGIT;
}
if (c == '.' || c == '/' || c == '?' || c == '=' ||
c == '&' || c == '-' || c == '_' || c == ':' || c == '%') {
return DELIMITER;
}
return ALPHA;
}
std::vector<int> extract_single_token_hashes(const std::string& text, int num_bins) {
std::vector<int> active_bins;
uint32_t current_hash = 0;
int token_length = 0;
CharType current_type = DELIMITER;
for (char c : text) {
CharType type = get_char_type(c);
if (type == DELIMITER) {
if (token_length > 0) {
active_bins.push_back(current_hash % num_bins);
current_hash = 0;
token_length = 0;
}
current_type = DELIMITER;
} else {
if (token_length > 0 && type != current_type) {
active_bins.push_back(current_hash % num_bins);
current_hash = 0;
token_length = 0;
}
current_hash = current_hash * 31 + static_cast<uint8_t>(c);
token_length++;
current_type = type;
}
}
if (token_length > 0) {
active_bins.push_back(current_hash % num_bins);
}
return active_bins;
}
py::tuple extract_token_hashes(const std::vector<std::string>& strings, int num_bins) {
std::vector<float> data;
std::vector<int> indices;
std::vector<int> indptr;
indptr.push_back(0);
for (const auto& s : strings) {
uint32_t current_hash = 0;
int token_length = 0;
CharType current_type = DELIMITER;
for (char c : s) {
CharType type = get_char_type(c);
if (type == DELIMITER) {
if (token_length > 0) {
indices.push_back(current_hash % num_bins);
data.push_back(1.0f);
current_hash = 0;
token_length = 0;
}
current_type = DELIMITER;
} else {
if (token_length > 0 && type != current_type) {
indices.push_back(current_hash % num_bins);
data.push_back(1.0f);
current_hash = 0;
token_length = 0;
}
current_hash = current_hash * 31 + static_cast<uint8_t>(c);
token_length++;
current_type = type;
}
}
if (token_length > 0) {
indices.push_back(current_hash % num_bins);
data.push_back(1.0f);
}
indptr.push_back(indices.size());
}
auto py_data = py::array_t<float>(data.size());
std::memcpy(py_data.mutable_data(), data.data(), data.size() * sizeof(float));
auto py_indices = py::array_t<int>(indices.size());
std::memcpy(py_indices.mutable_data(), indices.data(), indices.size() * sizeof(int));
auto py_indptr = py::array_t<int>(indptr.size());
std::memcpy(py_indptr.mutable_data(), indptr.data(), indptr.size() * sizeof(int));
return py::make_tuple(py_data, py_indices, py_indptr);
}
std::vector<int> extract_single_sparse_ngrams(const std::string& text, int num_bins) {
std::vector<int> active_bins;
size_t s_len = text.length();
if (s_len >= 3) {
for (size_t i = 0; i <= s_len - 3; i++) {
uint32_t h = (static_cast<uint8_t>(text[i]) * 961) +
(static_cast<uint8_t>(text[i+1]) * 31) +
static_cast<uint8_t>(text[i+2]);
active_bins.push_back(h % num_bins);
}
}
return active_bins;
}
py::tuple extract_sparse_ngrams(const std::vector<std::string>& strings, int num_bins) {
std::vector<float> data;
std::vector<int> indices;
std::vector<int> indptr;
indptr.push_back(0);
for (const auto& s : strings) {
size_t s_len = s.length();
if (s_len >= 3) {
for (size_t i = 0; i <= s_len - 3; i++) {
uint32_t h = (static_cast<uint8_t>(s[i]) * 961) +
(static_cast<uint8_t>(s[i+1]) * 31) +
static_cast<uint8_t>(s[i+2]);
indices.push_back(h % num_bins);
data.push_back(1.0f);
}
}
indptr.push_back(indices.size());
}
auto py_data = py::array_t<float>(data.size());
std::memcpy(py_data.mutable_data(), data.data(), data.size() * sizeof(float));
auto py_indices = py::array_t<int>(indices.size());
std::memcpy(py_indices.mutable_data(), indices.data(), indices.size() * sizeof(int));
auto py_indptr = py::array_t<int>(indptr.size());
std::memcpy(py_indptr.mutable_data(), indptr.data(), indptr.size() * sizeof(int));
return py::make_tuple(py_data, py_indices, py_indptr);
}
py::array_t<float> extract_basic_features(const std::vector<std::string>& strings, int vector_size) {
size_t num_samples = strings.size();
auto result = py::array_t<float>({num_samples, static_cast<size_t>(vector_size)});
auto r = result.mutable_unchecked<2>();
for (size_t i = 0; i < num_samples; i++) {
for (int j = 0; j < vector_size; j++) {
r(i, j) = 0.0f;
}
}
for (size_t idx = 0; idx < num_samples; idx++) {
const std::string& s = strings[idx];
size_t s_len = s.length();
if (s_len < 4) {
continue;
}
int digit_count = 0;
int special_count = 0;
int transition_count = 0;
uint32_t h1 = static_cast<uint8_t>(s[0]);
uint32_t h2 = static_cast<uint8_t>(s[1]);
uint32_t h3 = static_cast<uint8_t>(s[2]);
bool last_was_alpha =
std::isalpha(static_cast<unsigned char>(s[2]));
for (size_t i = 3; i < s_len; i++) {
uint32_t val_curr = static_cast<uint8_t>(s[i]);
bool curr_is_alpha =
std::isalpha(static_cast<unsigned char>(s[i]));
if (val_curr >= 48 && val_curr <= 57) {
digit_count++;
}
else if (
val_curr == 45 ||
val_curr == 46 ||
val_curr == 47 ||
val_curr == 63 ||
val_curr == 61 ||
val_curr == 37
) {
special_count++;
}
if (curr_is_alpha != last_was_alpha) {
transition_count++;
}
last_was_alpha = curr_is_alpha;
uint32_t sig3 =
((h2 << 10) ^ (h3 << 5) ^ val_curr) % vector_size;
r(idx, sig3) += 1.0f;
uint32_t sig4 =
((h1 << 15) ^
(h2 << 10) ^
(h3 << 5) ^
val_curr) % vector_size;
r(idx, sig4) += 0.5f;
h1 = h2;
h2 = h3;
h3 = val_curr;
}
float inv_len = 1.0f / static_cast<float>(s_len);
for (int j = 0; j < vector_size; j++) {
r(idx, j) *= inv_len;
}
r(idx, 0) = static_cast<float>(s_len) * 0.01f;
r(idx, 1) = digit_count * inv_len;
r(idx, 2) = special_count * inv_len;
r(idx, 3) = transition_count * inv_len;
}
return result;
}