forked from rime/librime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation.h
82 lines (65 loc) · 1.66 KB
/
segmentation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2011-05-15 GONG Chen <chen.sst@gmail.com>
//
#ifndef RIME_SEGMENTATION_H_
#define RIME_SEGMENTATION_H_
#include <rime_api.h>
#include <rime/common.h>
namespace rime {
class Candidate;
class Menu;
struct Segment {
enum Status {
kVoid,
kGuess,
kSelected,
kConfirmed,
};
Status status = kVoid;
size_t start = 0;
size_t end = 0;
size_t length = 0;
set<string> tags;
an<Menu> menu;
size_t selected_index = 0;
string prompt;
Segment() = default;
Segment(int start_pos, int end_pos)
: start(start_pos), end(end_pos), length(end_pos - start_pos) {}
void Clear() {
status = kVoid;
tags.clear();
menu.reset();
selected_index = 0;
prompt.clear();
}
void Close();
bool Reopen(size_t caret_pos);
bool HasTag(const string& tag) const { return tags.find(tag) != tags.end(); }
an<Candidate> GetCandidateAt(size_t index) const;
an<Candidate> GetSelectedCandidate() const;
};
class RIME_API Segmentation : public vector<Segment> {
public:
Segmentation();
virtual ~Segmentation() {}
void Reset(const string& input);
void Reset(size_t num_segments);
bool AddSegment(Segment segment);
bool Forward();
bool Trim();
bool HasFinishedSegmentation() const;
size_t GetCurrentStartPosition() const;
size_t GetCurrentEndPosition() const;
size_t GetCurrentSegmentLength() const;
size_t GetConfirmedPosition() const;
const string& input() const { return input_; }
protected:
string input_;
};
std::ostream& operator<<(std::ostream& out, const Segmentation& segmentation);
} // namespace rime
#endif // RIME_SEGMENTATION_H_