Loading...
Searching...
No Matches
Dict.hpp
/*
* Open Chinese Convert
*
* Copyright 2010-2020 Carbo Kuo <byvoid@byvoid.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <list>
#include <string_view>
#include "Common.hpp"
#include "DictEntry.hpp"
namespace opencc {
enum class DictGroupMatchPolicy {
ShortCircuit,
Union,
};
struct OPENCC_EXPORT PrefixMatchView {
bool matched = false;
size_t keyLength = 0;
std::string_view key;
std::string_view value;
};
class OPENCC_EXPORT Dict {
public:
virtual Optional<const DictEntry*> Match(const char* word,
size_t len) const = 0;
Optional<const DictEntry*> Match(const std::string& word) const {
return Match(word.c_str(), word.length());
}
virtual Optional<const DictEntry*> MatchPrefix(const char* word,
size_t len) const;
Optional<const DictEntry*> MatchPrefix(const char* word) const {
return MatchPrefix(word, KeyMaxLength());
}
Optional<const DictEntry*> MatchPrefix(const std::string& word) const {
return MatchPrefix(word.c_str(), word.length());
}
virtual std::vector<const DictEntry*> MatchAllPrefixes(const char* word,
size_t len) const;
std::vector<const DictEntry*>
MatchAllPrefixes(const std::string& word) const {
return MatchAllPrefixes(word.c_str(), word.length());
}
virtual size_t KeyMaxLength() const = 0;
virtual LexiconPtr GetLexicon() const = 0;
virtual const std::list<DictPtr>* GetDictGroupItems() const {
return nullptr;
}
virtual DictGroupMatchPolicy GetMatchPolicy() const {
return DictGroupMatchPolicy::ShortCircuit;
}
virtual bool SupportsFastPrefixMatch() const { return false; }
virtual PrefixMatchView MatchPrefixValue(const char* word,
size_t len) const {
return PrefixMatchView{};
}
virtual ~Dict() = default;
};
} // namespace opencc
opencc::Dict
Abstract class of dictionary.
Definition Dict.hpp:63
opencc::Dict::SupportsFastPrefixMatch
virtual bool SupportsFastPrefixMatch() const
Returns true if this dict can handle prefix queries directly without PrefixMatch building a lookup ta...
Definition Dict.hpp:147
opencc::Dict::MatchPrefixValue
virtual PrefixMatchView MatchPrefixValue(const char *word, size_t len) const
Fast-path prefix match.
Definition Dict.hpp:154
opencc::Dict::KeyMaxLength
virtual size_t KeyMaxLength() const =0
Returns the length of the longest key in the dictionary.
opencc::Dict::Match
virtual Optional< const DictEntry * > Match(const char *word, size_t len) const =0
Matches a word exactly and returns the DictEntry or Optional::Null().
opencc::Dict::GetLexicon
virtual LexiconPtr GetLexicon() const =0
Returns all entries in the dictionary.
opencc::Dict::MatchPrefix
Optional< const DictEntry * > MatchPrefix(const std::string &word) const
Matches the longest matched prefix of a word.
Definition Dict.hpp:96
opencc::Dict::Match
Optional< const DictEntry * > Match(const std::string &word) const
Matches a word exactly and returns the DictEntry or Optional::Null().
Definition Dict.hpp:74
opencc::Dict::MatchPrefix
Optional< const DictEntry * > MatchPrefix(const char *word) const
Matches the longest matched prefix of a word.
Definition Dict.hpp:89
opencc::Dict::GetMatchPolicy
virtual DictGroupMatchPolicy GetMatchPolicy() const
Returns the match policy when this dictionary is a group.
Definition Dict.hpp:138
opencc::Dict::GetDictGroupItems
virtual const std::list< DictPtr > * GetDictGroupItems() const
Returns child dictionaries when this dictionary is a group.
Definition Dict.hpp:129
opencc::Dict::MatchPrefix
virtual Optional< const DictEntry * > MatchPrefix(const char *word, size_t len) const
Matches the longest matched prefix of a word.
Definition Dict.cpp:25
opencc::Dict::MatchAllPrefixes
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
opencc::Dict::MatchAllPrefixes
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
opencc::Optional
A class that wraps type T into a nullable type.
Definition Optional.hpp:26
opencc::PrefixMatchView
Result of a PrefixMatch fast-path lookup.
Definition Dict.hpp:52