Debian Codegraph @

data/dictionary/DictionaryVariantRevPhrasesTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Validate reverse variant phrase dictionary coverage.
5 */
6
7 #include "gtest/gtest.h"
8
9 #include <fstream>
10 #include <sstream>
11 #include <string>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <vector>
15
16 #include "src/UTF8Util.hpp"
17 #include "tools/cpp/runfiles/runfiles.h"
18
19 using bazel::tools::cpp::runfiles::Runfiles;
20
21 namespace opencc {
22 namespace {
23
24 std::vector<std::string> SplitValues(const std::string& values) {
25 std::vector<std::string> split;
26 size_t start = 0;
27 while (start <= values.size()) {
28 const size_t end = values.find(' ', start);
29 split.push_back(values.substr(start, end - start));
30 if (end == std::string::npos) {
31 break;
32 }
33 start = end + 1;
34 }
35 return split;
36 }
37
38 bool IsDictionaryEntry(const std::string& line) {
39 return !line.empty() && line[0] != '#';
40 }
41
42 std::vector<std::unordered_set<std::string>> LoadMultiVariantSets(
43 const std::string& path) {
44 std::ifstream stream(path);
45 EXPECT_TRUE(stream.is_open()) << path;
46
47 std::vector<std::unordered_set<std::string>> variantSets;
48 std::string line;
49 while (std::getline(stream, line)) {
50 if (!IsDictionaryEntry(line)) {
51 continue;
52 }
53
54 const size_t firstTab = line.find('\t');
55 if (firstTab == std::string::npos) {
56 continue;
57 }
58 const size_t secondTab = line.find('\t', firstTab + 1);
59 const std::string variantsText =
60 line.substr(firstTab + 1, secondTab - firstTab - 1);
61 std::vector<std::string> variants = SplitValues(variantsText);
62 if (variants.size() <= 1) {
63 continue;
64 }
65
66 variantSets.emplace_back(variants.begin(), variants.end());
67 }
68 return variantSets;
69 }
70
71 bool IsInSameMultiVariantSet(
72 const std::vector<std::unordered_set<std::string>>& variantSets,
73 const std::string& first, const std::string& second) {
74 for (const auto& variantSet : variantSets) {
75 if (variantSet.count(first) != 0 && variantSet.count(second) != 0) {
76 return true;
77 }
78 }
79 return false;
80 }
81
82 std::unordered_map<std::string, std::string> LoadPreferredValues(
83 const std::string& path) {
84 std::ifstream stream(path);
85 EXPECT_TRUE(stream.is_open()) << path;
86
87 std::unordered_map<std::string, std::string> preferredValues;
88 std::string line;
89 while (std::getline(stream, line)) {
90 if (!IsDictionaryEntry(line)) {
91 continue;
92 }
93
94 const size_t tab = line.find('\t');
95 if (tab == std::string::npos) {
96 ADD_FAILURE() << line;
97 continue;
98 }
99 const std::string key = line.substr(0, tab);
100 const std::vector<std::string> values = SplitValues(line.substr(tab + 1));
101 if (!values.empty()) {
102 preferredValues[key] = values.front();
103 }
104 }
105 return preferredValues;
106 }
107
108 std::unordered_set<std::string> LoadReverseExceptionCharacters(
109 const std::string& variantsPath, const std::string& variantsRevPath,
110 const std::string& stCharactersPath) {
111 const std::vector<std::unordered_set<std::string>> variantSets =
112 LoadMultiVariantSets(stCharactersPath);
113 EXPECT_FALSE(variantSets.empty());
114 const std::unordered_map<std::string, std::string> reversePreferredValues =
115 LoadPreferredValues(variantsRevPath);
116
117 std::ifstream stream(variantsPath);
118 EXPECT_TRUE(stream.is_open()) << variantsPath;
119
120 std::unordered_set<std::string> exceptionChars;
121 std::string line;
122 while (std::getline(stream, line)) {
123 if (!IsDictionaryEntry(line)) {
124 continue;
125 }
126
127 const size_t tab = line.find('\t');
128 if (tab == std::string::npos) {
129 ADD_FAILURE() << line;
130 continue;
131 }
132 const std::string key = line.substr(0, tab);
133 const std::string values = line.substr(tab + 1);
134 for (const std::string& value : SplitValues(values)) {
135 if (key == value) {
136 continue;
137 }
138 const auto reversePreferredValue = reversePreferredValues.find(value);
139 if (reversePreferredValue != reversePreferredValues.end() &&
140 reversePreferredValue->second == value) {
141 continue;
142 }
143 if (IsInSameMultiVariantSet(variantSets, key, value)) {
144 exceptionChars.insert(value);
145 }
146 }
147 }
148 return exceptionChars;
149 }
150
151 bool ContainsAny(const std::string& text,
152 const std::unordered_set<std::string>& needles) {
153 for (const std::string& needle : needles) {
154 if (text.find(needle) != std::string::npos) {
155 return true;
156 }
157 }
158 return false;
159 }
160
161 std::unordered_set<std::string> LoadExpectedPhraseExceptions(
162 const std::string& stPhrasesPath,
163 const std::unordered_set<std::string>& exceptionChars) {
164 std::ifstream stream(stPhrasesPath);
165 EXPECT_TRUE(stream.is_open()) << stPhrasesPath;
166
167 std::unordered_set<std::string> expected;
168 std::string line;
169 while (std::getline(stream, line)) {
170 if (!IsDictionaryEntry(line)) {
171 continue;
172 }
173
174 const size_t tab = line.find('\t');
175 if (tab == std::string::npos) {
176 ADD_FAILURE() << line;
177 continue;
178 }
179 const std::string values = line.substr(tab + 1);
180 const std::vector<std::string> valueList = SplitValues(values);
181 if (valueList.empty()) {
182 continue;
183 }
184 const std::string& preferredValue = valueList.front();
185 if (UTF8Util::Length(preferredValue.c_str()) >= 2 &&
186 ContainsAny(preferredValue, exceptionChars)) {
187 expected.insert(preferredValue);
188 }
189 }
190 return expected;
191 }
192
193 std::unordered_set<std::string> LoadIdentityPhraseKeys(
194 const std::string& phrasesPath) {
195 std::ifstream stream(phrasesPath);
196 EXPECT_TRUE(stream.is_open()) << phrasesPath;
197
198 std::unordered_set<std::string> identityKeys;
199 std::string line;
200 while (std::getline(stream, line)) {
201 if (!IsDictionaryEntry(line)) {
202 continue;
203 }
204
205 const size_t tab = line.find('\t');
206 if (tab == std::string::npos) {
207 ADD_FAILURE() << line;
208 continue;
209 }
210 const std::string key = line.substr(0, tab);
211 const std::string values = line.substr(tab + 1);
212 for (const std::string& value : SplitValues(values)) {
213 if (key == value) {
214 identityKeys.insert(key);
215 break;
216 }
217 }
218 }
219 return identityKeys;
220 }
221
222 void ExpectRevPhrasesComplete(const std::string& variantsName) {
223 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
224 ASSERT_NE(nullptr, runfiles);
225
226 const std::string revPhrasesName = variantsName + "RevPhrases.txt";
227 const std::string variantsPath =
228 runfiles->Rlocation("_main/data/dictionary/" + variantsName + ".txt");
229 const std::string variantsRevPath =
230 runfiles->Rlocation("_main/data/dictionary/" + variantsName + "Rev.txt");
231 const std::string phrasesPath = runfiles->Rlocation(
232 "_main/data/dictionary/" + revPhrasesName);
233 const std::string stCharactersPath =
234 runfiles->Rlocation("_main/data/dictionary/STCharacters.txt");
235 const std::string stPhrasesPath =
236 runfiles->Rlocation("_main/data/dictionary/STPhrases.txt");
237
238 const std::unordered_set<std::string> exceptionChars =
239 LoadReverseExceptionCharacters(variantsPath, variantsRevPath,
240 stCharactersPath);
241 ASSERT_FALSE(exceptionChars.empty()) << variantsName;
242
243 const std::unordered_set<std::string> expected =
244 LoadExpectedPhraseExceptions(stPhrasesPath, exceptionChars);
245 const std::unordered_set<std::string> identityKeys =
246 LoadIdentityPhraseKeys(phrasesPath);
247
248 std::ostringstream missing;
249 for (const std::string& phrase : expected) {
250 if (identityKeys.count(phrase) == 0) {
251 missing << phrase << "\n";
252 }
253 }
254
255 EXPECT_TRUE(missing.str().empty())
256 << revPhrasesName << " missing reverse phrase identity exceptions:\n"
257 << missing.str();
258 }
259
260 TEST(DictionaryVariantRevPhrasesTest, HKVariantsAreComplete) {
261 ExpectRevPhrasesComplete("HKVariants");
262 }
263
264 TEST(DictionaryVariantRevPhrasesTest, TWVariantsAreComplete) {
265 ExpectRevPhrasesComplete("TWVariants");
266 }
267
268 } // namespace
269 } // namespace opencc