-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset.cpp
More file actions
64 lines (50 loc) · 1.45 KB
/
Copy pathdataset.cpp
File metadata and controls
64 lines (50 loc) · 1.45 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
#include "dataset.h"
#include <fstream>
#include <set>
std::string read_txt(const std::string &path) {
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return "";
}
std::string text = "";
std::string line;
while (std::getline(file, line)) {
text += line + "\n";
}
file.close();
return text;
}
Dataset::Dataset(std::string &txt_path, size_t block_size) {
Dataset::raw_data = read_txt(txt_path);
Dataset::block_size = block_size;
std::set<char> char_set(raw_data.begin(), raw_data.end());
Dataset::vocab = std::string(char_set.begin(), char_set.end());
auto encoded = Dataset::encode(raw_data);
size_t n = raw_data.length()*0.9;
Dataset::train_data = std::vector<size_t>(encoded.begin(), encoded.begin() + n);
Dataset::val_data = std::vector<size_t>(encoded.begin() + n, encoded.end());
}
size_t Dataset::get_vocab_size(){
return Dataset::vocab.size();
}
std::vector<size_t> Dataset::encode(const std::string &text) {
std::vector<size_t> idxs;
for (auto &character : text) {
idxs.emplace_back(Dataset::vocab.find(character));
}
return idxs;
}
std::string Dataset::decode(const std::vector<size_t> &idxs) {
std::string text = "";
for (auto &idx : idxs) {
text += Dataset::vocab[idx];
}
return text;
}
std::vector<size_t> Dataset::get_train(){
return Dataset::train_data;
}
std::vector<size_t> Dataset::get_val(){
return Dataset::val_data;
}