Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/dictionary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ cc_test(
srcs = ["DictionaryVariantRevPhrasesTest.cpp"],
data = [
"HKVariants.txt",
"HKVariantsRev.txt",
"HKVariantsRevPhrases.txt",
"STCharacters.txt",
"STPhrases.txt",
"TWVariants.txt",
"TWVariantsRev.txt",
"TWVariantsRevPhrases.txt",
],
deps = [
Expand Down
42 changes: 40 additions & 2 deletions data/dictionary/DictionaryVariantRevPhrasesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -78,11 +79,40 @@ bool IsInSameMultiVariantSet(
return false;
}

std::unordered_map<std::string, std::string> LoadPreferredValues(
const std::string& path) {
std::ifstream stream(path);
EXPECT_TRUE(stream.is_open()) << path;

std::unordered_map<std::string, std::string> preferredValues;
std::string line;
while (std::getline(stream, line)) {
if (!IsDictionaryEntry(line)) {
continue;
}

const size_t tab = line.find('\t');
if (tab == std::string::npos) {
ADD_FAILURE() << line;
continue;
}
const std::string key = line.substr(0, tab);
const std::vector<std::string> values = SplitValues(line.substr(tab + 1));
if (!values.empty()) {
preferredValues[key] = values.front();
}
}
return preferredValues;
}

std::unordered_set<std::string> LoadReverseExceptionCharacters(
const std::string& variantsPath, const std::string& stCharactersPath) {
const std::string& variantsPath, const std::string& variantsRevPath,
const std::string& stCharactersPath) {
const std::vector<std::unordered_set<std::string>> variantSets =
LoadMultiVariantSets(stCharactersPath);
EXPECT_FALSE(variantSets.empty());
const std::unordered_map<std::string, std::string> reversePreferredValues =
LoadPreferredValues(variantsRevPath);

std::ifstream stream(variantsPath);
EXPECT_TRUE(stream.is_open()) << variantsPath;
Expand All @@ -105,6 +135,11 @@ std::unordered_set<std::string> LoadReverseExceptionCharacters(
if (key == value) {
continue;
}
const auto reversePreferredValue = reversePreferredValues.find(value);
if (reversePreferredValue != reversePreferredValues.end() &&
reversePreferredValue->second == value) {
continue;
}
if (IsInSameMultiVariantSet(variantSets, key, value)) {
exceptionChars.insert(value);
}
Expand Down Expand Up @@ -191,6 +226,8 @@ void ExpectRevPhrasesComplete(const std::string& variantsName) {
const std::string revPhrasesName = variantsName + "RevPhrases.txt";
const std::string variantsPath =
runfiles->Rlocation("_main/data/dictionary/" + variantsName + ".txt");
const std::string variantsRevPath =
runfiles->Rlocation("_main/data/dictionary/" + variantsName + "Rev.txt");
const std::string phrasesPath = runfiles->Rlocation(
"_main/data/dictionary/" + revPhrasesName);
const std::string stCharactersPath =
Expand All @@ -199,7 +236,8 @@ void ExpectRevPhrasesComplete(const std::string& variantsName) {
runfiles->Rlocation("_main/data/dictionary/STPhrases.txt");

const std::unordered_set<std::string> exceptionChars =
LoadReverseExceptionCharacters(variantsPath, stCharactersPath);
LoadReverseExceptionCharacters(variantsPath, variantsRevPath,
stCharactersPath);
ASSERT_FALSE(exceptionChars.empty()) << variantsName;

const std::unordered_set<std::string> expected =
Expand Down
14 changes: 11 additions & 3 deletions data/dictionary/HKVariants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# License: Apache-2.0 (see LICENSE)
# Source: https://github.com/ByVoid/OpenCC
# Used in configs: s2hk.json, t2hk.json
# Also used to generate HKVariantsRev.txt. See data/scripts/reverse.py for
# the reverse dictionary generation algorithm.

僞 偽
兌 兑
Expand All @@ -18,11 +20,15 @@
悅 悦
慍 愠
戶 户
# @reverse-prefer: 才
才 才
挩 捝
搵 揾
敓 敚
敘 敍 敘
柺 枴
# @reverse-prefer: 核
核 核
梲 棁
棱 稜 棱
榲 榅
Expand All @@ -33,6 +39,8 @@
溼 濕
潙 溈
潨 潀
# @reverse-prefer: 煙
煙 煙
熅 煴
爲 為
癡 痴
Expand All @@ -43,21 +51,21 @@
糉 粽 糉 糭
線 線 綫
縕 緼
纔 才
纔 才 纔
脣 唇
脫 脱
膃 腽
臥 卧
臺 台
菸 煙
菸 煙 菸
蒕 蒀
蔥 葱
蔿 蒍
蘊 藴
蛻 蜕
衆 眾
衛 衞
覈 核
覈 核 覈
說 説
踊 踴
轀 輼
Expand Down
Loading
Loading