librime 1.2
Rime Input Method Engine, the core library
segmentation.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-05-15 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_SEGMENTATION_H_
8#define RIME_SEGMENTATION_H_
9
10#include <rime_api.h>
11#include <rime/common.h>
12
13namespace rime {
14
15class Candidate;
16class Menu;
17
18struct Segment {
19 enum Status {
24 };
26 size_t start = 0;
27 size_t end = 0;
28 size_t length = 0;
29 set<string> tags;
31 size_t selected_index = 0;
32 string prompt;
33
34 Segment() = default;
35
36 Segment(int start_pos, int end_pos)
37 : start(start_pos), end(end_pos), length(end_pos - start_pos) {}
38
39 void Clear() {
40 status = kVoid;
41 tags.clear();
42 menu.reset();
44 prompt.clear();
45 }
46
47 void Close();
48 bool Reopen(size_t caret_pos);
49
50 bool HasTag(const string& tag) const { return tags.find(tag) != tags.end(); }
51 bool HasAnyTagIn(const vector<string>& tags) const {
52 return std::any_of(tags.begin(), tags.end(),
53 [this](const string& tag) { return HasTag(tag); });
54 }
55
56 an<Candidate> GetCandidateAt(size_t index) const;
58};
59
60class RIME_DLL Segmentation : public vector<Segment> {
61 public:
63 virtual ~Segmentation() {}
64 void Reset(const string& input);
65 void Reset(size_t num_segments);
66 bool AddSegment(Segment segment);
67
68 bool Forward();
69 bool Trim();
70 bool HasFinishedSegmentation() const;
71 size_t GetCurrentStartPosition() const;
72 size_t GetCurrentEndPosition() const;
73 size_t GetCurrentSegmentLength() const;
74 size_t GetConfirmedPosition() const;
75
76 const string& input() const { return input_; }
77
78 protected:
79 string input_;
80};
81
82std::ostream& operator<<(std::ostream& out, const Segmentation& segmentation);
83
84} // namespace rime
85
86#endif // RIME_SEGMENTATION_H_
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
std::ostream & operator<<(std::ostream &stream, const Reference &reference)
Definition config_compiler.cc:12
Definition candidate.h:14
Definition menu.h:27
Definition segmentation.h:18
bool HasTag(const string &tag) const
Definition segmentation.h:50
an< Menu > menu
Definition segmentation.h:30
void Clear()
Definition segmentation.h:39
an< Candidate > GetCandidateAt(size_t index) const
Definition segmentation.cc:45
bool Reopen(size_t caret_pos)
Definition segmentation.cc:26
string prompt
Definition segmentation.h:32
Status status
Definition segmentation.h:25
set< string > tags
Definition segmentation.h:29
Segment(int start_pos, int end_pos)
Definition segmentation.h:36
Segment()=default
size_t end
Definition segmentation.h:27
void Close()
Definition segmentation.cc:17
size_t start
Definition segmentation.h:26
size_t selected_index
Definition segmentation.h:31
size_t length
Definition segmentation.h:28
bool HasAnyTagIn(const vector< string > &tags) const
Definition segmentation.h:51
Status
Definition segmentation.h:19
@ kSelected
Definition segmentation.h:22
@ kGuess
Definition segmentation.h:21
@ kConfirmed
Definition segmentation.h:23
@ kVoid
Definition segmentation.h:20
an< Candidate > GetSelectedCandidate() const
Definition segmentation.cc:51
Definition segmentation.h:60
Segmentation()
Definition segmentation.cc:55
const string & input() const
Definition segmentation.h:76
string input_
Definition segmentation.h:79
virtual ~Segmentation()
Definition segmentation.h:63