forked from larsjuhljensen/tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_handlers.h
195 lines (160 loc) · 4.83 KB
/
base_handlers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef __REFLECT_BASE_HANDLERS_HEADER__
#define __REFLECT_BASE_HANDLERS_HEADER__
#include "document.h"
#include "tagger_types.h"
using namespace std;
typedef vector<SERIAL> SERIALS;
typedef unordered_set<SERIAL> ENTITY_SET;
class IBatchHandler;
class IDocumentHandler
{
public:
virtual ~IDocumentHandler() {};
virtual void on_batch_begin() = 0;
virtual void on_batch_end() = 0;
virtual void on_document_begin(Document& document) = 0;
virtual void on_document_end(Document& document) = 0;
virtual void on_match(Document& document, Match* match) = 0;
virtual void process(Document& document, Matches& matches) = 0;
};
class DocumentHandler : public IDocumentHandler
{
protected:
IBatchHandler* batch_handler;
public:
DocumentHandler(IBatchHandler* batch_handler);
public:
virtual void on_batch_begin();
virtual void on_batch_end();
virtual void on_document_begin(Document& document);
virtual void on_document_end(Document& document);
virtual void on_match(Document& document, Match* match);
void process(Document& document, Matches& matches);
};
class StructuredDocumentHandler : public DocumentHandler
{
public:
StructuredDocumentHandler(IBatchHandler* batch_handler);
public:
virtual void on_paragraph_begin();
virtual void on_paragraph_end();
virtual void on_sentence_begin();
virtual void on_sentence_end();
void process(Document& document, Matches& matches);
};
class IBatchHandler
{
public:
virtual IDocumentHandler* create_document_handler() = 0;
virtual void on_batch_begin() = 0;
virtual void on_batch_end() = 0;
virtual void on_document_begin(Document& document) = 0;
virtual void on_document_end(Document& document) = 0;
virtual void on_match(Document& document, Match* match) = 0;
};
class BatchHandler : public IBatchHandler
{
public:
virtual IDocumentHandler* create_document_handler();
virtual void on_batch_begin();
virtual void on_batch_end();
virtual void on_document_begin(Document& document);
virtual void on_document_end(Document& document);
virtual void on_match(Document& document, Match* match);
};
////////////////////////////////////////////////////////////////////////////////
DocumentHandler::DocumentHandler(IBatchHandler* batch_handler)
{
this->batch_handler = batch_handler;
}
void DocumentHandler::on_batch_begin() {
}
void DocumentHandler::on_batch_end() {
}
void DocumentHandler::on_document_begin(Document& document) {
}
void DocumentHandler::on_document_end(Document& document) {
}
void DocumentHandler::on_match(Document& document, Match* match)
{
}
void DocumentHandler::process(Document& document, Matches& matches)
{
this->batch_handler->on_document_begin(document);
this->on_document_begin(document);
for (Matches::iterator match = matches.begin(); match != matches.end(); match++) {
this->on_match(document, *match);
this->batch_handler->on_match(document, *match);
}
this->on_document_end(document);
this->batch_handler->on_document_end(document);
}
////////////////////////////////////////////////////////////////////////////////
StructuredDocumentHandler::StructuredDocumentHandler(IBatchHandler* batch_handler)
: DocumentHandler(batch_handler)
{
}
void StructuredDocumentHandler::on_paragraph_begin() {
}
void StructuredDocumentHandler::on_paragraph_end() {
}
void StructuredDocumentHandler::on_sentence_begin() {
}
void StructuredDocumentHandler::on_sentence_end() {
}
void StructuredDocumentHandler::process(Document& document, Matches& matches)
{
this->batch_handler->on_document_begin(document);
this->on_document_begin(document);
Matches::iterator match = matches.begin();
Segments segments = document.get_segments();
Segments::iterator segment = segments.begin();
while (match != matches.end()) {
if (segment->paragraph_begin) {
this->on_paragraph_begin();
}
this->on_sentence_begin();
while (match != matches.end() && document.text+(*match)->start < segment->begin) {
match++;
}
if (match == matches.end()) {
this->on_sentence_end();
break;
}
if (document.text+(*match)->stop < segment->end) {
while (match != matches.end() && document.text+(*match)->stop < segment->end) {
this->on_match(document, *match);
this->batch_handler->on_match(document, *match);
match++;
}
}
this->on_sentence_end();
if (segment->paragraph_end) {
this->on_paragraph_end();
}
segment++;
}
this->on_document_end(document);
this->batch_handler->on_document_end(document);
}
////////////////////////////////////////////////////////////////////////////////
IDocumentHandler* BatchHandler::create_document_handler()
{
return new DocumentHandler(this);
}
void BatchHandler::on_batch_begin()
{
}
void BatchHandler::on_batch_end()
{
}
void BatchHandler::on_document_begin(Document& document)
{
}
void BatchHandler::on_document_end(Document& document)
{
}
void BatchHandler::on_match(Document& document, Match* match)
{
}
#endif