| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Validate generated reverse phrase dictionaries. |
| 5 | */ |
| 6 | |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | #include <unordered_map> |
| 10 | #include <unordered_set> |
| 11 | |
| 12 | #include "src/Exception.hpp" |
| 13 | #include "src/Lexicon.hpp" |
| 14 | #include "src/UTF8Util.hpp" |
| 15 | #include "tools/cpp/runfiles/runfiles.h" |
| 16 | |
| 17 | using bazel::tools::cpp::runfiles::Runfiles; |
| 18 | |
| 19 | namespace opencc { |
| 20 | namespace { |
| 21 | |
| 22 | static FILE* OpenFile(const std::string& path) { |
| 23 | #ifdef _MSC_VER |
| 24 | return _wfopen(UTF8Util::GetPlatformString(path).c_str(), L"rb"); |
| 25 | #else |
| 26 | return fopen(UTF8Util::GetPlatformString(path).c_str(), "rb"); |
| 27 | #endif |
| 28 | } |
| 29 | |
| 30 | void ExpectRevComplete(const std::string& phrasesName) { |
| 31 | std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); |
| 32 | ASSERT_NE(nullptr, runfiles); |
| 33 | |
| 34 | const std::string phrasesFile = |
| 35 | runfiles->Rlocation("_main/data/dictionary/" + phrasesName + ".txt"); |
| 36 | const std::string phrasesRevFile = |
| 37 | runfiles->Rlocation("_main/data/dictionary/" + phrasesName + "Rev" + ".txt"); |
| 38 | |
| 39 | auto loadLexicon = [](const std::string& path) -> LexiconPtr { |
| 40 | FILE* fp = OpenFile(path); |
| 41 | EXPECT_NE(fp, nullptr) << path; |
| 42 | if (fp == nullptr) { |
| 43 | return LexiconPtr(); |
| 44 | } |
| 45 | return Lexicon::ParseLexiconFromFile(fp); |
| 46 | }; |
| 47 | |
| 48 | auto buildMap = [](const LexiconPtr& lexicon) |
| 49 | -> std::unordered_map<std::string, std::unordered_set<std::string>> { |
| 50 | std::unordered_map<std::string, std::unordered_set<std::string>> map; |
| 51 | if (!lexicon) { |
| 52 | return map; |
| 53 | } |
| 54 | for (size_t i = 0; i < lexicon->Length(); ++i) { |
| 55 | const DictEntry* entry = lexicon->At(i); |
| 56 | auto& values = map[entry->Key()]; |
| 57 | for (const auto& value : entry->Values()) { |
| 58 | values.insert(value); |
| 59 | } |
| 60 | } |
| 61 | return map; |
| 62 | }; |
| 63 | |
| 64 | try { |
| 65 | LexiconPtr twPhrases = loadLexicon(phrasesFile); |
| 66 | LexiconPtr twPhrasesRev = loadLexicon(phrasesRevFile); |
| 67 | ASSERT_NE(twPhrases, nullptr); |
| 68 | ASSERT_NE(twPhrasesRev, nullptr); |
| 69 | |
| 70 | auto phrasesMap = buildMap(twPhrases); |
| 71 | auto phrasesRevMap = buildMap(twPhrasesRev); |
| 72 | |
| 73 | for (const auto& entry : phrasesMap) { |
| 74 | const std::string& key = entry.first; |
| 75 | for (const auto& value : entry.second) { |
| 76 | auto it = phrasesRevMap.find(value); |
| 77 | EXPECT_TRUE(it != phrasesRevMap.end() && it->second.count(key) > 0) |
| 78 | << "Missing reverse mapping: " << key << " -> " << value; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for (const auto& entry : phrasesRevMap) { |
| 83 | const std::string& key = entry.first; |
| 84 | for (const auto& value : entry.second) { |
| 85 | auto it = phrasesMap.find(value); |
| 86 | EXPECT_TRUE(it != phrasesMap.end() && it->second.count(key) > 0) |
| 87 | << "Missing reverse mapping: " << key << " -> " << value; |
| 88 | } |
| 89 | } |
| 90 | } catch (const Exception& ex) { |
| 91 | FAIL() << "Exception: " << ex.what(); |
| 92 | } catch (const std::exception& ex) { |
| 93 | FAIL() << "std::exception: " << ex.what(); |
| 94 | } catch (...) { |
| 95 | FAIL() << "Unknown exception thrown during reverse mapping check."; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | TEST(DictionaryReverseMappingTest, HKPhrasesReverseMapping) { |
| 100 | ExpectRevComplete("HKPhrases"); |
| 101 | } |
| 102 | |
| 103 | TEST(DictionaryReverseMappingTest, TWPhrasesReverseMapping) { |
| 104 | ExpectRevComplete("TWPhrases"); |
| 105 | } |
| 106 | |
| 107 | } // namespace |
| 108 | } // namespace opencc |