-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterRules.cpp
More file actions
197 lines (154 loc) · 5.26 KB
/
CharacterRules.cpp
File metadata and controls
197 lines (154 loc) · 5.26 KB
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
#include "stdafx.h"
#include "CharacterRules.h"
std::map<Character, std::vector<Rule> > CharacterRules::m_rules;
Characters CharacterRules::m_numbers;
std::vector<std::pair<Character, Probability> > CharacterRules::recognize(const std::vector<const Image*> & images)
{
std::vector<Shape::ShapeWithAnchors> shapes;
for(std::vector<const Image*>::const_iterator it = images.begin(); it != images.end(); ++it)
{
Shape::ShapeWithAnchors shape = Shape::mostLikely(**it);
shapes.push_back(shape);
}
std::vector<std::pair<Character, Probability> > result;
result = filterInvalidShapes(shapes);
if (result.size() < 2 )
return result;
result = filterInvalidJoins(result, shapes);
if (result.size() < 2 )
return result;
result = filterInvalidSize(result, shapes);
return result;
}
struct filter_character_if_shapes_dont_match_t : public std::unary_function<std::pair<Character, std::vector<Rule> >, void> {
filter_character_if_shapes_dont_match_t(CharacterRules::CharacterProbabilities & _result, std::vector<Shape::ShapeWithAnchors> & _shapes)
: shapes(_shapes), result(_result) {}
void operator () (const argument_type & characterRules) {
for(std::vector<Rule>::const_iterator it = characterRules.second.begin(); it != characterRules.second.end(); ++it)
{
const Rule & rule = *it;
if (shapes.size() == rule.shapes.size())
{
std::vector<Shape::ShapeWithAnchors> filtered_shapes = shapes;
for(std::vector<Shape*>::const_iterator it = rule.shapes.begin(); it != rule.shapes.end(); ++it)
{
Shape & rule_shape = * * it;
for(std::vector<Shape::ShapeWithAnchors>::iterator it = filtered_shapes.begin(); it != filtered_shapes.end(); ++it)
{
Shape & filtered_shape = it->first;
if (Shape::mayBeEqual(rule_shape, filtered_shape))
{
filtered_shapes.erase(it);
break;
}
}
}
if (filtered_shapes.empty())
{
result.push_back(std::make_pair(characterRules.first, 1));
}
}
}
}
std::vector<Shape::ShapeWithAnchors> & shapes;
CharacterRules::CharacterProbabilities & result;
};
CharacterRules::CharacterProbabilities CharacterRules::filterInvalidShapes(std::vector<Shape::ShapeWithAnchors> & shapes)
{
CharacterRules::CharacterProbabilities result;
filter_character_if_shapes_dont_match_t filter_character_if_shapes_dont_match(result,shapes);
std::for_each(rules().begin(), rules().end(), filter_character_if_shapes_dont_match);
return result;
}
CharacterRules::CharacterProbabilities CharacterRules::filterInvalidJoins(CharacterProbabilities probs, std::vector<Shape::ShapeWithAnchors> & shapes)
{
CharacterProbabilities result;
for(CharacterProbabilities::iterator it = probs.begin(); it != probs.end(); ++it)
{
CharacterProbability & prob = *it;
if (mayMatchJoins(prob.first, shapes))
{
result.push_back(prob);
}
}
return result;
}
bool CharacterRules::mayMatchJoins(const Character & character, std::vector<Shape::ShapeWithAnchors> & shapes)
{
std::vector<Rule> & rules = m_rules[character];
for(std::vector<Rule>::iterator it = rules.begin(); it != rules.end(); ++it)
{
Rule & rule = *it;
if (mayMatchJoins(shapes, rule))
return true;
}
return false;
}
template<class T> std::vector<const T*> vec2ptr(const std::vector<T> & vec)
{
std::vector<const T*> result;
for (std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it)
result.push_back( &(*it) );
return result;
}
bool CharacterRules::mayMatchJoins(std::vector<Shape::ShapeWithAnchors> & r_shapes, Rule & rule)
{
std::vector<const Shape::ShapeWithAnchors*> shapes = vec2ptr(r_shapes);
std::sort(shapes.begin(), shapes.end());
do
{
if (rule.matches(shapes))
return true;
}
while(std::next_permutation(shapes.begin(), shapes.end()));
return false;
}
bool Rule::matches(const std::vector<const Shape::ShapeWithAnchors*> & shapes) const
{
for(std::vector<Join*>::const_iterator it = joins.begin(); it != joins.end(); ++it)
{
Join* join = *it;
const Shape::ShapeWithAnchors* first = shapes[shape_index(join->first->shape)];
const Shape::ShapeWithAnchors* second = shapes[shape_index(join->second->shape)];
if ( ! Shape::mayBeEqual(first->first, * join->first->shape) )
return false;
if ( ! Shape::mayBeEqual(second->first, * join->second->shape) )
return false;
if (! join->matches(*first, *second))
return false;
}
return true;
}
size_t Rule::shape_index(Shape* shape) const
{
return std::find(shapes.begin(), shapes.end(), shape) - shapes.begin();
}
CharacterRules::CharacterProbabilities CharacterRules::filterInvalidSize(CharacterProbabilities probs, std::vector<Shape::ShapeWithAnchors> & shapes)
{
return probs;
}
std::vector<std::pair<Character, Probability> > CharacterRules::recognize(const std::vector<Image> & images)
{
return CharacterRules::recognize(vec2ptr(images));
}
Character CharacterRules::recognizeSingle(const std::vector<Image> & images)
{
CharacterProbabilities probs = recognize(images);
if(probs.size() == 1)
return probs.front().first;
else
return Character();
}
Rule & CharacterRules::rule_for(const Character & character)
{
static bool sranded = false;
if (!sranded)
{
srand(clock());
sranded = true;
}
std::vector<Rule> & rules = m_rules[character];
if (rules.empty())
return * new Rule(); //mwahaha
return rules[rand() % rules.size()];
}