librime: segmentation.h Source File

| 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
18 struct Segment {
19 enum Status {
20 kVoid,
21 kGuess,
22 kSelected,
23 kConfirmed,
24 };
25 Status status = kVoid;
26 size_t start = 0;
27 size_t end = 0;
28 size_t length = 0;
29 set<string> tags;
30 an<Menu> menu;
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();
43 selected_index = 0;
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;
57 an<Candidate> GetSelectedCandidate() const;
58};
59
60 class RIME_DLL Segmentation : public vector<Segment> {
61 public:
62 Segmentation();
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_
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::operator<<
std::ostream & operator<<(std::ostream &stream, const Reference &reference)
Definition config_compiler.cc:12
rime::Candidate
Definition candidate.h:14
rime::Menu
Definition menu.h:27
rime::Segment
Definition segmentation.h:18
rime::Segment::HasTag
bool HasTag(const string &tag) const
Definition segmentation.h:50
rime::Segment::menu
an< Menu > menu
Definition segmentation.h:30
rime::Segment::Clear
void Clear()
Definition segmentation.h:39
rime::Segment::GetCandidateAt
an< Candidate > GetCandidateAt(size_t index) const
Definition segmentation.cc:45
rime::Segment::Reopen
bool Reopen(size_t caret_pos)
Definition segmentation.cc:26
rime::Segment::prompt
string prompt
Definition segmentation.h:32
rime::Segment::status
Status status
Definition segmentation.h:25
rime::Segment::tags
set< string > tags
Definition segmentation.h:29
rime::Segment::Segment
Segment(int start_pos, int end_pos)
Definition segmentation.h:36
rime::Segment::Segment
Segment()=default
rime::Segment::end
size_t end
Definition segmentation.h:27
rime::Segment::Close
void Close()
Definition segmentation.cc:17
rime::Segment::start
size_t start
Definition segmentation.h:26
rime::Segment::selected_index
size_t selected_index
Definition segmentation.h:31
rime::Segment::length
size_t length
Definition segmentation.h:28
rime::Segment::HasAnyTagIn
bool HasAnyTagIn(const vector< string > &tags) const
Definition segmentation.h:51
rime::Segment::Status
Status
Definition segmentation.h:19
rime::Segment::kSelected
@ kSelected
Definition segmentation.h:22
rime::Segment::kGuess
@ kGuess
Definition segmentation.h:21
rime::Segment::kConfirmed
@ kConfirmed
Definition segmentation.h:23
rime::Segment::kVoid
@ kVoid
Definition segmentation.h:20
rime::Segment::GetSelectedCandidate
an< Candidate > GetSelectedCandidate() const
Definition segmentation.cc:51
rime::Segmentation
Definition segmentation.h:60
rime::Segmentation::Segmentation
Segmentation()
Definition segmentation.cc:55
rime::Segmentation::input
const string & input() const
Definition segmentation.h:76
rime::Segmentation::input_
string input_
Definition segmentation.h:79
rime::Segmentation::~Segmentation
virtual ~Segmentation()
Definition segmentation.h:63