-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDictUtil.cpp
More file actions
140 lines (128 loc) · 5.97 KB
/
DictUtil.cpp
File metadata and controls
140 lines (128 loc) · 5.97 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
#include "DictUtil.h"
#include <filesystem>
#include <iostream>
#include <fstream>
#include <sstream>
#include <functional>
#include <vector>
#include <string>
#include "cpp-pinyin/U16Str.h"
namespace Pinyin
{
// Helper function to read and open file
static std::ifstream openFile(const std::filesystem::path &dict_dir) {
#ifdef _WIN32
const std::wstring wdict_dir = dict_dir.wstring();
return std::ifstream(wdict_dir.c_str());
#else
return std::ifstream(dict_dir.c_str());
#endif
}
// Helper function to trim whitespace from a string
static void trim(std::string &str) {
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
}
// Common function for reading lines and processing key-value pairs
template <typename K, typename V, typename KeyFunc, typename ValueFunc>
static bool processFile(std::ifstream &file, std::unordered_map<K, V> &resultMap,
const char &sep1, KeyFunc keyProcessor, ValueFunc valueProcessor) {
if (!file.is_open()) {
std::cerr << "Error: Unable to open file" << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
trim(line);
std::istringstream iss(line);
std::string key, value;
if (std::getline(iss, key, sep1) && std::getline(iss, value)) {
resultMap[keyProcessor(key)] = valueProcessor(value);
}
}
return true;
}
static std::vector<std::string> split(const std::string &s, const std::string &delimiter) {
std::vector<std::string> tokens;
if (delimiter.empty()) {
for (char c : s) {
tokens.emplace_back(1, c);
}
} else {
std::string::size_type start = 0;
std::string::size_type end = s.find(delimiter);
while (end != std::string::npos) {
tokens.push_back(s.substr(start, end - start));
start = end + delimiter.size();
end = s.find(delimiter, start);
}
tokens.push_back(s.substr(start));
}
return tokens;
}
bool loadDict(const std::filesystem::path &dict_dir,
std::unordered_map<char16_t, char16_t> &resultMap, const char &sep1) {
std::ifstream file = openFile(dict_dir);
return processFile(file, resultMap, sep1,
[](const std::string &key) { return utf8strToU16str(key)[0]; },
[](const std::string &value) { return utf8strToU16str(value)[0]; });
}
bool loadDict(const std::filesystem::path &dict_dir,
std::unordered_map<char16_t, std::u16string> &resultMap, const char &sep1) {
std::ifstream file = openFile(dict_dir);
return processFile(file, resultMap, sep1,
[](const std::string &key) { return utf8strToU16str(key)[0]; },
[](const std::string &value) { return utf8strToU16str(value); });
}
bool loadDict(const std::filesystem::path &dict_dir,
std::unordered_map<char16_t, std::vector<std::u16string>> &resultMap, const char &sep1,
const std::string &sep2) {
std::ifstream file = openFile(dict_dir);
return processFile(file, resultMap, sep1,
[](const std::string &key) { return utf8strToU16str(key)[0]; },
[&sep2](const std::string &value)
{
std::vector<std::u16string> u8strlist;
for (const auto &str : split(value, sep2)) {
if (!str.empty())
u8strlist.emplace_back(utf8strToU16str(str));
}
return u8strlist;
});
}
bool loadDict(const std::filesystem::path &dict_dir,
std::unordered_map<std::u16string, std::vector<std::u16string>> &resultMap, const char &sep1,
const std::string &sep2) {
std::ifstream file = openFile(dict_dir);
return processFile(file, resultMap, sep1,
[](const std::string &key) { return utf8strToU16str(key); },
[&sep2](const std::string &value)
{
std::vector<std::u16string> u8strlist;
for (const auto &str : split(value, sep2)) {
if (!str.empty())
u8strlist.emplace_back(utf8strToU16str(str));
}
return u8strlist;
});
}
bool loadAdditionalDict(const std::filesystem::path &dict_dir,
std::unordered_map<std::u16string, std::vector<std::u16string>> &resultMap,
const char &sep1,
const std::string &sep2,
const std::function<std::u16string(const std::u16string &pinyin)> &
converterForDefaultPinyin) {
std::ifstream file = openFile(dict_dir);
return processFile(file, resultMap, sep1,
[](const std::string &key) { return utf8strToU16str(key); },
[&sep2, &converterForDefaultPinyin](const std::string &value)
{
std::vector<std::u16string> u8strlist;
for (const auto &str : split(value, sep2)) {
if (!str.empty())
u8strlist.emplace_back(converterForDefaultPinyin(utf8strToU16str(str)));
}
return u8strlist;
});
}
} // namespace Pinyin