| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-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 <list> |
| 22 | #include <string_view> |
| 23 | |
| 24 | #include "Common.hpp" |
| 25 | #include "DictEntry.hpp" |
| 26 | |
| 27 | namespace opencc { |
| 28 | |
| 29 | /** |
| 30 | * Defines how a dictionary group resolves matches across child dictionaries. |
| 31 | */ |
| 32 | enum class DictGroupMatchPolicy { |
| 33 | /** |
| 34 | * Preserve legacy DictGroup behavior: query child dictionaries in order and |
| 35 | * return the first dictionary that has a match. For prefix lookup, this means |
| 36 | * a shorter prefix from an earlier dictionary can win over a longer prefix |
| 37 | * from a later dictionary. |
| 38 | */ |
| 39 | ShortCircuit, |
| 40 | /** |
| 41 | * Treat child dictionaries as a union for prefix lookup: the longest prefix |
| 42 | * across all children wins, with dictionary order breaking ties. |
| 43 | */ |
| 44 | Union, |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Result of a PrefixMatch fast-path lookup. |
| 49 | * key and value are non-owning views; see PrefixMatch::MatchPrefixView() |
| 50 | * for lifetime details. |
| 51 | */ |
| 52 | struct OPENCC_EXPORT PrefixMatchView { |
| 53 | bool matched = false; |
| 54 | size_t keyLength = 0; |
| 55 | std::string_view key; |
| 56 | std::string_view value; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Abstract class of dictionary |
| 61 | * @ingroup opencc_cpp_api |
| 62 | */ |
| 63 | class OPENCC_EXPORT Dict { |
| 64 | public: |
| 65 | /** |
| 66 | * Matches a word exactly and returns the DictEntry or Optional::Null(). |
| 67 | */ |
| 68 | virtual Optional<const DictEntry*> Match(const char* word, |
| 69 | size_t len) const = 0; |
| 70 | |
| 71 | /** |
| 72 | * Matches a word exactly and returns the DictEntry or Optional::Null(). |
| 73 | */ |
| 74 | Optional<const DictEntry*> Match(const std::string& word) const { |
| 75 | return Match(word.c_str(), word.length()); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Matches the longest matched prefix of a word. |
| 80 | * For example given a dictionary having "a", "an", "b", "ba", "ban", "bana", |
| 81 | * the longest prefix of "banana" matched is "bana". |
| 82 | */ |
| 83 | virtual Optional<const DictEntry*> MatchPrefix(const char* word, |
| 84 | size_t len) const; |
| 85 | |
| 86 | /** |
| 87 | * Matches the longest matched prefix of a word. |
| 88 | */ |
| 89 | Optional<const DictEntry*> MatchPrefix(const char* word) const { |
| 90 | return MatchPrefix(word, KeyMaxLength()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Matches the longest matched prefix of a word. |
| 95 | */ |
| 96 | Optional<const DictEntry*> MatchPrefix(const std::string& word) const { |
| 97 | return MatchPrefix(word.c_str(), word.length()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns all matched prefixes of a word, sorted by the length (desc). |
| 102 | * For example given a dictionary having "a", "an", "b", "ba", "ban", "bana", |
| 103 | * all the matched prefixes of "banana" are "bana", "ban", "ba", "b". |
| 104 | */ |
| 105 | virtual std::vector<const DictEntry*> MatchAllPrefixes(const char* word, |
| 106 | size_t len) const; |
| 107 | |
| 108 | /** |
| 109 | * Returns all matched prefixes of a word, sorted by the length (desc). |
| 110 | */ |
| 111 | std::vector<const DictEntry*> |
| 112 | MatchAllPrefixes(const std::string& word) const { |
| 113 | return MatchAllPrefixes(word.c_str(), word.length()); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns the length of the longest key in the dictionary. |
| 118 | */ |
| 119 | virtual size_t KeyMaxLength() const = 0; |
| 120 | |
| 121 | /** |
| 122 | * Returns all entries in the dictionary. |
| 123 | */ |
| 124 | virtual LexiconPtr GetLexicon() const = 0; |
| 125 | |
| 126 | /** |
| 127 | * Returns child dictionaries when this dictionary is a group. |
| 128 | */ |
| 129 | virtual const std::list<DictPtr>* GetDictGroupItems() const { |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns the match policy when this dictionary is a group. Non-group |
| 135 | * dictionaries return ShortCircuit because the value is ignored unless |
| 136 | * GetDictGroupItems() returns children. |
| 137 | */ |
| 138 | virtual DictGroupMatchPolicy GetMatchPolicy() const { |
| 139 | return DictGroupMatchPolicy::ShortCircuit; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns true if this dict can handle prefix queries directly without |
| 144 | * PrefixMatch building a lookup table. Subclasses that override |
| 145 | * MatchPrefixValue() must also override this to return true. |
| 146 | */ |
| 147 | virtual bool SupportsFastPrefixMatch() const { return false; } |
| 148 | |
| 149 | /** |
| 150 | * Fast-path prefix match. Only called when SupportsFastPrefixMatch() is |
| 151 | * true. Returns a PrefixMatchView with matched=false on no match. |
| 152 | * See PrefixMatch::MatchPrefixView() for key/value lifetime details. |
| 153 | */ |
| 154 | virtual PrefixMatchView MatchPrefixValue(const char* word, |
| 155 | size_t len) const { |
| 156 | return PrefixMatchView{}; |
| 157 | } |
| 158 | |
| 159 | virtual ~Dict() = default; |
| 160 | }; |
| 161 | } // namespace opencc |