librime: vocabulary.h Source File

| librime 1.2 Rime Input Method Engine, the core library

vocabulary.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-07-10 GONG Chen <chen.sst@gmail.com>
6//
7
8#ifndef RIME_VOCABULARY_H_
9#define RIME_VOCABULARY_H_
10
11#include <stdint.h>
12#include <rime_api.h>
13#include <rime/common.h>
14
15namespace rime {
16
17 using Syllabary = set<string>;
18
19using SyllableId = int32_t;
20
21 class Code : public vector<SyllableId> {
22 public:
23 Code() = default;
24 Code(const Code::const_iterator& begin, const Code::const_iterator& end)
25 : vector<SyllableId>(begin, end) {}
26
27 static const size_t kIndexCodeMaxLength = 3;
28
29 bool operator<(const Code& other) const;
30 bool operator==(const Code& other) const;
31
32 void CreateIndex(Code* index_code);
33
34 string ToString() const;
35};
36
37 struct ShortDictEntry {
38 string text;
39 Code code; // multi-syllable code from prism
40 double weight = 0.0;
41
42 ShortDictEntry() = default;
43 bool operator<(const ShortDictEntry& other) const;
44};
45
46 struct DictEntry {
47 string text;
48 string comment;
49 string preedit;
50 Code code; // multi-syllable code from prism
51 string custom_code; // user defined code
52 double weight = 0.0;
53 // 全碼匹配長度積分，記錄該詞條對應的路徑中，屬於「全拼」部分的字符總長度
54 double quality_len = 0.0;
55 int commit_count = 0;
56 int remaining_code_length = 0;
57 int matching_code_size = 0;
58
59 DictEntry() = default;
60 ShortDictEntry ToShort() const { return {text, code, weight}; }
61 bool IsExactMatch() const {
62 return matching_code_size == 0 || matching_code_size == code.size();
63 }
64 bool IsPredictiveMatch() const {
65 return matching_code_size != 0 && matching_code_size < code.size();
66 }
67 bool operator<(const DictEntry& other) const;
68};
69
70 class ShortDictEntryList : public vector<of<ShortDictEntry>> {
71 public:
72 void Sort();
73 void SortRange(size_t start, size_t count);
74};
75
76 class DictEntryList : public vector<of<DictEntry>> {
77 public:
78 void Sort();
79 void SortRange(size_t start, size_t count);
80};
81
82 using DictEntryFilter = function<bool(an<DictEntry> entry)>;
83
84 class RIME_DLL DictEntryFilterBinder {
85 public:
86 virtual ~DictEntryFilterBinder() = default;
87 virtual void AddFilter(DictEntryFilter filter);
88
89 protected:
90 DictEntryFilter filter_;
91};
92
93class Vocabulary;
94
95 struct VocabularyPage {
96 ShortDictEntryList entries;
97 an<Vocabulary> next_level;
98};
99
100 class Vocabulary : public map<int, VocabularyPage> {
101 public:
102 ShortDictEntryList* LocateEntries(const Code& code);
103 void SortHomophones();
104};
105
106// word -> { code, ... }
107 using ReverseLookupTable = hash_map<string, set<string>>;
108
109} // namespace rime
110
111#endif // RIME_VOCABULARY_H_
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
common.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::Syllabary
set< string > Syllabary
Definition vocabulary.h:17
rime::DictEntryFilter
function< bool(an< DictEntry > entry)> DictEntryFilter
Definition vocabulary.h:82
rime::hash_map
boost::unordered_map< Key, T > hash_map
Definition common.h:53
rime::SyllableId
int32_t SyllableId
Definition syllabifier.h:21
rime::ReverseLookupTable
hash_map< string, set< string > > ReverseLookupTable
Definition vocabulary.h:107
rime::Code
Definition vocabulary.h:21
rime::Code::Code
Code(const Code::const_iterator &begin, const Code::const_iterator &end)
Definition vocabulary.h:24
rime::Code::Code
Code()=default
rime::Code::kIndexCodeMaxLength
static const size_t kIndexCodeMaxLength
Definition vocabulary.h:27
rime::Code::operator==
bool operator==(const Code &other) const
Definition vocabulary.cc:25
rime::Code::CreateIndex
void CreateIndex(Code *index_code)
Definition vocabulary.cc:35
rime::Code::ToString
string ToString() const
Definition vocabulary.cc:46
rime::Code::operator<
bool operator<(const Code &other) const
Definition vocabulary.cc:15
rime::ShortDictEntry
Definition vocabulary.h:37
rime::ShortDictEntry::operator<
bool operator<(const ShortDictEntry &other) const
Definition vocabulary.cc:60
rime::ShortDictEntry::ShortDictEntry
ShortDictEntry()=default
rime::ShortDictEntry::weight
double weight
Definition vocabulary.h:40
rime::ShortDictEntry::text
string text
Definition vocabulary.h:38
rime::ShortDictEntry::code
Code code
Definition vocabulary.h:39
rime::DictEntry
Definition vocabulary.h:46
rime::DictEntry::IsExactMatch
bool IsExactMatch() const
Definition vocabulary.h:61
rime::DictEntry::remaining_code_length
int remaining_code_length
Definition vocabulary.h:56
rime::DictEntry::DictEntry
DictEntry()=default
rime::DictEntry::matching_code_size
int matching_code_size
Definition vocabulary.h:57
rime::DictEntry::weight
double weight
Definition vocabulary.h:52
rime::DictEntry::custom_code
string custom_code
Definition vocabulary.h:51
rime::DictEntry::quality_len
double quality_len
Definition vocabulary.h:54
rime::DictEntry::comment
string comment
Definition vocabulary.h:48
rime::DictEntry::ToShort
ShortDictEntry ToShort() const
Definition vocabulary.h:60
rime::DictEntry::preedit
string preedit
Definition vocabulary.h:49
rime::DictEntry::operator<
bool operator<(const DictEntry &other) const
Definition vocabulary.cc:68
rime::DictEntry::text
string text
Definition vocabulary.h:47
rime::DictEntry::IsPredictiveMatch
bool IsPredictiveMatch() const
Definition vocabulary.h:64
rime::DictEntry::commit_count
int commit_count
Definition vocabulary.h:55
rime::DictEntry::code
Code code
Definition vocabulary.h:50
rime::ShortDictEntryList
Definition vocabulary.h:70
rime::ShortDictEntryList::Sort
void Sort()
Definition vocabulary.cc:96
rime::ShortDictEntryList::SortRange
void SortRange(size_t start, size_t count)
Definition vocabulary.cc:100
rime::DictEntryList
Definition vocabulary.h:76
rime::DictEntryList::SortRange
void SortRange(size_t start, size_t count)
Definition vocabulary.cc:108
rime::DictEntryList::Sort
void Sort()
Definition vocabulary.cc:104
rime::DictEntryFilterBinder
Definition vocabulary.h:84
rime::DictEntryFilterBinder::AddFilter
virtual void AddFilter(DictEntryFilter filter)
Definition vocabulary.cc:112
rime::DictEntryFilterBinder::~DictEntryFilterBinder
virtual ~DictEntryFilterBinder()=default
rime::DictEntryFilterBinder::filter_
DictEntryFilter filter_
Definition vocabulary.h:90
rime::VocabularyPage
Definition vocabulary.h:95
rime::VocabularyPage::next_level
an< Vocabulary > next_level
Definition vocabulary.h:97
rime::VocabularyPage::entries
ShortDictEntryList entries
Definition vocabulary.h:96
rime::Vocabulary
Definition vocabulary.h:100
rime::Vocabulary::LocateEntries
ShortDictEntryList * LocateEntries(const Code &code)
Definition vocabulary.cc:123
rime::Vocabulary::SortHomophones
void SortHomophones()
Definition vocabulary.cc:143