Open Chinese Convert 1.4.2+g71621a0e1.dirty
A project for conversion between Traditional and Simplified Chinese
Loading...
Searching...
No Matches
MarisaDict.hpp
1/*
2 * Open Chinese Convert
3 *
4 * Copyright 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 <atomic>
22#include <functional>
23#include <mutex>
24
25#include "Common.hpp"
26#include "SerializableDict.hpp"
27
28namespace marisa {
29class Trie;
30}
31
32namespace opencc {
37class OPENCC_EXPORT MarisaDict : public Dict, public SerializableDict {
38public:
39 virtual ~MarisaDict() override;
40
41 virtual size_t KeyMaxLength() const override;
42
43 virtual Optional<const DictEntry*> Match(const char* word,
44 size_t len) const override;
45
46 virtual Optional<const DictEntry*> MatchPrefix(const char* word,
47 size_t len) const override;
48
49 virtual std::vector<const DictEntry*> MatchAllPrefixes(
50 const char* word, size_t len) const override;
51
52 virtual LexiconPtr GetLexicon() const override;
53
54 virtual bool SupportsFastPrefixMatch() const override { return true; }
55
56 virtual PrefixMatchView MatchPrefixValue(const char* word,
57 size_t len) const override;
58
66 bool EnumerateKeys(const std::function<void(const char*, size_t)>& cb) const;
67
68 virtual void SerializeToFile(FILE* fp) const override;
69
73 static MarisaDictPtr NewFromDict(const Dict& thatDict);
74
75 static MarisaDictPtr NewFromFile(FILE* fp);
76
77 static MarisaDictPtr NewFromBuffer(const char* data, size_t size);
78
79 // Exposed for testing only.
80 bool IsLexiconReconstructed() const {
81 return lexiconReconstructed.load(std::memory_order_acquire);
82 }
83
84private:
85 MarisaDict();
86
87 void LoadFromMappedBuffer();
88 void ReconstructLexicon() const;
89
90 mutable size_t maxLength;
91 mutable LexiconPtr lexicon;
92 mutable std::mutex lexiconMutex;
93 mutable std::atomic<bool> lexiconReconstructed;
94 LexiconPtr valuesLexicon;
95
96 class MarisaInternal;
97 std::unique_ptr<MarisaInternal> internal;
98};
99} // namespace opencc
Abstract class of dictionary.
Definition Dict.hpp:63
virtual Optional< const DictEntry * > MatchPrefix(const char *word, size_t len) const override
Matches the longest matched prefix of a word.
Definition MarisaDict.cpp:70
virtual Optional< const DictEntry * > Match(const char *word, size_t len) const override
Matches a word exactly and returns the DictEntry or Optional::Null().
Definition MarisaDict.cpp:54
virtual LexiconPtr GetLexicon() const override
Returns all entries in the dictionary.
Definition MarisaDict.cpp:101
virtual std::vector< const DictEntry * > MatchAllPrefixes(const char *word, size_t len) const override
Returns all matched prefixes of a word, sorted by the length (desc).
Definition MarisaDict.cpp:87
virtual size_t KeyMaxLength() const override
Returns the length of the longest key in the dictionary.
Definition MarisaDict.cpp:49
virtual bool SupportsFastPrefixMatch() const override
Returns true if this dict can handle prefix queries directly without PrefixMatch building a lookup ta...
Definition MarisaDict.hpp:54
A class that wraps type T into a nullable type.
Definition Optional.hpp:26
Serializable dictionary interface.
Definition SerializableDict.hpp:32
Result of a PrefixMatch fast-path lookup.
Definition Dict.hpp:52