-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdf-parser.php
126 lines (111 loc) · 4.18 KB
/
pdf-parser.php
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
<?php
/**
* Parse text lines from PDFs of Nedělení žaltář and Všední žaltář based on XML output of pdfminer.six
*
* The XML was generated using the following command:
*
* $ pdf2txt.py --output_type xml Zaltar.pdf > zaltar.xml
*/
use Symfony\Component\Yaml\Yaml;
require_once 'vendor/autoload.php';
define('PSLM_LINE_MARGIN', 5);
define('PSLM_ASTERISK_LINE_MARGIN', 10);
define('PSLM_WORD_MARGIN', 2);
// Really want to override?
$psalm_lines = pslm_pdf_to_psalm_lines('db/Nedelni_zaltar_final_2021.xml.gz', 'r');
pslm_save_psalm_lines($psalm_lines, 'db/Nedelni_zaltar_final_2021.yml');
$psalm_lines = pslm_pdf_to_psalm_lines('db/Vsedni_zaltar_final_2021.xml.gz', 'r');
pslm_save_psalm_lines($psalm_lines, 'db/Vsedni_zaltar_final_2021.yml');
function pslm_save_psalm_lines($psalm_lines, $filename) {
file_put_contents($filename, Yaml::dump($psalm_lines));
}
function pslm_get_line_key($text_lines, $top, $margin = PSLM_LINE_MARGIN) {
for ($d = 0; $d <= $margin; ++$d) {
$key_1 = $top + $d;
$key_2 = $top - $d;
if (isset($text_lines[$key_1])) {
return $key_1;
} elseif (isset($text_lines[$key_2])) {
return $key_2;
}
}
return false;
}
function pslm_text_lines_to_psalm_lines($text_lines) {
$psalm_lines = [];
// reassign asterisks to lines with extended line margin
foreach ($text_lines as $top => $texts) {
foreach ($texts as $left => $text) {
if ($text[1] === '*') {
unset($text_lines[$top][$left]);
if (empty($text_lines[$top])) {
unset($text_lines[$top]);
}
$key = pslm_get_line_key($text_lines, $top, PSLM_ASTERISK_LINE_MARGIN);
if ($key === false) {
$key = $top;
$text_lines[$key] = [];
}
$text_lines[$key][$left] = $text;
}
}
}
krsort($text_lines);
foreach ($text_lines as $top => $texts) {
$psalm_line = '';
ksort($texts);
$left = array_keys($texts);
$text = array_values($texts);
// build up an array of right coordinates to compare with left coordinate of next text
$right = [$text[0][0]];
$psalm_line .= $text[0][1];
for ($i = 1; $i < count($texts); ++$i) {
$right[$i] = $text[$i][0];
if ($left[$i] - $right[$i-1] > PSLM_WORD_MARGIN) {
$psalm_line .= " ";
}
$psalm_line .= $text[$i][1];
}
$psalm_line = preg_replace('#[^ěščřžýáíéóúůďťňĎŇŤŠČŘŽÝÁÍÉÚŮĚÓa-zA-Z0-9,\.\-\?\!„“":;\(\)\[\]\*\+/ ]+#u', '', $psalm_line);
$psalm_line = preg_replace('#\s+#u', ' ', $psalm_line);
$psalm_line = trim($psalm_line);
$psalm_line = preg_replace('#^[ AQRSE]*(\d+)[ AQRSE.]* #u', '\1. ', $psalm_line);
$is_garbage = preg_match('#^[ AQRSE\.,\(\)]*$#u', $psalm_line);
if (!$is_garbage) {
$psalm_lines[] = $psalm_line;
}
}
return $psalm_lines;
}
function pslm_pdf_to_psalm_lines($filename) {
$fp = gzopen($filename, 'r');
$page = 0;
$text_lines = [];
$psalm_lines = [];
while (($xml_line = fgets($fp, 4096)) !== false) {
preg_match('#<page id="(\d+)"#', $xml_line, $page_m);
if ($page_m) {
if (!empty($text_lines)) {
$psalm_lines[$page] = pslm_text_lines_to_psalm_lines($text_lines);
}
$page = $page_m[1];
$text_lines = [];
}
preg_match('#<text [^>]*bbox="([\d\.]+),([\d\.]+),([\d\.]+),([\d\.]+)"[^>]*>([^<]+)</text>#', $xml_line, $text_m);
if ($text_m) {
$left = intval($text_m[1]);
$top = intval($text_m[2]);
$right = intval($text_m[3]);
$text = $text_m[5];
$key = pslm_get_line_key($text_lines, $top);
if ($key === false) {
$key = $top;
$text_lines[$key] = [];
}
$text_lines[$key][$left] = [$right, $text];
}
}
$psalm_lines[$page] = pslm_text_lines_to_psalm_lines($text_lines);
fclose($fp);
return $psalm_lines;
}