Debian Codegraph @

src/DictGroup.hpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2014 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
23 #include "Common.hpp"
24 #include "Dict.hpp"
25
26 namespace opencc {
27
28 /**
29 * Group of dictionaries.
30 *
31 * DictGroup preserves dictionary order. With the current ShortCircuit match
32 * policy, exact lookup returns the first match from the first child dictionary
33 * that contains the key.
34 *
35 * OpenCC's built-in conversion and mmseg segmentation paths do not call
36 * DictGroup::MatchPrefix() or DictGroup::MatchAllPrefixes() directly. They
37 * construct a PrefixMatch from the group, and PrefixMatch uses
38 * GetDictGroupItems() to build its own group-aware prefix matcher. The prefix
39 * methods below are still part of the Dict API for direct DictGroup callers
40 * and tests.
41 *
42 * @ingroup opencc_cpp_api
43 */
44 class OPENCC_EXPORT DictGroup : public Dict {
45 public:
46 explicit DictGroup(const std::list<DictPtr>& dicts);
47
48 DictGroup(const std::list<DictPtr>& dicts, DictGroupMatchPolicy matchPolicy);
49
50 static DictGroupPtr NewFromDict(const Dict& dict);
51
52 virtual ~DictGroup();
53
54 /**
55 * Returns the maximum KeyMaxLength() among all child dictionaries.
56 */
57 virtual size_t KeyMaxLength() const;
58
59 /**
60 * Matches the key exactly against child dictionaries in group order.
61 * Returns the first child dictionary's exact match, if any.
62 */
63 virtual Optional<const DictEntry*> Match(const char* word, size_t len) const;
64
65 /**
66 * Matches the longest prefix within the first child dictionary that has any
67 * prefix match. A shorter match from an earlier child dictionary therefore
68 * wins over a longer match from a later child dictionary.
69 *
70 * This method is not used by OpenCC's normal conversion or mmseg
71 * segmentation paths; PrefixMatch expands the group via GetDictGroupItems()
72 * instead.
73 */
74 virtual Optional<const DictEntry*> MatchPrefix(const char* word,
75 size_t len) const;
76
77 /**
78 * Returns prefix matches from all child dictionaries, sorted by key length
79 * descending. When multiple child dictionaries have matches of the same key
80 * length, the first child dictionary wins.
81 *
82 * This method is not used by OpenCC's normal conversion or mmseg
83 * segmentation paths; PrefixMatch expands the group via GetDictGroupItems()
84 * instead.
85 */
86 virtual std::vector<const DictEntry*> MatchAllPrefixes(const char* word,
87 size_t len) const;
88
89 /**
90 * Returns a merged lexicon from all child dictionaries.
91 *
92 * Duplicate keys are not deduplicated here. PrefixMatch handles group
93 * priority while building its lookup table.
94 */
95 virtual LexiconPtr GetLexicon() const;
96
97 /**
98 * Exposes child dictionaries to callers that need group-aware behavior.
99 * PrefixMatch uses this to preserve nested group boundaries and dictionary
100 * order.
101 */
102 virtual const std::list<DictPtr>* GetDictGroupItems() const {
103 return &dicts;
104 }
105
106 /**
107 * Returns the child dictionaries by value.
108 */
109 const std::list<DictPtr> GetDicts() const { return dicts; }
110
111 /**
112 * Returns how this group resolves matches across child dictionaries.
113 */
114 virtual DictGroupMatchPolicy GetMatchPolicy() const { return matchPolicy; }
115
116 private:
117 const size_t keyMaxLength;
118 const std::list<DictPtr> dicts;
119 const DictGroupMatchPolicy matchPolicy;
120 };
121
122 /**
123 * Group of dictionaries with union prefix-match semantics.
124 *
125 * Exact lookup still uses dictionary order for duplicate keys. Prefix lookup
126 * chooses the longest child match, using dictionary order only as a tie-breaker.
127 * MatchAllPrefixes() has the same behavior as DictGroup: all matched prefix
128 * lengths are returned once, and earlier dictionaries win equal-length ties.
129 */
130 class OPENCC_EXPORT UnionDictGroup : public DictGroup {
131 public:
132 explicit UnionDictGroup(const std::list<DictPtr>& dicts);
133
134 virtual Optional<const DictEntry*> MatchPrefix(const char* word,
135 size_t len) const;
136 };
137 } // namespace opencc