librime: candidate.h Source File

| librime 1.2 Rime Input Method Engine, the core library

candidate.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-05-22 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_CANDIDATE_H_
8#define RIME_CANDIDATE_H_
9
10#include <rime/common.h>
11
12namespace rime {
13
14 class Candidate {
15 public:
16 Candidate() = default;
17 Candidate(const string& type, size_t start, size_t end, double quality = 0.)
18 : type_(type), start_(start), end_(end), quality_(quality) {}
19 virtual ~Candidate() = default;
20
21 static an<Candidate> GetGenuineCandidate(const an<Candidate>& cand);
22 static vector<of<Candidate>> GetGenuineCandidates(const an<Candidate>& cand);
23 int compare(const Candidate& other);
24
25 // recognized by translators in learning phase
26 const string& type() const { return type_; }
27 // [start, end) mark a range in the input that the candidate corresponds to
28 size_t start() const { return start_; }
29 size_t end() const { return end_; }
30 double quality() const { return quality_; }
31
32 // candidate text to commit
33 virtual const string& text() const = 0;
34 // (optional)
35 virtual string comment() const { return string(); }
36 // text shown in the preedit area, replacing input string (optional)
37 virtual string preedit() const { return string(); }
38
39 void set_type(const string& type) { type_ = type; }
40 void set_start(size_t start) { start_ = start; }
41 void set_end(size_t end) { end_ = end; }
42 void set_quality(double quality) { quality_ = quality; }
43
44 private:
45 string type_;
46 size_t start_ = 0;
47 size_t end_ = 0;
48 double quality_ = 0.;
49};
50
51 using CandidateQueue = list<of<Candidate>>;
52 using CandidateList = vector<of<Candidate>>;
53
54// useful implementations
55
56 class SimpleCandidate : public Candidate {
57 public:
58 SimpleCandidate() = default;
59 SimpleCandidate(const string& type,
60 size_t start,
61 size_t end,
62 const string& text,
63 const string& comment = string(),
64 const string& preedit = string())
65 : Candidate(type, start, end),
66 text_(text),
67 comment_(comment),
68 preedit_(preedit) {}
69
70 const string& text() const { return text_; }
71 string comment() const { return comment_; }
72 string preedit() const { return preedit_; }
73
74 void set_text(const string& text) { text_ = text; }
75 void set_comment(const string& comment) { comment_ = comment; }
76 void set_preedit(const string& preedit) { preedit_ = preedit; }
77
78 protected:
79 string text_;
80 string comment_;
81 string preedit_;
82};
83
84 class ShadowCandidate : public Candidate {
85 public:
86 ShadowCandidate(const an<Candidate>& item,
87 const string& type,
88 const string& text = string(),
89 const string& comment = string(),
90 const bool inherit_comment = true)
91 : Candidate(type, item->start(), item->end(), item->quality()),
92 text_(text),
93 comment_(comment),
94 item_(item),
95 inherit_comment_(inherit_comment) {}
96
97 const string& text() const { return text_.empty() ? item_->text() : text_; }
98 string comment() const {
99 return inherit_comment_ && comment_.empty() ? item_->comment() : comment_;
100 }
101 string preedit() const { return item_->preedit(); }
102
103 const an<Candidate>& item() const { return item_; }
104
105 protected:
106 string text_;
107 string comment_;
108 an<Candidate> item_;
109 bool inherit_comment_;
110};
111
112 class UniquifiedCandidate : public Candidate {
113 public:
114 UniquifiedCandidate(const an<Candidate>& item,
115 const string& type,
116 const string& text = string(),
117 const string& comment = string())
118 : Candidate(type, item->start(), item->end(), item->quality()),
119 text_(text),
120 comment_(comment) {
121 Append(item);
122 }
123
124 const string& text() const {
125 return text_.empty() && !items_.empty() ? items_.front()->text() : text_;
126 }
127 string comment() const {
128 return comment_.empty() && !items_.empty() ? items_.front()->comment()
129 : comment_;
130 }
131 string preedit() const {
132 return !items_.empty() ? items_.front()->preedit() : string();
133 }
134
135 void Append(an<Candidate> item) {
136 items_.push_back(item);
137 if (quality() < item->quality())
138 set_quality(item->quality());
139 }
140 const CandidateList& items() const { return items_; }
141
142 protected:
143 string text_;
144 string comment_;
145 CandidateList items_;
146};
147
148} // namespace rime
149
150#endif // RIME_CANDIDATE_H_
common.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::CandidateQueue
list< of< Candidate > > CandidateQueue
Definition candidate.h:51
rime::CandidateList
vector< of< Candidate > > CandidateList
Definition candidate.h:52
rime::Candidate::set_type
void set_type(const string &type)
Definition candidate.h:39
rime::Candidate::compare
int compare(const Candidate &other)
Definition candidate.cc:34
rime::Candidate::set_quality
void set_quality(double quality)
Definition candidate.h:42
rime::Candidate::set_end
void set_end(size_t end)
Definition candidate.h:41
rime::Candidate::set_start
void set_start(size_t start)
Definition candidate.h:40
rime::Candidate::comment
virtual string comment() const
Definition candidate.h:35
rime::Candidate::preedit
virtual string preedit() const
Definition candidate.h:37
rime::Candidate::quality
double quality() const
Definition candidate.h:30
rime::Candidate::GetGenuineCandidates
static vector< of< Candidate > > GetGenuineCandidates(const an< Candidate > &cand)
Definition candidate.cc:21
rime::Candidate::Candidate
Candidate()=default
rime::Candidate::type
const string & type() const
Definition candidate.h:26
rime::Candidate::GetGenuineCandidate
static an< Candidate > GetGenuineCandidate(const an< Candidate > &cand)
Definition candidate.cc:16
rime::Candidate::~Candidate
virtual ~Candidate()=default
rime::Candidate::start
size_t start() const
Definition candidate.h:28
rime::Candidate::text
virtual const string & text() const =0
rime::Candidate::Candidate
Candidate(const string &type, size_t start, size_t end, double quality=0.)
Definition candidate.h:17
rime::Candidate::end
size_t end() const
Definition candidate.h:29
rime::SimpleCandidate::comment_
string comment_
Definition candidate.h:80
rime::SimpleCandidate::text_
string text_
Definition candidate.h:79
rime::SimpleCandidate::text
const string & text() const
Definition candidate.h:70
rime::SimpleCandidate::set_text
void set_text(const string &text)
Definition candidate.h:74
rime::SimpleCandidate::preedit
string preedit() const
Definition candidate.h:72
rime::SimpleCandidate::set_preedit
void set_preedit(const string &preedit)
Definition candidate.h:76
rime::SimpleCandidate::SimpleCandidate
SimpleCandidate()=default
rime::SimpleCandidate::set_comment
void set_comment(const string &comment)
Definition candidate.h:75
rime::SimpleCandidate::comment
string comment() const
Definition candidate.h:71
rime::SimpleCandidate::preedit_
string preedit_
Definition candidate.h:81
rime::SimpleCandidate::SimpleCandidate
SimpleCandidate(const string &type, size_t start, size_t end, const string &text, const string &comment=string(), const string &preedit=string())
Definition candidate.h:59
rime::ShadowCandidate::item_
an< Candidate > item_
Definition candidate.h:108
rime::ShadowCandidate::ShadowCandidate
ShadowCandidate(const an< Candidate > &item, const string &type, const string &text=string(), const string &comment=string(), const bool inherit_comment=true)
Definition candidate.h:86
rime::ShadowCandidate::text_
string text_
Definition candidate.h:106
rime::ShadowCandidate::preedit
string preedit() const
Definition candidate.h:101
rime::ShadowCandidate::comment_
string comment_
Definition candidate.h:107
rime::ShadowCandidate::text
const string & text() const
Definition candidate.h:97
rime::ShadowCandidate::comment
string comment() const
Definition candidate.h:98
rime::ShadowCandidate::inherit_comment_
bool inherit_comment_
Definition candidate.h:109
rime::ShadowCandidate::item
const an< Candidate > & item() const
Definition candidate.h:103
rime::UniquifiedCandidate::UniquifiedCandidate
UniquifiedCandidate(const an< Candidate > &item, const string &type, const string &text=string(), const string &comment=string())
Definition candidate.h:114
rime::UniquifiedCandidate::text_
string text_
Definition candidate.h:143
rime::UniquifiedCandidate::preedit
string preedit() const
Definition candidate.h:131
rime::UniquifiedCandidate::items
const CandidateList & items() const
Definition candidate.h:140
rime::UniquifiedCandidate::comment_
string comment_
Definition candidate.h:144
rime::UniquifiedCandidate::text
const string & text() const
Definition candidate.h:124
rime::UniquifiedCandidate::items_
CandidateList items_
Definition candidate.h:145
rime::UniquifiedCandidate::Append
void Append(an< Candidate > item)
Definition candidate.h:135
rime::UniquifiedCandidate::comment
string comment() const
Definition candidate.h:127