Debian Codegraph @

data/dictionary/DictionaryPhraseSegmentationTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Validate that Taiwan phrase keys are not interrupted by Simplified-to-
5 * Traditional phrase values during s2twp conversion.
6 */
7
8 #include "gtest/gtest.h"
9
10 #include <fstream>
11 #include <sstream>
12 #include <string>
13 #include <unordered_set>
14 #include <vector>
15
16 #include "src/Exception.hpp"
17 #include "src/Lexicon.hpp"
18 #include "src/UTF8Util.hpp"
19 #include "tools/cpp/runfiles/runfiles.h"
20
21 using bazel::tools::cpp::runfiles::Runfiles;
22
23 namespace opencc {
24 namespace {
25
26 static FILE* OpenFile(const std::string& path) {
27 #ifdef _MSC_VER
28 return _wfopen(UTF8Util::GetPlatformString(path).c_str(), L"rb");
29 #else
30 return fopen(UTF8Util::GetPlatformString(path).c_str(), "rb");
31 #endif
32 }
33
34 LexiconPtr LoadLexicon(const std::string& path) {
35 FILE* fp = OpenFile(path);
36 EXPECT_NE(fp, nullptr) << path;
37 if (fp == nullptr) {
38 return LexiconPtr();
39 }
40 return Lexicon::ParseLexiconFromFile(fp);
41 }
42
43 bool HasAtLeastTwoCharacters(const std::string& text) {
44 return UTF8Util::Length(text.c_str()) >= 2;
45 }
46
47 struct STPhraseValue {
48 std::string source;
49 size_t lineNumber;
50 std::string key;
51 std::string value;
52 };
53
54 std::vector<std::string> SplitValues(const std::string& values) {
55 std::vector<std::string> split;
56 size_t start = 0;
57 while (start <= values.size()) {
58 const size_t end = values.find(' ', start);
59 split.push_back(values.substr(start, end - start));
60 if (end == std::string::npos) {
61 break;
62 }
63 start = end + 1;
64 }
65 return split;
66 }
67
68 std::vector<STPhraseValue> LoadSTPhraseValues(const std::string& path,
69 const std::string& source) {
70 std::ifstream stream(path);
71 if (!stream.is_open()) {
72 ADD_FAILURE() << path;
73 return std::vector<STPhraseValue>();
74 }
75
76 std::vector<STPhraseValue> values;
77 std::unordered_set<std::string> seenValues;
78 std::string line;
79 size_t lineNumber = 0;
80 while (std::getline(stream, line)) {
81 ++lineNumber;
82 if (line.empty() || line[0] == '#') {
83 continue;
84 }
85
86 const size_t tab = line.find('\t');
87 if (tab == std::string::npos) {
88 continue;
89 }
90
91 const std::string key = line.substr(0, tab);
92 const std::string valueText = line.substr(tab + 1);
93 for (const std::string& value : SplitValues(valueText)) {
94 if (seenValues.insert(value).second) {
95 values.push_back(STPhraseValue{source, lineNumber, key, value});
96 }
97 }
98 }
99 return values;
100 }
101
102 bool IsCoveredBySTPhraseValue(const std::string& key,
103 const std::vector<STPhraseValue>& stValues) {
104 for (const STPhraseValue& stValue : stValues) {
105 if (stValue.value.find(key) != std::string::npos) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 struct FragmentRisk {
113 std::string category;
114 std::string source;
115 size_t lineNumber;
116 std::string key;
117 std::string value;
118 };
119
120 bool FindSTPhraseValueFragment(const std::string& key,
121 const std::vector<STPhraseValue>& stValues,
122 FragmentRisk* risk) {
123 for (const STPhraseValue& stValue : stValues) {
124 const std::string& value = stValue.value;
125 if (value.size() >= key.size() || !HasAtLeastTwoCharacters(value)) {
126 continue;
127 }
128
129 const size_t pos = key.find(value);
130 if (pos == std::string::npos) {
131 continue;
132 }
133
134 if (pos == 0) {
135 risk->category = "prefix";
136 } else if (pos + value.size() == key.size()) {
137 risk->category = "suffix";
138 } else {
139 risk->category = "middle";
140 }
141 risk->source = stValue.source;
142 risk->lineNumber = stValue.lineNumber;
143 risk->key = stValue.key;
144 risk->value = value;
145 return true;
146 }
147 return false;
148 }
149
150 void ExpectProperSegmentationCoverage(const std::string& phrasesName) {
151 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
152 ASSERT_NE(nullptr, runfiles);
153
154 const std::string stPhrasesFile =
155 runfiles->Rlocation("_main/data/dictionary/STPhrases.txt");
156 const std::string generatedSTPhrasesFile = runfiles->Rlocation(
157 "_main/data/dictionary/STPhrases_GeneratedFromRegionalPhrases.txt");
158 const std::string phrasesFile =
159 runfiles->Rlocation("_main/data/dictionary/" + phrasesName + ".txt");
160
161 try {
162 LexiconPtr phrases = LoadLexicon(phrasesFile);
163 ASSERT_NE(phrases, nullptr);
164
165 std::vector<STPhraseValue> stValues =
166 LoadSTPhraseValues(stPhrasesFile, "STPhrases.txt");
167 std::vector<STPhraseValue> generatedSTValues = LoadSTPhraseValues(
168 generatedSTPhrasesFile, "STPhrases_GeneratedFromRegionalPhrases.txt");
169 stValues.insert(stValues.end(), generatedSTValues.begin(),
170 generatedSTValues.end());
171 ASSERT_FALSE(stValues.empty());
172
173 std::ostringstream missing;
174 size_t missingCount = 0;
175 for (size_t i = 0; i < phrases->Length(); ++i) {
176 const std::string key = phrases->At(i)->Key();
177 if (IsCoveredBySTPhraseValue(key, stValues)) {
178 continue;
179 }
180
181 FragmentRisk risk;
182 if (!FindSTPhraseValueFragment(key, stValues, &risk)) {
183 continue;
184 }
185
186 ++missingCount;
187 missing << phrasesName << " key \"" << key
188 << "\" is not covered by any ST phrase value.\n"
189 << " Conflicting ST phrase record: " << risk.source << ":"
190 << risk.lineNumber << " \"" << risk.key
191 << "\" -> \"" << risk.value << "\"\n"
192 << " The existing value appears as a " << risk.category
193 << " fragment of the " << phrasesName << " key.\n";
194 }
195
196 EXPECT_EQ(0U, missingCount)
197 << "Potential missing ST phrase entries for " << phrasesName << ":\n"
198 << missing.str();
199 } catch (const Exception& ex) {
200 FAIL() << "Exception: " << ex.what();
201 } catch (const std::exception& ex) {
202 FAIL() << "std::exception: " << ex.what();
203 } catch (...) {
204 FAIL() << "Unknown exception thrown during " << phrasesName << " segmentation "
205 "check.";
206 }
207 }
208
209 void ExpectGeneratedSTPhrasesFromRegionalPhrasesAreValid() {
210 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
211 ASSERT_NE(nullptr, runfiles);
212
213 const std::string hkPhrasesFile =
214 runfiles->Rlocation("_main/data/dictionary/HKPhrases.txt");
215 const std::string twPhrasesFile =
216 runfiles->Rlocation("_main/data/dictionary/TWPhrases.txt");
217 const std::string generatedName = "STPhrases_GeneratedFromRegionalPhrases";
218 const std::string generatedSTPhrasesFile = runfiles->Rlocation(
219 "_main/data/dictionary/" + generatedName + ".txt");
220
221 try {
222 LexiconPtr hkPhrases = LoadLexicon(hkPhrasesFile);
223 ASSERT_NE(hkPhrases, nullptr);
224 LexiconPtr twPhrases = LoadLexicon(twPhrasesFile);
225 ASSERT_NE(twPhrases, nullptr);
226 LexiconPtr generatedSTPhrases = LoadLexicon(generatedSTPhrasesFile);
227 ASSERT_NE(generatedSTPhrases, nullptr);
228
229 std::unordered_set<std::string> phraseKeys;
230 for (size_t i = 0; i < hkPhrases->Length(); ++i) {
231 phraseKeys.insert(hkPhrases->At(i)->Key());
232 }
233 for (size_t i = 0; i < twPhrases->Length(); ++i) {
234 phraseKeys.insert(twPhrases->At(i)->Key());
235 }
236
237 for (size_t i = 0; i < generatedSTPhrases->Length(); ++i) {
238 const std::string key = generatedSTPhrases->At(i)->Key();
239 EXPECT_GE(UTF8Util::Length(key.c_str()), 3U)
240 << "Short generated ST phrase key may split longer words: " << key;
241
242 const std::vector<std::string> values = generatedSTPhrases->At(i)->Values();
243 for (const std::string& value : values) {
244 EXPECT_NE(phraseKeys.count(value), 0U)
245 << generatedName << " value is not a regional phrase key: "
246 << value;
247 }
248 }
249 } catch (const Exception& ex) {
250 FAIL() << "Exception: " << ex.what();
251 } catch (const std::exception& ex) {
252 FAIL() << "std::exception: " << ex.what();
253 } catch (...) {
254 FAIL() << "Unknown exception thrown during " << generatedName
255 << " coverage check.";
256 }
257 }
258
259 TEST(DictionaryPhraseSegmentationTest,
260 HKPhrasesKeysAreCoveredBySTPhraseValues) {
261 ExpectProperSegmentationCoverage("HKPhrases");
262 }
263
264 TEST(DictionaryPhraseSegmentationTest,
265 GeneratedSTPhrasesFromRegionalPhrasesAreValid) {
266 ExpectGeneratedSTPhrasesFromRegionalPhrasesAreValid();
267 }
268
269 } // namespace
270 } // namespace opencc