-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyparser.cc
More file actions
167 lines (138 loc) · 4.65 KB
/
Copy pathmyparser.cc
File metadata and controls
167 lines (138 loc) · 4.65 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
#include "myparser.h"
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
using namespace std;
int line_top = -1;
int line_right = -1;
int line_bottom = -1;
int line_left = -1;
int prev_top = -1;
int prev_right = -1;
int prev_bottom = -1;
int prev_left = -1;
int bottom = -1;
int top = -1;
int left_ = -1;
int right_ = -1;
bool first_in_line =true;
bool new_element = false;
vector<TextElement> *textElements;
string current_element = "";
int current_element_left = -1;
int current_element_right = -1;
int current_element_top = -1;
int current_element_bottom = -1;
MySaxParser::MySaxParser(vector<TextElement> *& te)
: xmlpp::SaxParser()
{
textElements = te;
}
MySaxParser::~MySaxParser()
{
}
void MySaxParser::split(const string& s, char c,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
void MySaxParser::on_start_document()
{
// std::cout << "on_start_document()" << std::endl;
}
void MySaxParser::on_end_document()
{
// std::cout << "on_end_document()" << std::endl;
}
void MySaxParser::on_start_element(const Glib::ustring& name,
const AttributeList& attributes)
{
//std::cout << "node name=" << name << std::endl;
//std::cout << "node name=" <<name.compare("span")<< std::endl;
if (name.compare("span") == 0) {
new_element = true;
//std::cout << "attribute" << attributes[0].value << std::endl;
//std::cout << "attribute" << attributes[2].value << std::endl;
//std::string bbox = attributes[2].value.split(";")[0];
//std::cout << "attribute" << bbox << std::endl;
vector<std::string> * n = new vector<std::string>();
split(attributes[2].value,';',*n);
vector<std::string> * item = new vector<std::string>();
split(n->front(),' ',*item);
// std::cout<< "New : "<<attributes[2].value<<std::endl;
prev_left = left_;
prev_bottom = bottom;
prev_right = right_;
prev_top = top;
bottom = atoi(item->back().c_str());
item->pop_back();
right_ = atoi(item->back().c_str());
item->pop_back();
top = atoi(item->back().c_str());
item->pop_back();
left_ = atoi(item->back().c_str());
item->pop_back();
//std::cout << "attribute " <<left_<<" "<< top << " "<< right_ << " "<<bottom << std::endl;
if(attributes[0].value.compare("ocr_line") == 0) {
//std::cout<<std::endl;
line_left = left_;
line_bottom = bottom;
line_right = right_;
line_top = top;
first_in_line = true;
// std::cout<< "New Line: "<<attributes[2].value<<std::endl;
}
}
}
void MySaxParser::on_end_element(const Glib::ustring& name)
{
//std::cout << "on_end_element()" << std::endl;
}
void MySaxParser::on_characters(const Glib::ustring& text)
{
std::string whitespaces (" \t\f\v\n\r");
//std::cout <<" jkd "<< text.size()<<" "<<(text.find_first_not_of(whitespaces) == std::string::npos) << " "<< text <<std::endl;
if((text.find_first_not_of(whitespaces) == std::string::npos) == 0 ) {
// std::cout<< first_in_line<< " "<<prev_right<< " " << left_ << " "<< line_bottom << " "<<line_top<<" " <<line_bottom - line_top << " " <<left_ - prev_right<<endl;
//std::cout << " " << text << " ";
if( new_element && (first_in_line || (left_ - prev_right) > (line_bottom - line_top)) ) {
// std::cout << "One Element "<<current_element << " " <<current_element_left << " "<<current_element_right << " "<<current_element_top<<" "<<current_element_bottom<<endl;
if (!current_element.empty()) {
TextElement* t = new TextElement(current_element,current_element_top,current_element_left,(current_element_right-current_element_left),(current_element_bottom - current_element_top),-1,"text","text");
textElements->push_back(*t);
}
current_element = "";
current_element_left = left_;
}
current_element += " " +text;
current_element_right = right_;
current_element_top = line_top;
current_element_bottom = line_bottom;
}
first_in_line = false;
new_element = false;
}
void MySaxParser::on_comment(const Glib::ustring& text)
{
//std::cout << "on_comment(): " << text << std::endl;
}
void MySaxParser::on_warning(const Glib::ustring& text)
{
std::cout << "on_warning(): " << text << std::endl;
}
void MySaxParser::on_error(const Glib::ustring& text)
{
std::cout << "on_error(): " << text << std::endl;
}
void MySaxParser::on_fatal_error(const Glib::ustring& text)
{
std::cout << "on_fatal_error(): " << text << std::endl;
}