| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020-2026 Carbo Kuo and contributors |
| 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 | #include <algorithm> |
| 20 | #include <cstring> |
| 21 | #include <list> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "PrefixMatch.hpp" |
| 26 | #include "DictGroup.hpp" |
| 27 | #include "MarisaDict.hpp" |
| 28 | #include "TestUtilsUTF8.hpp" |
| 29 | #include "TextDictTestBase.hpp" |
| 30 | |
| 31 | namespace opencc { |
| 32 | |
| 33 | class PrefixMatchTest : public TextDictTestBase { |
| 34 | protected: |
| 35 | PrefixMatchTest() |
| 36 | : marisaDict(MarisaDict::NewFromDict(*textDict)), fileName("dict.ocd2") { |
| 37 | marisaDict->opencc::SerializableDict::SerializeToFile(fileName); |
| 38 | } |
| 39 | |
| 40 | virtual ~PrefixMatchTest() { |
| 41 | std::remove(fileName.c_str()); |
| 42 | } |
| 43 | |
| 44 | const MarisaDictPtr marisaDict; |
| 45 | const std::string fileName; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(PrefixMatchTest, SingleDictFastPathMatchesTablePath) { |
| 49 | // 1. Create a single-element DictGroup wrapper around marisaDict |
| 50 | std::list<DictPtr> dicts = { marisaDict }; |
| 51 | DictPtr dictGroup(new DictGroup(dicts)); |
| 52 | |
| 53 | // 2. Create two PrefixMatch instances: |
| 54 | // - pmFast uses the single dict fast-path (unwrapped group -> marisaDict) |
| 55 | PrefixMatch pmFast(dictGroup); |
| 56 | |
| 57 | // - pmTable uses the standard table path because we pass a group of two dicts |
| 58 | std::list<DictPtr> dictsMulti = { marisaDict, textDict }; |
| 59 | DictPtr dictGroupMulti(new DictGroup(dictsMulti)); |
| 60 | PrefixMatch pmTable(dictGroupMulti); |
| 61 | |
| 62 | // Compare queries |
| 63 | const std::vector<std::string> testQueries = { |
| 64 | "清華", "清華大", "清華大學", "清", "積羽沉舟", "nowhere" |
| 65 | }; |
| 66 | |
| 67 | for (const std::string& query : testQueries) { |
| 68 | PrefixMatch::Match mFast = pmFast.MatchPrefix(query.c_str(), query.length()); |
| 69 | PrefixMatch::Match mTable = pmTable.MatchPrefix(query.c_str(), query.length()); |
| 70 | |
| 71 | EXPECT_EQ(mFast.matched, mTable.matched); |
| 72 | if (mFast.matched) { |
| 73 | EXPECT_EQ(mFast.keyLength, mTable.keyLength); |
| 74 | EXPECT_EQ(*mFast.key, *mTable.key); |
| 75 | EXPECT_EQ(*mFast.value, *mTable.value); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TEST_F(PrefixMatchTest, MarisaLazyLoadFastPathDoesNotReconstruct) { |
| 81 | // 1. Load MarisaDict from the serialized ocd2 file (lazy load) |
| 82 | MarisaDictPtr lazyDict = SerializableDict::NewFromFile<MarisaDict>(fileName); |
| 83 | |
| 84 | // Verify it is not reconstructed initially |
| 85 | EXPECT_FALSE(lazyDict->IsLexiconReconstructed()); |
| 86 | |
| 87 | // 2. Create PrefixMatch single-dict fast-path on it |
| 88 | PrefixMatch pm(lazyDict); |
| 89 | |
| 90 | // 3. Query a prefix that should hit |
| 91 | PrefixMatch::Match m = pm.MatchPrefix("清華", 6); |
| 92 | EXPECT_TRUE(m.matched); |
| 93 | EXPECT_EQ(6, m.keyLength); // "清華" length is 6 bytes in UTF-8 |
| 94 | EXPECT_EQ("清華", *m.key); |
| 95 | EXPECT_EQ(utf8("Tsinghua"), *m.value); |
| 96 | |
| 97 | // 4. Verify that the lexicon was NOT reconstructed during the query! |
| 98 | EXPECT_FALSE(lazyDict->IsLexiconReconstructed()); |
| 99 | } |
| 100 | |
| 101 | TEST_F(PrefixMatchTest, FastPathSelectsLongestMatch) { |
| 102 | // Dictionary has "清"→Tsing, "清華"→Tsinghua, "清華大學"→TsinghuaUniversity. |
| 103 | // A query with a longer input must match the longest key, not the first |
| 104 | // prefix returned by common_prefix_search(). |
| 105 | PrefixMatch pm(marisaDict); |
| 106 | |
| 107 | const std::string query = utf8("清華大學校園"); |
| 108 | PrefixMatch::Match m = pm.MatchPrefix(query.c_str(), query.length()); |
| 109 | |
| 110 | EXPECT_TRUE(m.matched); |
| 111 | EXPECT_EQ(utf8("清華大學"), *m.key); |
| 112 | EXPECT_EQ(utf8("TsinghuaUniversity"), *m.value); |
| 113 | EXPECT_EQ(m.key->length(), m.keyLength); |
| 114 | } |
| 115 | |
| 116 | TEST_F(PrefixMatchTest, MatchPrefixViewFastPathReturnsViews) { |
| 117 | // Single-dict fast-path: key view should point into caller's input buffer, |
| 118 | // value view should point into the dict's stable storage (no copy). |
| 119 | PrefixMatch pm(marisaDict); |
| 120 | |
| 121 | const std::string query = utf8("清華大學校園"); |
| 122 | PrefixMatchView v = pm.MatchPrefixView(query.c_str(), query.length()); |
| 123 | |
| 124 | EXPECT_TRUE(v.matched); |
| 125 | EXPECT_EQ(utf8("清華大學"), std::string(v.key)); |
| 126 | EXPECT_EQ(utf8("TsinghuaUniversity"), std::string(v.value)); |
| 127 | EXPECT_EQ(v.key.size(), v.keyLength); |
| 128 | // key view must alias the caller's buffer exactly. |
| 129 | EXPECT_EQ(query.data(), v.key.data()); |
| 130 | } |
| 131 | |
| 132 | TEST_F(PrefixMatchTest, MatchPrefixViewTablePathReturnsViews) { |
| 133 | // Table-path (multi-dict): value view points into stable PrefixMatch storage. |
| 134 | std::list<DictPtr> dictsMulti = {marisaDict, textDict}; |
| 135 | DictPtr dictGroupMulti(new DictGroup(dictsMulti)); |
| 136 | PrefixMatch pm(dictGroupMulti); |
| 137 | |
| 138 | const std::string query = utf8("清華大學校園"); |
| 139 | PrefixMatchView v = pm.MatchPrefixView(query.c_str(), query.length()); |
| 140 | |
| 141 | EXPECT_TRUE(v.matched); |
| 142 | EXPECT_EQ(utf8("清華大學"), std::string(v.key)); |
| 143 | EXPECT_EQ(utf8("TsinghuaUniversity"), std::string(v.value)); |
| 144 | EXPECT_EQ(v.key.size(), v.keyLength); |
| 145 | } |
| 146 | |
| 147 | TEST_F(PrefixMatchTest, ShortCircuitGroupPrefersEarlierShorterMatch) { |
| 148 | LexiconPtr firstLexicon(new Lexicon); |
| 149 | firstLexicon->Add(DictEntryFactory::New(utf8("意大利"), utf8("義大利"))); |
| 150 | firstLexicon->Sort(); |
| 151 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 152 | |
| 153 | LexiconPtr secondLexicon(new Lexicon); |
| 154 | secondLexicon->Add(DictEntryFactory::New(utf8("意大利面"), utf8("義大利麵"))); |
| 155 | secondLexicon->Sort(); |
| 156 | DictPtr secondDict(new TextDict(secondLexicon)); |
| 157 | |
| 158 | DictPtr dictGroup(new DictGroup( |
| 159 | std::list<DictPtr>{firstDict, secondDict}, |
| 160 | DictGroupMatchPolicy::ShortCircuit)); |
| 161 | PrefixMatch pm(dictGroup); |
| 162 | |
| 163 | const std::string query = utf8("意大利面"); |
| 164 | PrefixMatch::Match m = pm.MatchPrefix(query.c_str(), query.length()); |
| 165 | |
| 166 | EXPECT_TRUE(m.matched); |
| 167 | EXPECT_EQ(utf8("意大利"), *m.key); |
| 168 | EXPECT_EQ(utf8("義大利"), *m.value); |
| 169 | } |
| 170 | |
| 171 | TEST_F(PrefixMatchTest, UnionGroupPrefersLaterLongerMatch) { |
| 172 | LexiconPtr firstLexicon(new Lexicon); |
| 173 | firstLexicon->Add(DictEntryFactory::New(utf8("意大利"), utf8("義大利"))); |
| 174 | firstLexicon->Sort(); |
| 175 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 176 | |
| 177 | LexiconPtr secondLexicon(new Lexicon); |
| 178 | secondLexicon->Add(DictEntryFactory::New(utf8("意大利面"), utf8("義大利麵"))); |
| 179 | secondLexicon->Sort(); |
| 180 | DictPtr secondDict(new TextDict(secondLexicon)); |
| 181 | |
| 182 | DictPtr dictGroup( |
| 183 | new UnionDictGroup(std::list<DictPtr>{firstDict, secondDict})); |
| 184 | PrefixMatch pm(dictGroup); |
| 185 | |
| 186 | const std::string query = utf8("意大利面"); |
| 187 | PrefixMatch::Match m = pm.MatchPrefix(query.c_str(), query.length()); |
| 188 | |
| 189 | EXPECT_TRUE(m.matched); |
| 190 | EXPECT_EQ(utf8("意大利面"), *m.key); |
| 191 | EXPECT_EQ(utf8("義大利麵"), *m.value); |
| 192 | } |
| 193 | |
| 194 | TEST_F(PrefixMatchTest, UnionGroupPreservesNestedShortCircuitBoundary) { |
| 195 | LexiconPtr firstLexicon(new Lexicon); |
| 196 | firstLexicon->Add(DictEntryFactory::New(utf8("意"), "nested-first")); |
| 197 | firstLexicon->Sort(); |
| 198 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 199 | |
| 200 | LexiconPtr hiddenLexicon(new Lexicon); |
| 201 | hiddenLexicon->Add(DictEntryFactory::New(utf8("意大利面"), "hidden")); |
| 202 | hiddenLexicon->Sort(); |
| 203 | DictPtr hiddenDict(new TextDict(hiddenLexicon)); |
| 204 | |
| 205 | LexiconPtr rootLexicon(new Lexicon); |
| 206 | rootLexicon->Add(DictEntryFactory::New(utf8("意大利"), "root-second")); |
| 207 | rootLexicon->Sort(); |
| 208 | DictPtr rootDict(new TextDict(rootLexicon)); |
| 209 | |
| 210 | DictPtr nestedGroup( |
| 211 | new DictGroup(std::list<DictPtr>{firstDict, hiddenDict})); |
| 212 | DictPtr rootGroup( |
| 213 | new UnionDictGroup(std::list<DictPtr>{nestedGroup, rootDict})); |
| 214 | PrefixMatch pm(rootGroup); |
| 215 | |
| 216 | const std::string query = utf8("意大利面"); |
| 217 | PrefixMatch::Match m = pm.MatchPrefix(query.c_str(), query.length()); |
| 218 | |
| 219 | EXPECT_TRUE(m.matched); |
| 220 | EXPECT_EQ(utf8("意大利"), *m.key); |
| 221 | EXPECT_EQ("root-second", *m.value); |
| 222 | } |
| 223 | |
| 224 | } // namespace opencc |