-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
211 lines (165 loc) · 6.18 KB
/
Parser.php
File metadata and controls
211 lines (165 loc) · 6.18 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
require_once ('Data.php');
require_once ('Utility/simple_html_dom.php');
class Parser {
public function __construct($sourcefile){
$dir = dirname($sourcefile);
$fn = basename($sourcefile);
// Rendering PDF source file to HTML
$res=exec('"' . $GLOBALS['config']['pdftohtml'] . '" -l 1 -c -i "' . $dir . "/" . $fn . '"');
$html_fn=$dir . "/" . basename($fn, ".pdf") . '-1.html';
// Parsing all DIV elements and putting text content into $this->content[y_position][x_position]
$html = file_get_html($html_fn);
foreach($html->find('div') as $element) {
$pos = $this->get_position( $element->style );
$this->content[ $pos['y'] ][ $pos['x'] ] = trim( html_entity_decode( str_replace(' ', ' ', strip_tags($element)) ) );
}
// Clean up temporary files
$mask = $dir . "/" . "*.html";
array_map( "unlink", glob( $mask ) );
}
public function parse(){
// Extract tested germ name
if ( false === ($tested_germ = $this->extract_tested_germ()) )
return false;
$germ = new Data($tested_germ);
// Extract data range
$data_range = $this->extract_data_range();
$pos = strpos($data_range, 'To');
$germ->set_data_range_from( substr($data_range, 0, $pos) );
$germ->set_data_range_to( substr($data_range, $pos+2) );
// Get the coordinates of the origin (DIV with text 'Antimicrobic')
$orig = $this->get_coordinates('Antimicrobic');
if ( false === ($x_axis_coord = $this->get_x_axis_coord()) )
return false;
// Set the threshold proportional to the distance between two colums
$x_threshold = ($x_axis_coord['0,004']['x'] - $x_axis_coord['0,002']['x']) * .4;
$y_threshold = 3;
// OUTER LOOP: For each row of the source being parsed
foreach ($this->content as $y => $row) {
// Only if the row is aligned with the origin but it is not the origin
if ( isset($row[ $orig['x'] ]) && $row[ $orig['x'] ] != 'Antimicrobic' ) {
$antimicrobic = new Antimicrobic($row[ $orig['x'] ]);
$burst_start = $burst_end = false;
// INNER LOOP: For each tick of the x axis
foreach ($x_axis_coord as $tick => $tick_pos) {
$parsed_value = $this->get_text($y, $tick_pos['x'], $y_threshold, $x_threshold);
// Begin - sanity check
// If I read 2 burst of values there is an hole in the middle: cumulative data should not have holes!
if ( (false !== $parsed_value) && $burst_end ) {
Utils::log("Errore nel parsing, controllo di coerenza fallito. Antimicrobic: " . $antimicrobic->get_name());
die();
}
if ( (false !== $parsed_value) && (!$burst_start) && ($tick != '#Tested') )
$burst_start = true;
if ( (false === $parsed_value) && ($burst_start) )
$burst_end = true;
// End - sanity check
if ($tick == '#Tested')
$antimicrobic->set_number_tested($parsed_value);
else
$antimicrobic->set_value($tick, $parsed_value);
}
$germ->add_antimicrobic($antimicrobic);
}
}
return $germ;
}
// **** Private functions ****
function extract_tested_germ() {
if ( false === ($pos = $this->get_coordinates('Organismo:')) )
return false;
foreach ($this->content[ $pos['y'] ] as $x => $text)
if ($x != $pos['x'])
return $text;
return false;
}
function extract_data_range() {
if ( false === ($pos = $this->get_coordinates('Date Range')) )
return false;
foreach ($this->content[ $pos['y'] ] as $x => $text)
if ($x != $pos['x'])
return $text;
return false;
}
function get_x_axis_coord() {
// TODO mettere def intervallo fuori da qui e fuori da Data.php se abbiamo voglia
$out = array('#Tested' => $this->get_coordinates('#Tested'),
'0,002' => $this->get_coordinates('0.002'),
'0,004'=> $this->get_coordinates('0.004'),
'0,008'=> $this->get_coordinates('0.008'),
'0,015'=> $this->get_coordinates('0.015'),
'0,03'=> $this->get_coordinates('0.03'),
'0,06'=> $this->get_coordinates('0.06'),
'0,12'=> $this->get_coordinates('0.12'),
'0,25'=> $this->get_coordinates('0.25'),
'0,5'=> $this->get_coordinates('0.5'),
'1'=> $this->get_coordinates('1'),
'2'=> $this->get_coordinates('2'),
'4'=> $this->get_coordinates('4'),
'8'=> $this->get_coordinates('8'),
'16'=> $this->get_coordinates('16'),
'32'=> $this->get_coordinates('32'),
'64'=> $this->get_coordinates('64'),
'128'=> $this->get_coordinates('128'),
'256'=> $this->get_coordinates('256'),
'512'=> $this->get_coordinates('512'),
'1024'=> $this->get_coordinates('1024'),
'2048'=> $this->get_coordinates('2048'));
// NOMIC workaround (non dovrebbe succedere se l'estrazione segue pedissequamente la guida utente)
if ($out['2048'] === false)
$out['2048'] = $this->get_coordinates('2048 NOMIC');
foreach ($out as $coord)
if ($coord === false)
return false;
return $out;
}
function get_coordinates($pattern) {
foreach($this->content as $y => $row)
if(false !== ($x = array_search($pattern, $row)))
return array('x' => $x, 'y' => $y);
return false;
}
// Return text at specified coordinates. Return false if more than a text box is found.
public function get_text($y, $x, $y_threshold, $x_threshold) {
$rows = $this->content[$y];
while ($y_threshold > 0) {
if ( isset($this->content[ $y+$y_threshold ]) )
$rows += $this->content[ $y+$y_threshold ];
if ( isset($this->content[ $y-$y_threshold ]) )
$rows += $this->content[ $y-$y_threshold ];
$y_threshold--;
}
foreach ($rows as $c_x => $c_text)
if (abs($x-$c_x) <= $x_threshold) {
if (isset($out))
return false;
$out = $c_text;
}
if (isset($out))
return $out;
return false;
}
// Input: DIV.style string
// Output: position as array (top => y, left => x)
function get_position($style) {
// Get top:$y
$from = strrpos($style, 'top:');
if ($from === false)
return false;
$from+=4;
$to = strpos($style, ';', $from);
$y = substr($style, $from, $to-$from);
// Get left:$x
$from = strpos($style, 'left:', $to);
if ($from === false)
return false;
$from+=5;
$to = strpos($style, ';', $from);
if ($to === false)
$to = strlen($style);
$x = substr($style, $from, $to-$from);
return array('x' => $x, 'y' => $y);
}
}
?>