-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
61 lines (51 loc) · 1.76 KB
/
Copy pathdictionary.cpp
File metadata and controls
61 lines (51 loc) · 1.76 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
#include "dictionary.h"
#include <fstream>
#include <sstream>
#include <format>
#include <string>
std::vector<std::basic_string<char>> dictionary::search(const std::string& toTranslate) {
auto opt = tree.searchNode(toTranslate);
return (opt.has_value() ? opt.value()->value :
throw std::runtime_error(std::format("Not found translation for {}", toTranslate)));
}
void dictionary::add(const std::string& word, const std::vector<std::string>& translations) {
const auto nodeOpt = tree.searchNode(word);
if (nodeOpt.has_value()) {
tree.compute(nodeOpt.value(),
[&translations](const auto& k, auto& v)-> std::vector<std::string> {
for (const auto& t: translations) {
v.emplace_back(t);
}
return v;
});
} else {
tree.insert(word, translations);
}
}
void dictionary::remove(const std::string& key) {
tree.deleteKey(key);
}
void dictionary::loadDictionary() {
std::ifstream dictFile(pathToDict, std::ios::in);
if ( !dictFile.is_open()) {
throw std::runtime_error(std::format("Can not open file with name {}", pathToDict));
}
std::string str;
while (std::getline(dictFile, str)) {
auto splitted = split(str, ':');
add(splitted[0], { splitted[1] });
}
}
std::vector<std::string> dictionary::split(const std::string& str, char separator) {
std::stringstream ss(str);
std::vector<std::string> tokens;
std::string token;
while (std::getline(ss, token, separator)) {
tokens.push_back(token);
}
return tokens;
}
std::ostream &operator<<(std::ostream& os, const dictionary& dictionary) {
os << dictionary.tree;
return os;
}