-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
70 lines (54 loc) · 1.27 KB
/
dictionary.cpp
File metadata and controls
70 lines (54 loc) · 1.27 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
#include "dictionary.h"
#include "split.h"
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
#include <sstream>
#include <utility>
namespace timlan {
bool dictionary::load_words(const std::string &filename)
{
std::fstream file{filename};
file >> *this;
return true;
}
std::optional<word> dictionary::find_word(const std::string& input)
{
for (const auto &word : words_) {
if (input == word.timlan_)
return {word};
}
return {};
}
std::istream &operator>>(std::istream &is, dictionary &dict)
{
dict.words_.clear();
std::string line_str{
std::istreambuf_iterator<char>{is},
std::istreambuf_iterator<char>{}};
std::vector<std::string> lines{split(line_str, "<br />")};
for (const auto &word_line : lines) {
if (word_line == "")
continue;
try {
word new_word{word_line};
dict.words_.emplace_back(std::move(new_word));
}
catch (word_format_exception) {
std::cerr << "Failed to load line " << word_line << "\n";
is.setstate(std::ios::failbit);
}
}
return is;
}
std::ostream &operator<<(std::ostream &os, const dictionary &dict)
{
os << "START OF DICT\n";
for (const auto &word : dict.words_) {
os << " " << word << "\n";
}
os << "END OF DICT";
return os;
}
}