This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_vocabulary.cpp
207 lines (169 loc) · 7.22 KB
/
io_vocabulary.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
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
#include "vocabulary.h"
#include "io.h"
#include <fstream>
#include <memory>
#include <cstring>
std::ostream& operator<<(std::ostream& out, const yzw2v::vocab::Token& token) {
for (const auto c : token) {
out << c;
}
return out;
}
void yzw2v::vocab::WriteTSV(const Vocabulary& vocab, const std::string& path) {
Vocabulary::WriteTSVWithFilter(vocab, path, 0);
}
void yzw2v::vocab::WriteTSVWithFilter(const Vocabulary& vocab, const std::string& path,
const uint32_t min_token_freq) {
Vocabulary::WriteTSVWithFilter(vocab, path, min_token_freq);
}
void yzw2v::vocab::Vocabulary::WriteTSVWithFilter(const Vocabulary& vocab, const std::string& path,
const uint32_t min_token_freq) {
std::ofstream out{path, std::ios::out};
if (!out) {
throw std::runtime_error{"failed to open file for writing"};
}
for (const auto& info : vocab.tokens_) {
if (info.count < min_token_freq) {
continue;
}
out << info.count
<< '\t' << info.token
<< '\n';
}
}
static const char VOCABULARY_MAGIC[] = {"YZW2V_VOCABULARY (ノ◕ヮ◕)ノ*:・゚✧"};
static constexpr size_t VOCABULARY_MAGIC_SIZE = sizeof(VOCABULARY_MAGIC)
/ sizeof(VOCABULARY_MAGIC[0]);
static uint32_t IntHash(const uint32_t value) noexcept {
// Knuth's Multiplicative Method
return value * uint32_t{2654435761};
}
void yzw2v::vocab::WriteBinary(const Vocabulary& vocab, const std::string& path) {
Vocabulary::WriteBinaryWithFilter(vocab, path, 0);
}
void yzw2v::vocab::WriteBinaryWithFilter(const Vocabulary& vocab, const std::string& path,
const uint32_t min_token_freq) {
Vocabulary::WriteBinaryWithFilter(vocab, path, min_token_freq);
}
void yzw2v::vocab::Vocabulary::WriteBinaryWithFilter(
const Vocabulary& vocab, const std::string& path, const uint32_t min_token_freq
){
const auto number_of_tokens = [&vocab, min_token_freq]{
if (!min_token_freq) {
return vocab.size();
}
auto res = uint32_t{};
for (const auto& info : vocab.tokens_) {
if (info.count >= min_token_freq) {
++res;
}
}
return res;
}();
std::ofstream out{path, std::ios::binary};
if (!out) {
throw std::runtime_error{"failed to open file for writing"};
}
static constexpr size_t BUFFER_SIZE = 1024 * 1024 * 32; // 32 Mb
io::BinaryBufferedWriteProxy proxy{out, BUFFER_SIZE};
// [VOCABULARY_MAGIC]
proxy.Write(VOCABULARY_MAGIC, VOCABULARY_MAGIC_SIZE);
// [vocab.max_number_of_tokens_, hash(vocab.max_number_of_tokens_)]
proxy.Write(&vocab.max_number_of_tokens_, sizeof(vocab.max_number_of_tokens_));
const auto max_number_of_tokens_hash = IntHash(vocab.max_number_of_tokens_);
proxy.Write(&max_number_of_tokens_hash, sizeof(max_number_of_tokens_hash));
// [vocab.hash_table_size_, hash(vocab.hash_table_size_)]
proxy.Write(&vocab.hash_table_size_, sizeof(vocab.hash_table_size_));
const auto hash_table_size_hash = IntHash(vocab.hash_table_size_);
proxy.Write(&hash_table_size_hash, sizeof(hash_table_size_hash));
// [vocab.tokens_.size(), hash(vocab.tokens_.size())]
proxy.Write(&number_of_tokens, sizeof(number_of_tokens));
const auto number_of_tokens_hash = IntHash(number_of_tokens);
proxy.Write(&number_of_tokens_hash, sizeof(number_of_tokens_hash));
// [count, length(token), token]
for (const auto& info : vocab.tokens_) {
if (info.count < min_token_freq) {
continue;
}
proxy.Write(&info.count, sizeof(info.count));
const auto size = info.token.length();
proxy.Write(&size, sizeof(size));
proxy.Write(info.token.cbegin(), info.token.length());
}
}
yzw2v::vocab::Vocabulary yzw2v::vocab::ReadBinary(const std::string& path) {
Vocabulary res{1};
Vocabulary::ReadBinaryWithFilter(path, 0, res);
return res;
}
void yzw2v::vocab::ReadBinary(const std::string& path, Vocabulary& vocab) {
Vocabulary::ReadBinaryWithFilter(path, 0, vocab);
}
yzw2v::vocab::Vocabulary yzw2v::vocab::ReadBinaryWithFilter(const std::string& path,
const uint32_t min_token_freq) {
Vocabulary res{1};
Vocabulary::ReadBinaryWithFilter(path, min_token_freq, res);
return res;
}
void yzw2v::vocab::ReadBinaryWithFilter(const std::string& path, const uint32_t min_token_freq,
Vocabulary& vocab) {
Vocabulary::ReadBinaryWithFilter(path, min_token_freq, vocab);
}
void yzw2v::vocab::Vocabulary::ReadBinaryWithFilter(const std::string& path,
const uint32_t min_token_freq,
Vocabulary& vocab) {
const auto in_size = io::FileSize(path);
std::ifstream in{path, std::ios::binary};
if (!in) {
throw std::runtime_error{"failed to open file for reading"};
}
static constexpr size_t BUFFER_SIZE = 1024 * 1024 * 32; // 32 Mb
io::BinaryBufferedReadProxy proxy{in, in_size, BUFFER_SIZE};
{
char magic[VOCABULARY_MAGIC_SIZE] = {};
proxy.Read(magic, VOCABULARY_MAGIC_SIZE);
if (std::strncmp(VOCABULARY_MAGIC, magic, VOCABULARY_MAGIC_SIZE)) {
throw std::runtime_error{"magic doesn't match"};
}
}
proxy.Read(&vocab.max_number_of_tokens_, sizeof(vocab.max_number_of_tokens_));
{
auto max_number_of_tokens_hash = uint32_t{};
proxy.Read(&max_number_of_tokens_hash, sizeof(max_number_of_tokens_hash));
if (IntHash(vocab.max_number_of_tokens_) != max_number_of_tokens_hash) {
throw std::runtime_error{"hash(vocab.max_number_of_tokens_) doesn't match"};
}
}
proxy.Read(&vocab.hash_table_size_, sizeof(vocab.hash_table_size_));
{
auto hash_table_size_hash = uint32_t{};
proxy.Read(&hash_table_size_hash, sizeof(hash_table_size_hash));
if (IntHash(vocab.hash_table_size_) != hash_table_size_hash) {
throw std::runtime_error{"hash(vocab.hash_table_size_) does't match"};
}
}
const auto number_of_tokens_to_read = [&proxy]{
auto res = uint32_t{};
proxy.Read(&res, sizeof(res));
auto res_hash = uint32_t{};
proxy.Read(&res_hash, sizeof(res_hash));
if (IntHash(res) != res_hash) {
throw std::runtime_error{"hash(number_of_tokens) doesn't match"};
}
return res;
}();
const std::unique_ptr<char[]> token_buffer{new char[MAX_TOKEN_LENGTH]};
// it can be done in a more efficient manner
vocab = Vocabulary{vocab.max_number_of_tokens_};
for (auto i = uint32_t{}; i < number_of_tokens_to_read; ++i) {
auto count = uint32_t{};
proxy.Read(&count, sizeof(count));
auto length = decltype(yzw2v::vocab::Token{}.length()){};
proxy.Read(&length, sizeof(length));
proxy.Read(token_buffer.get(), length);
if (count >= min_token_freq ) {
const auto id = vocab.Add({token_buffer.get(), length});
vocab.tokens_[id].count = count;
}
}
}