| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-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 <map> |
| 21 | |
| 22 | #include "DictGroup.hpp" |
| 23 | #include "Lexicon.hpp" |
| 24 | #include "TextDict.hpp" |
| 25 | |
| 26 | using namespace opencc; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | size_t GetKeyMaxLength(const std::list<DictPtr>& dicts) { |
| 31 | size_t keyMaxLength = 0; |
| 32 | for (const DictPtr& dict : dicts) { |
| 33 | keyMaxLength = (std::max)(keyMaxLength, dict->KeyMaxLength()); |
| 34 | } |
| 35 | return keyMaxLength; |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | DictGroup::DictGroup(const std::list<DictPtr>& _dicts) |
| 41 | : DictGroup(_dicts, DictGroupMatchPolicy::ShortCircuit) {} |
| 42 | |
| 43 | DictGroup::DictGroup(const std::list<DictPtr>& _dicts, |
| 44 | DictGroupMatchPolicy _matchPolicy) |
| 45 | : keyMaxLength(GetKeyMaxLength(_dicts)), dicts(_dicts), |
| 46 | matchPolicy(_matchPolicy) {} |
| 47 | |
| 48 | DictGroup::~DictGroup() {} |
| 49 | |
| 50 | size_t DictGroup::KeyMaxLength() const { return keyMaxLength; } |
| 51 | |
| 52 | Optional<const DictEntry*> DictGroup::Match(const char* word, |
| 53 | size_t len) const { |
| 54 | for (const auto& dict : dicts) { |
| 55 | const Optional<const DictEntry*>& prefix = dict->Match(word, len); |
| 56 | if (!prefix.IsNull()) { |
| 57 | return prefix; |
| 58 | } |
| 59 | } |
| 60 | return Optional<const DictEntry*>::Null(); |
| 61 | } |
| 62 | |
| 63 | Optional<const DictEntry*> DictGroup::MatchPrefix(const char* word, |
| 64 | size_t len) const { |
| 65 | for (const auto& dict : dicts) { |
| 66 | const Optional<const DictEntry*>& prefix = dict->MatchPrefix(word, len); |
| 67 | if (!prefix.IsNull()) { |
| 68 | return prefix; |
| 69 | } |
| 70 | } |
| 71 | return Optional<const DictEntry*>::Null(); |
| 72 | } |
| 73 | |
| 74 | std::vector<const DictEntry*> DictGroup::MatchAllPrefixes(const char* word, |
| 75 | size_t len) const { |
| 76 | std::map<size_t, const DictEntry*> matched; |
| 77 | // Match all prefixes from all dictionaries |
| 78 | for (const auto& dict : dicts) { |
| 79 | const std::vector<const DictEntry*>& entries = |
| 80 | dict->MatchAllPrefixes(word, len); |
| 81 | for (const auto& entry : entries) { |
| 82 | size_t entryLen = entry->KeyLength(); |
| 83 | // If the current length has already result, skip |
| 84 | if (matched.find(entryLen) == matched.end()) { |
| 85 | matched[entryLen] = entry; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | std::vector<const DictEntry*> matchedEntries; |
| 90 | for (auto i = matched.rbegin(); i != matched.rend(); i++) { |
| 91 | matchedEntries.push_back(i->second); |
| 92 | } |
| 93 | return matchedEntries; |
| 94 | } |
| 95 | |
| 96 | LexiconPtr DictGroup::GetLexicon() const { |
| 97 | LexiconPtr allLexicon(new Lexicon); |
| 98 | for (const auto& dict : dicts) { |
| 99 | const auto& lexicon = dict->GetLexicon(); |
| 100 | for (const std::unique_ptr<DictEntry>& item : *lexicon) { |
| 101 | allLexicon->Add(DictEntryFactory::New(item.get())); |
| 102 | } |
| 103 | } |
| 104 | allLexicon->Sort(); |
| 105 | // Fixme deduplicate |
| 106 | return allLexicon; |
| 107 | } |
| 108 | |
| 109 | DictGroupPtr DictGroup::NewFromDict(const Dict& dict) { |
| 110 | TextDictPtr newDict = TextDict::NewFromDict(dict); |
| 111 | return DictGroupPtr(new DictGroup(std::list<DictPtr>{newDict})); |
| 112 | } |
| 113 | |
| 114 | UnionDictGroup::UnionDictGroup(const std::list<DictPtr>& _dicts) |
| 115 | : DictGroup(_dicts, DictGroupMatchPolicy::Union) {} |
| 116 | |
| 117 | Optional<const DictEntry*> UnionDictGroup::MatchPrefix(const char* word, |
| 118 | size_t len) const { |
| 119 | Optional<const DictEntry*> best = Optional<const DictEntry*>::Null(); |
| 120 | const std::list<DictPtr>* items = GetDictGroupItems(); |
| 121 | if (items == nullptr) { |
| 122 | return best; |
| 123 | } |
| 124 | for (const DictPtr& dict : *items) { |
| 125 | const Optional<const DictEntry*>& prefix = dict->MatchPrefix(word, len); |
| 126 | if (prefix.IsNull()) { |
| 127 | continue; |
| 128 | } |
| 129 | if (best.IsNull() || |
| 130 | prefix.Get()->KeyLength() > best.Get()->KeyLength()) { |
| 131 | best = prefix; |
| 132 | } |
| 133 | } |
| 134 | return best; |
| 135 | } |