Debian Codegraph @

data/dictionary/DictionaryPhraseCharacterDependencyTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Validate that phrase-level character substitutions remain declared in the
5 * corresponding character dictionary when phrase entries participate in the
6 * same character conversion domain.
7 */
8
9 #include "gtest/gtest.h"
10
11 #include <fstream>
12 #include <sstream>
13 #include <string>
14 #include <unordered_map>
15 #include <unordered_set>
16 #include <vector>
17
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 struct Entry {
27 size_t lineNumber;
28 std::string key;
29 std::vector<std::string> values;
30 };
31
32 struct DictionaryPair {
33 std::string phraseFile;
34 std::string characterFile;
35 };
36
37 std::vector<std::string> SplitValues(const std::string& values) {
38 std::vector<std::string> split;
39 size_t start = 0;
40 while (start <= values.size()) {
41 const size_t end = values.find(' ', start);
42 split.push_back(values.substr(start, end - start));
43 if (end == std::string::npos) {
44 break;
45 }
46 start = end + 1;
47 }
48 return split;
49 }
50
51 std::vector<std::string> SplitCharacters(const std::string& text) {
52 std::vector<std::string> characters;
53 const char* cursor = text.c_str();
54 while (*cursor != '\0') {
55 const size_t length = UTF8Util::NextCharLength(cursor);
56 characters.push_back(UTF8Util::FromSubstr(cursor, length));
57 cursor += length;
58 }
59 return characters;
60 }
61
62 bool IsDictionaryEntry(const std::string& line) {
63 return !line.empty() && line[0] != '#';
64 }
65
66 std::vector<Entry> LoadEntries(const std::string& path) {
67 std::ifstream stream(path);
68 EXPECT_TRUE(stream.is_open()) << path;
69
70 std::vector<Entry> entries;
71 std::string line;
72 size_t lineNumber = 0;
73 while (std::getline(stream, line)) {
74 ++lineNumber;
75 if (!IsDictionaryEntry(line)) {
76 continue;
77 }
78
79 const size_t tab = line.find('\t');
80 if (tab == std::string::npos) {
81 ADD_FAILURE() << path << ":" << lineNumber
82 << " is missing a tab separator.";
83 continue;
84 }
85 entries.push_back(Entry{lineNumber, line.substr(0, tab),
86 SplitValues(line.substr(tab + 1))});
87 }
88 return entries;
89 }
90
91 std::unordered_map<std::string, std::unordered_set<std::string>>
92 LoadCharacterCandidates(const std::string& path) {
93 std::unordered_map<std::string, std::unordered_set<std::string>> candidates;
94 for (const Entry& entry : LoadEntries(path)) {
95 if (UTF8Util::Length(entry.key.c_str()) != 1) {
96 continue;
97 }
98 for (const std::string& value : entry.values) {
99 if (UTF8Util::Length(value.c_str()) == 1) {
100 candidates[entry.key].insert(value);
101 }
102 }
103 }
104 return candidates;
105 }
106
107 void ExpectPhraseCharacterSubstitutionsDeclared(
108 const Runfiles& runfiles, const DictionaryPair& dictionaryPair) {
109 const std::string dictionaryDir = "_main/data/dictionary/";
110 const std::string phrasePath =
111 runfiles.Rlocation(dictionaryDir + dictionaryPair.phraseFile);
112 const std::string characterPath =
113 runfiles.Rlocation(dictionaryDir + dictionaryPair.characterFile);
114
115 const std::vector<Entry> phraseEntries = LoadEntries(phrasePath);
116 const std::unordered_map<std::string, std::unordered_set<std::string>>
117 characterCandidates = LoadCharacterCandidates(characterPath);
118
119 std::ostringstream failures;
120 size_t failureCount = 0;
121 for (const Entry& entry : phraseEntries) {
122 const std::vector<std::string> keyCharacters = SplitCharacters(entry.key);
123 for (const std::string& value : entry.values) {
124 const std::vector<std::string> valueCharacters = SplitCharacters(value);
125 if (keyCharacters.size() != valueCharacters.size()) {
126 continue;
127 }
128
129 for (size_t i = 0; i < keyCharacters.size(); ++i) {
130 const std::string& source = keyCharacters[i];
131 const std::string& target = valueCharacters[i];
132 if (source == target) {
133 continue;
134 }
135
136 const auto candidates = characterCandidates.find(source);
137 if (candidates != characterCandidates.end() &&
138 candidates->second.count(target) != 0) {
139 continue;
140 }
141
142 ++failureCount;
143 failures << dictionaryPair.phraseFile << ":" << entry.lineNumber
144 << " maps \"" << entry.key << "\" -> \"" << value
145 << "\", including character substitution \"" << source
146 << "\" -> \"" << target << "\", but "
147 << dictionaryPair.characterFile
148 << " does not list that target as a candidate.\n";
149 }
150 }
151 }
152
153 EXPECT_EQ(0U, failureCount)
154 << "Phrase-level character substitutions must also be declared in the "
155 "corresponding character dictionary. If a direct character conversion "
156 "is not generally desirable, keep it as a non-default candidate such "
157 "as A<TAB>A B instead of deleting it outright:\n"
158 << failures.str();
159 }
160
161 TEST(DictionaryPhraseCharacterDependencyTest,
162 PhraseSubstitutionsRemainDeclaredInCharacterDictionaries) {
163 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
164 ASSERT_NE(nullptr, runfiles);
165
166 // Regional variant phrase dictionaries may contain phrase-only exceptions
167 // that should not become general character-level variant candidates.
168 const std::vector<DictionaryPair> dictionaryPairs = {
169 {"STPhrases.txt", "STCharacters.txt"},
170 {"TSPhrases.txt", "TSCharacters.txt"},
171 {"HKVariantsRevPhrases.txt", "HKVariantsRev.txt"},
172 {"TWVariantsRevPhrases.txt", "TWVariantsRev.txt"},
173 };
174 for (const DictionaryPair& dictionaryPair : dictionaryPairs) {
175 ExpectPhraseCharacterSubstitutionsDeclared(*runfiles, dictionaryPair);
176 }
177 }
178
179 } // namespace
180 } // namespace opencc