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
11 changes: 1 addition & 10 deletions data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(
TSCharacters
TSPhrases
TWPhrases
TWPhrasesRev
TWVariants
TWVariantsRevPhrases
HKVariants
Expand All @@ -23,7 +24,6 @@ set(

set(
DICTS_GENERATED
TWPhrasesRev
TWVariantsRev
HKVariantsRev
JPVariantsRev
Expand Down Expand Up @@ -59,15 +59,6 @@ set(
${DICT_REVERSE_BIN} ${DICT_TWVariantsRev_GENERATING_INPUT} TWVariantsRev.txt
)

set(
DICT_TWPhrasesRev_GENERATING_INPUT
${DICT_DIR}/TWPhrases.txt
)
set(
DICT_TWPhrasesRev_GENERATING_COMMAND
${DICT_REVERSE_BIN} ${DICT_TWPhrasesRev_GENERATING_INPUT} TWPhrasesRev.txt
)

set(
DICT_HKVariantsRev_GENERATING_INPUT
${DICT_DIR}/HKVariants.txt
Expand Down
2 changes: 0 additions & 2 deletions data/dictionary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ package(default_visibility = ["//visibility:public"])
)
for txt in [
"TWVariants",
"TWPhrases",
"HKVariants",
"JPVariants",
]
]

TEXT_DICTS = glob(["*.txt"]) + [
"TWVariantsRev.txt",
"TWPhrasesRev.txt",
"HKVariantsRev.txt",
"JPVariantsRev.txt",
]
Expand Down
76 changes: 76 additions & 0 deletions data/dictionary/DictionaryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include "gtest/gtest.h"

#include <unordered_map>
#include <unordered_set>

#include "src/Lexicon.hpp"
#include "src/MarisaDict.hpp"
#include "src/UTF8Util.hpp"
Expand All @@ -44,6 +47,21 @@ class DictionaryTest : public ::testing::Test,

std::string DictionaryTest::runfile_dir_;

class DictionaryRunfilesTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
std::string program_filename = ::testing::internal::GetArgvs().front();
size_t suffix_pos = program_filename.find(RUNFILE_SUFFIX);
ASSERT_NE(suffix_pos, std::string::npos);
runfile_dir_ =
program_filename.substr(0, suffix_pos + strlen(RUNFILE_SUFFIX));
}

static std::string runfile_dir_;
};

std::string DictionaryRunfilesTest::runfile_dir_;

INSTANTIATE_TEST_SUITE_P(
, DictionaryTest,
::testing::Values(
Expand Down Expand Up @@ -86,4 +104,62 @@ TEST_P(DictionaryTest, BinaryTest) {
EXPECT_EQ(dict->GetLexicon()->Length(), txt_lexicon->Length());
}

TEST_F(DictionaryRunfilesTest, TWPhrasesReverseMapping) {
const std::string twPhrasesFile =
runfile_dir_ + "/data/dictionary/TWPhrases.txt";
const std::string twPhrasesRevFile =
runfile_dir_ + "/data/dictionary/TWPhrasesRev.txt";

auto loadLexicon = [](const std::string& path) -> LexiconPtr {
FILE* fp = fopen(UTF8Util::GetPlatformString(path).c_str(), "rb");
EXPECT_NE(fp, nullptr) << path;
if (fp == nullptr) {
return LexiconPtr();
}
return Lexicon::ParseLexiconFromFile(fp);
};

auto buildMap = [](const LexiconPtr& lexicon)
-> std::unordered_map<std::string, std::unordered_set<std::string>> {
std::unordered_map<std::string, std::unordered_set<std::string>> map;
if (!lexicon) {
return map;
}
for (size_t i = 0; i < lexicon->Length(); ++i) {
const DictEntry* entry = lexicon->At(i);
auto& values = map[entry->Key()];
for (const auto& value : entry->Values()) {
values.insert(value);
}
}
return map;
};

LexiconPtr twPhrases = loadLexicon(twPhrasesFile);
LexiconPtr twPhrasesRev = loadLexicon(twPhrasesRevFile);
ASSERT_NE(twPhrases, nullptr);
ASSERT_NE(twPhrasesRev, nullptr);

auto twMap = buildMap(twPhrases);
auto twRevMap = buildMap(twPhrasesRev);

for (const auto& entry : twMap) {
const std::string& key = entry.first;
for (const auto& value : entry.second) {
auto it = twRevMap.find(value);
EXPECT_TRUE(it != twRevMap.end() && it->second.count(key) > 0)
<< "Missing reverse mapping: " << key << " -> " << value;
}
}

for (const auto& entry : twRevMap) {
const std::string& key = entry.first;
for (const auto& value : entry.second) {
auto it = twMap.find(value);
EXPECT_TRUE(it != twMap.end() && it->second.count(key) > 0)
<< "Missing reverse mapping: " << key << " -> " << value;
}
}
}

} // namespace opencc
Loading
Loading