Open Chinese Convert 1.3.2+gad37fd0a6.dirty
A project for conversion between Traditional and Simplified Chinese
Loading...
Searching...
No Matches
Dict.hpp
1/*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2020 Carbo Kuo <byvoid@byvoid.com>
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
27namespace opencc {
28
32enum class DictGroupMatchPolicy {
39 ShortCircuit,
44 Union,
45};
46
52struct OPENCC_EXPORT PrefixMatchView {
53 bool matched = false;
54 size_t keyLength = 0;
55 std::string_view key;
56 std::string_view value;
57};
58
63class OPENCC_EXPORT Dict {
64public:
68 virtual Optional<const DictEntry*> Match(const char* word,
69 size_t len) const = 0;
70
74 Optional<const DictEntry*> Match(const std::string& word) const {
75 return Match(word.c_str(), word.length());
76 }
77
83 virtual Optional<const DictEntry*> MatchPrefix(const char* word,
84 size_t len) const;
85
89 Optional<const DictEntry*> MatchPrefix(const char* word) const {
90 return MatchPrefix(word, KeyMaxLength());
91 }
92
96 Optional<const DictEntry*> MatchPrefix(const std::string& word) const {
97 return MatchPrefix(word.c_str(), word.length());
98 }
99
105 virtual std::vector<const DictEntry*> MatchAllPrefixes(const char* word,
106 size_t len) const;
107
111 std::vector<const DictEntry*>
112 MatchAllPrefixes(const std::string& word) const {
113 return MatchAllPrefixes(word.c_str(), word.length());
114 }
115
119 virtual size_t KeyMaxLength() const = 0;
120
124 virtual LexiconPtr GetLexicon() const = 0;
125
129 virtual const std::list<DictPtr>* GetDictGroupItems() const {
130 return nullptr;
131 }
132
138 virtual DictGroupMatchPolicy GetMatchPolicy() const {
139 return DictGroupMatchPolicy::ShortCircuit;
140 }
141
147 virtual bool SupportsFastPrefixMatch() const { return false; }
148
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
Abstract class of dictionary.
Definition Dict.hpp:63
virtual bool SupportsFastPrefixMatch() const
Returns true if this dict can handle prefix queries directly without PrefixMatch building a lookup ta...
Definition Dict.hpp:147
virtual PrefixMatchView MatchPrefixValue(const char *word, size_t len) const
Fast-path prefix match.
Definition Dict.hpp:154
virtual size_t KeyMaxLength() const =0
Returns the length of the longest key in the dictionary.
virtual Optional< const DictEntry * > Match(const char *word, size_t len) const =0
Matches a word exactly and returns the DictEntry or Optional::Null().
virtual LexiconPtr GetLexicon() const =0
Returns all entries in the dictionary.
Optional< const DictEntry * > MatchPrefix(const std::string &word) const
Matches the longest matched prefix of a word.
Definition Dict.hpp:96
Optional< const DictEntry * > Match(const std::string &word) const
Matches a word exactly and returns the DictEntry or Optional::Null().
Definition Dict.hpp:74
Optional< const DictEntry * > MatchPrefix(const char *word) const
Matches the longest matched prefix of a word.
Definition Dict.hpp:89
virtual DictGroupMatchPolicy GetMatchPolicy() const
Returns the match policy when this dictionary is a group.
Definition Dict.hpp:138
virtual const std::list< DictPtr > * GetDictGroupItems() const
Returns child dictionaries when this dictionary is a group.
Definition Dict.hpp:129
virtual Optional< const DictEntry * > MatchPrefix(const char *word, size_t len) const
Matches the longest matched prefix of a word.
Definition Dict.cpp:25
virtual std::vector< const DictEntry * > MatchAllPrefixes(const char *word, size_t len) const
Returns all matched prefixes of a word, sorted by the length (desc).
Definition Dict.cpp:42
std::vector< const DictEntry * > MatchAllPrefixes(const std::string &word) const
Returns all matched prefixes of a word, sorted by the length (desc).
Definition Dict.hpp:112
A class that wraps type T into a nullable type.
Definition Optional.hpp:26
Result of a PrefixMatch fast-path lookup.
Definition Dict.hpp:52