-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmatch_handlers.h
265 lines (242 loc) · 7.11 KB
/
match_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifndef __REFLECT_MATCH_HANDLERS_HEADER__
#define __REFLECT_MATCH_HANDLERS_HEADER__
#include "document.h"
#include "file.h"
#include "tagger_types.h"
#include <climits>
#include <vector>
#include <unordered_map>
using namespace std;
typedef vector<SERIAL> SERIALS;
typedef unordered_set<SERIAL> ENTITY_SET;
typedef unordered_map<int, int> TYPE_COUNT_MAP;
class EntityTypeMap : public unordered_map<SERIAL, int>
{
public:
EntityTypeMap(const char* entities_filename);
};
class IMatchHandler
{
public:
virtual ~IMatchHandler() {};
virtual void process(Matches& matches) = 0;
};
class MatchHandler : public IMatchHandler
{
public:
virtual void process(Matches& matches);
};
class DisambiguationMatchHandler : public IMatchHandler
{
public:
virtual void process(Matches& matches);
};
class GroupMatchHandler : public DisambiguationMatchHandler
{
private:
int type;
EntityTypeMap* entity_type_map;
unordered_map<SERIAL, SERIALS> entity_group_map;
public:
GroupMatchHandler(int type, const char* groups_filename);
GroupMatchHandler(EntityTypeMap* entity_type_map, const char* groups_filename);
public:
void load_groups(const char* groups_filename);
virtual void process(Matches& matches);
};
////////////////////////////////////////////////////////////////////////////////
EntityTypeMap::EntityTypeMap(const char* entities_filename)
{
InputFile entities_file(entities_filename);
while (true) {
vector<char *> fields = entities_file.get_fields();
int size = fields.size();
if (size == 0) {
break;
}
if (size >= 2) {
SERIAL serial = atoi(fields[0]);
int type = atoi(fields[1]);
(*this)[serial] = type;
}
for (vector<char*>::iterator it = fields.begin(); it != fields.end(); it++) {
delete *it;
}
}
}
////////////////////////////////////////////////////////////////////////////////
void MatchHandler::process(Matches& matches)
{
}
////////////////////////////////////////////////////////////////////////////////
void DisambiguationMatchHandler::process(Matches& matches)
{
ENTITY_SET entity_set;
for (Matches::iterator it = matches.begin(); it != matches.end(); it++) {
if ((*it)->size == 1) {
entity_set.insert((*it)->entities[0].id.serial);
}
}
for (Matches::iterator it = matches.begin(); it != matches.end(); it++) {
int count = 0;
for (int i = 0; i < (*it)->size; i++) {
if (entity_set.find((*it)->entities[i].id.serial) != entity_set.end()) {
count++;
}
}
if (count == 0) {
for (int i = 0; i < (*it)->size; i++) {
entity_set.insert((*it)->entities[i].id.serial);
}
}
else if (count < (*it)->size) {
Entity* entities = new Entity[count];
int index = 0;
for (int i = 0; i < (*it)->size; i++) {
if (entity_set.find((*it)->entities[i].id.serial) != entity_set.end()) {
entities[index] = (*it)->entities[i];
index++;
}
}
delete (*it)->entities;
(*it)->entities = entities;
(*it)->size = count;
}
}
}
////////////////////////////////////////////////////////////////////////////////
GroupMatchHandler::GroupMatchHandler(int type, const char* groups_filename)
{
this->type = type;
this->entity_type_map = NULL;
this->load_groups(groups_filename);
}
GroupMatchHandler::GroupMatchHandler(EntityTypeMap* entity_type_map, const char* groups_filename)
{
this->type = 0;
this->entity_type_map = entity_type_map;
this->load_groups(groups_filename);
}
void GroupMatchHandler::load_groups(const char* groups_filename)
{
InputFile groups_file(groups_filename);
while (true) {
vector<char *> fields = groups_file.get_fields();
int size = fields.size();
if (size == 0) {
break;
}
if (size >= 2) {
SERIAL serial1 = atoi(fields[0]);
SERIAL serial2 = atoi(fields[1]);
unordered_map<SERIAL, SERIALS>::iterator it = this->entity_group_map.find(serial1);
if (it == this->entity_group_map.end()) {
this->entity_group_map[serial1] = SERIALS();
it = this->entity_group_map.find(serial1);
}
it->second.push_back(serial2);
}
for (vector<char*>::iterator it = fields.begin(); it != fields.end(); it++) {
delete *it;
}
}
}
void GroupMatchHandler::process(Matches& matches)
{
DisambiguationMatchHandler::process(matches);
TYPE_COUNT_MAP type_count_map;
for (Matches::iterator match_it = matches.begin(); match_it != matches.end(); match_it++) {
for (int i = 0; i < (*match_it)->size; i++) {
int type = (*match_it)->entities[i].type;
TYPE_COUNT_MAP::iterator type_count_it = type_count_map.find(type);
if (type_count_it == type_count_map.end()) {
type_count_map[type] = 1;
}
else {
type_count_it->second++;
}
}
}
for (Matches::iterator match_it = matches.begin(); match_it != matches.end(); match_it++) {
unordered_set<int> excluded;
bool ambiguous = true;
while (ambiguous) {
unordered_map<SERIAL, int> group_count;
int size = 0;
for (int i = 0; i < (*match_it)->size; i++) {
if (excluded.find((*match_it)->entities[i].type) == excluded.end()) {
unordered_map<SERIAL, SERIALS>::iterator entity_group_it = this->entity_group_map.find((*match_it)->entities[i].id.serial);
if (entity_group_it != this->entity_group_map.end()) {
for (SERIALS::iterator serials_it = entity_group_it->second.begin(); serials_it != entity_group_it->second.end(); serials_it++) {
unordered_map<SERIAL,int>::iterator group_count_it = group_count.find(*serials_it);
if (group_count_it == group_count.end()) {
group_count[*serials_it] = 1;
}
else {
group_count_it->second++;
}
}
}
size++;
}
}
SERIALS common_groups;
for (unordered_map<SERIAL, int>::iterator group_count_it = group_count.begin(); group_count_it != group_count.end(); group_count_it++) {
if (group_count_it->second == size) {
common_groups.push_back(group_count_it->first);
}
}
if (size == 0) {
delete (*match_it)->entities;
(*match_it)->entities = NULL;
(*match_it)->size = 0;
ambiguous = false;
}
else if (size == 1 or common_groups.size()) {
int n = common_groups.size();
size += n;
Entity* entities = new Entity[size];
int i = 0;
for (int j = 0; j < (*match_it)->size; j++) {
if (excluded.find((*match_it)->entities[j].type) == excluded.end()) {
entities[i] = (*match_it)->entities[j];
i++;
}
}
for (int j = 0; j < n; j++) {
entities[i].id.serial = common_groups[j];
if (this->entity_type_map != NULL) {
entities[i].type = (*this->entity_type_map)[common_groups[j]];
}
else {
entities[i].type = this->type;
}
i++;
}
delete (*match_it)->entities;
(*match_it)->entities = entities;
(*match_it)->size = size;
ambiguous = false;
}
else {
int min_count = INT_MAX;
for (int i = 0; i < (*match_it)->size; i++) {
int type = (*match_it)->entities[i].type;
if (excluded.find(type) == excluded.end()) {
int count = type_count_map[type];
if (count < min_count) {
min_count = count;
}
}
}
for (int i = 0; i < (*match_it)->size; i++) {
int type = (*match_it)->entities[i].type;
if (type_count_map[type] == min_count) {
excluded.insert(type);
}
}
}
}
}
}
#endif