| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2015-2020 Carbo Kuo <[email protected]> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "Lexicon.hpp" |
| 22 | #include "TestUtils.hpp" |
| 23 | #include "TestUtilsUTF8.hpp" |
| 24 | #include "TextDict.hpp" |
| 25 | |
| 26 | namespace opencc { |
| 27 | |
| 28 | class TextDictTestBase : public ::testing::Test { |
| 29 | protected: |
| 30 | TextDictTestBase() : textDict(CreateTextDictForText()){}; |
| 31 | |
| 32 | TextDictPtr CreateTextDictForText() const { |
| 33 | LexiconPtr lexicon(new Lexicon); |
| 34 | lexicon->Add(DictEntryFactory::New("BYVoid", "byv")); |
| 35 | lexicon->Add(DictEntryFactory::New("zigzagzig", "zag")); |
| 36 | lexicon->Add(DictEntryFactory::New(utf8("積羽沉舟"), utf8("羣輕折軸"))); |
| 37 | lexicon->Add(DictEntryFactory::New(utf8("清"), "Tsing")); |
| 38 | lexicon->Add(DictEntryFactory::New(utf8("清華"), "Tsinghua")); |
| 39 | lexicon->Add(DictEntryFactory::New(utf8("清華大學"), "TsinghuaUniversity")); |
| 40 | lexicon->Sort(); |
| 41 | return TextDictPtr(new TextDict(lexicon)); |
| 42 | } |
| 43 | |
| 44 | DictPtr CreateDictForCharacters() const { |
| 45 | LexiconPtr lexicon(new Lexicon); |
| 46 | lexicon->Add(DictEntryFactory::New( |
| 47 | utf8("后"), std::vector<std::string>{utf8("后"), utf8("後")})); |
| 48 | lexicon->Add(DictEntryFactory::New( |
| 49 | utf8("发"), std::vector<std::string>{utf8("發"), utf8("髮")})); |
| 50 | lexicon->Add(DictEntryFactory::New( |
| 51 | utf8("干"), |
| 52 | std::vector<std::string>{utf8("幹"), utf8("乾"), utf8("干")})); |
| 53 | lexicon->Add(DictEntryFactory::New( |
| 54 | utf8("里"), std::vector<std::string>{utf8("裏"), utf8("里")})); |
| 55 | lexicon->Sort(); |
| 56 | return TextDictPtr(new TextDict(lexicon)); |
| 57 | } |
| 58 | |
| 59 | DictPtr CreateDictForPhrases() const { |
| 60 | LexiconPtr lexicon(new Lexicon); |
| 61 | lexicon->Add(DictEntryFactory::New(utf8("太后"), utf8("太后"))); |
| 62 | lexicon->Add(DictEntryFactory::New(utf8("头发"), utf8("頭髮"))); |
| 63 | lexicon->Add(DictEntryFactory::New(utf8("干燥"), utf8("乾燥"))); |
| 64 | lexicon->Add(DictEntryFactory::New(utf8("鼠标"), utf8("鼠標"))); |
| 65 | lexicon->Sort(); |
| 66 | return TextDictPtr(new TextDict(lexicon)); |
| 67 | } |
| 68 | |
| 69 | DictPtr CreateDictForTaiwanVariants() const { |
| 70 | LexiconPtr lexicon(new Lexicon); |
| 71 | lexicon->Add(DictEntryFactory::New(utf8("裏"), utf8("裡"))); |
| 72 | return TextDictPtr(new TextDict(lexicon)); |
| 73 | } |
| 74 | |
| 75 | DictPtr CreateTaiwanPhraseDict() const { |
| 76 | LexiconPtr lexicon(new Lexicon); |
| 77 | lexicon->Add(DictEntryFactory::New(utf8("鼠标"), utf8("滑鼠"))); |
| 78 | lexicon->Add(DictEntryFactory::New(utf8("服务器"), utf8("伺服器"))); |
| 79 | lexicon->Add(DictEntryFactory::New(utf8("克罗地亚"), utf8("克羅埃西亞"))); |
| 80 | lexicon->Sort(); |
| 81 | return TextDictPtr(new TextDict(lexicon)); |
| 82 | } |
| 83 | |
| 84 | void TestDict(const DictPtr dict) const { |
| 85 | TestMatch(dict); |
| 86 | TestMatchPrefix(dict); |
| 87 | TestMatchAllPrefixes(dict); |
| 88 | } |
| 89 | |
| 90 | void TestMatch(const DictPtr& dict) const { |
| 91 | Optional<const DictEntry*> entry = Optional<const DictEntry*>::Null(); |
| 92 | entry = dict->Match("BYVoid"); |
| 93 | EXPECT_TRUE(!entry.IsNull()); |
| 94 | EXPECT_EQ(utf8("BYVoid"), entry.Get()->Key()); |
| 95 | EXPECT_EQ(utf8("byv"), entry.Get()->GetDefault()); |
| 96 | |
| 97 | entry = dict->Match(""); |
| 98 | EXPECT_TRUE(entry.IsNull()); |
| 99 | |
| 100 | entry = dict->Match("xxx"); |
| 101 | EXPECT_TRUE(entry.IsNull()); |
| 102 | } |
| 103 | |
| 104 | void TestMatchPrefix(const DictPtr& dict) const { |
| 105 | Optional<const DictEntry*> entry = Optional<const DictEntry*>::Null(); |
| 106 | entry = dict->MatchPrefix("BYVoid"); |
| 107 | EXPECT_TRUE(!entry.IsNull()); |
| 108 | EXPECT_EQ(utf8("BYVoid"), entry.Get()->Key()); |
| 109 | EXPECT_EQ(utf8("byv"), entry.Get()->GetDefault()); |
| 110 | |
| 111 | entry = dict->MatchPrefix(utf8("清華大學")); |
| 112 | EXPECT_TRUE(!entry.IsNull()); |
| 113 | EXPECT_EQ(utf8("清華大學"), entry.Get()->Key()); |
| 114 | EXPECT_EQ(utf8("TsinghuaUniversity"), entry.Get()->GetDefault()); |
| 115 | |
| 116 | entry = dict->MatchPrefix("BYVoid123"); |
| 117 | EXPECT_TRUE(!entry.IsNull()); |
| 118 | EXPECT_EQ(utf8("BYVoid"), entry.Get()->Key()); |
| 119 | EXPECT_EQ(utf8("byv"), entry.Get()->GetDefault()); |
| 120 | |
| 121 | entry = dict->MatchPrefix(utf8("積羽沉舟")); |
| 122 | EXPECT_TRUE(!entry.IsNull()); |
| 123 | EXPECT_EQ(utf8("積羽沉舟"), entry.Get()->Key()); |
| 124 | EXPECT_EQ(utf8("羣輕折軸"), entry.Get()->GetDefault()); |
| 125 | |
| 126 | entry = dict->MatchPrefix("Unknown"); |
| 127 | EXPECT_TRUE(entry.IsNull()); |
| 128 | |
| 129 | entry = dict->MatchPrefix(""); |
| 130 | EXPECT_TRUE(entry.IsNull()); |
| 131 | } |
| 132 | |
| 133 | void TestMatchAllPrefixes(const DictPtr& dict) const { |
| 134 | const std::vector<const DictEntry*> matches = |
| 135 | dict->MatchAllPrefixes(utf8("清華大學計算機系")); |
| 136 | EXPECT_EQ(3, matches.size()); |
| 137 | EXPECT_EQ(utf8("清華大學"), matches.at(0)->Key()); |
| 138 | EXPECT_EQ(utf8("TsinghuaUniversity"), matches.at(0)->GetDefault()); |
| 139 | EXPECT_EQ(utf8("清華"), matches.at(1)->Key()); |
| 140 | EXPECT_EQ(utf8("Tsinghua"), matches.at(1)->GetDefault()); |
| 141 | EXPECT_EQ(utf8("清"), matches.at(2)->Key()); |
| 142 | EXPECT_EQ(utf8("Tsing"), matches.at(2)->GetDefault()); |
| 143 | } |
| 144 | |
| 145 | const TextDictPtr textDict; |
| 146 | }; |
| 147 | |
| 148 | } // namespace opencc |