-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractors.cpp
More file actions
390 lines (308 loc) · 13.1 KB
/
Copy pathextractors.cpp
File metadata and controls
390 lines (308 loc) · 13.1 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "engine.h"
#include <iostream>
#include <sstream>
#include <regex>
#include <fstream>
#include <cstdio>
#include <memory>
#include <vector>
#include <cctype>
#include <thread>
#include <algorithm>
#include <poppler/cpp/poppler-document.h>
#include <poppler/cpp/poppler-page.h>
#include <poppler/GlobalParams.h>
#include <poppler/PDFDoc.h>
#include <poppler/Outline.h>
#include <poppler/Link.h>
#include <poppler/goo/GooString.h>
#include <poppler/Catalog.h>
void flatten_outline( const std::vector<OutlineItem*>* items, std::vector<OutlineItem*>& flat_list ) {
if ( !items ) return;
for ( OutlineItem *item : *items ) {
flat_list.push_back( item );
if ( item->hasKids() ) {
item->open();
if ( item->getKids() ) {
flatten_outline( item->getKids(), flat_list );
}
}
}
}
bool get_metadata_chapter_bounds( const char* file_path, int target_chapter, int* start_page, int* end_page ) {
if ( !globalParams ) globalParams = std::make_unique<GlobalParams>();
auto goo_file = std::make_unique<GooString>( file_path );
auto doc = std::make_unique<PDFDoc>( std::move( goo_file ) );
if ( !doc->isOk() || !doc->getOutline() ) return false;
const auto *items = doc->getOutline()->getItems();
if ( !items ) return false;
std::vector<OutlineItem*> all_items;
flatten_outline( items, all_items );
int current_chapter = 0;
int found_start = -1;
int found_end = doc->getNumPages();
std::regex chapter_regex( R"(^\s*(chapter\s+[0-9]+|chapter\s+[ivxlcdm]+|[ivxlcdm]+)\s*$)", std::regex_constants::icase );
for ( OutlineItem *item : all_items ) {
const std::vector<Unicode>& title_uni = item->getTitle();
std::string title_str;
for ( Unicode u : title_uni ) title_str += (char)( u & 0xFF );
if ( std::regex_match( title_str, chapter_regex ) ) {
current_chapter++;
int page_num = -1;
const LinkAction *action = item->getAction();
if ( action && action->getKind() == actionGoTo ) {
const LinkGoTo *goto_action = static_cast<const LinkGoTo*>( action );
const LinkDest *dest = goto_action->getDest();
std::unique_ptr<LinkDest> resolved_dest;
if ( !dest ) {
const GooString *named = goto_action->getNamedDest();
if ( named ) {
resolved_dest = doc->getCatalog()->findDest( named );
dest = resolved_dest.get();
}
}
if ( dest ) page_num = dest->isPageRef() ? doc->findPage( dest->getPageRef() ) : dest->getPageNum();
}
if ( current_chapter == target_chapter && page_num != -1 ) {
found_start = page_num;
} else if ( current_chapter == target_chapter + 1 && page_num != -1 ) {
found_end = page_num - 1;
break;
}
}
}
if ( found_start != -1 ) {
*start_page = found_start;
*end_page = found_end;
return true;
}
return false;
}
std::vector<std::string> extract_epub_html_files( const char* file_path ) {
std::vector<std::string> chapters;
std::string cmd_list = std::string( "unzip -Z1 \"" ) + file_path + "\" \"*.html\" \"*.xhtml\" \"*.htm\" 2>/dev/null";
FILE* pipe_list = popen( cmd_list.c_str(), "r" );
if ( !pipe_list ) return chapters;
char buf[ 256 ];
std::vector<std::string> filenames;
while ( fgets( buf, sizeof( buf ), pipe_list ) ) {
std::string name = buf;
name.erase( name.find_last_not_of( " \n\r\t" ) + 1 );
filenames.push_back( name );
}
pclose( pipe_list );
for ( const auto& fname : filenames ) {
std::string cmd_extract = std::string( "unzip -p \"" ) + file_path + "\" \"" + fname + "\" 2>/dev/null";
FILE* pipe_ext = popen( cmd_extract.c_str(), "r" );
if ( !pipe_ext ) continue;
char buf2[ 2048 ];
std::string html;
while ( fgets( buf2, sizeof( buf2 ), pipe_ext ) != nullptr ) html += buf2;
pclose( pipe_ext );
chapters.push_back( html );
}
return chapters;
}
extern "C" {
bool load_pdf_session( const char* file_path, int start_page, int end_page ) {
cleanup_session();
poppler::document* doc = poppler::document::load_from_file( file_path );
if ( !doc ) return false;
int total_pages = doc->pages();
if ( start_page < 1 ) start_page = 1;
if ( end_page > total_pages || end_page < start_page ) end_page = total_pages;
for ( int i = start_page - 1; i < end_page; i++ ) {
poppler::page* p = doc->create_page( i );
if ( p ) {
poppler::ustring ustr = p->text();
std::vector<char> utf8_bytes = ustr.to_utf8();
std::string text( utf8_bytes.begin(), utf8_bytes.end() );
std::istringstream iss( text );
std::string word;
while ( iss >> word ) push_processed_word( word );
delete p;
}
}
delete doc;
return true;
}
bool load_pdf_chapter( const char* file_path, int target_chapter ) {
int start_page = -1;
int end_page = -1;
if ( get_metadata_chapter_bounds( file_path, target_chapter, &start_page, &end_page ) ) {
return load_pdf_session( file_path, start_page, end_page );
}
return false;
}
bool load_txt_session( const char* file_path ) {
cleanup_session();
std::ifstream file( file_path );
if ( !file.is_open() ) return false;
std::string word;
while ( file >> word ) push_processed_word( word );
return session_words.size() > 0;
}
bool load_docx_session( const char* file_path ) {
cleanup_session();
std::string cmd = std::string( "unzip -p \"" ) + file_path + "\" word/document.xml 2>/dev/null";
FILE* pipe = popen( cmd.c_str(), "r" );
if ( !pipe ) return false;
char buffer[ 1024 ];
std::string xml_data;
while ( fgets( buffer, sizeof( buffer ), pipe ) != nullptr ) xml_data += buffer;
pclose( pipe );
if ( xml_data.empty() ) return false;
std::regex xml_tags( "<[^>]+>" );
std::string plain_text = std::regex_replace( xml_data, xml_tags, " " );
std::regex html_entities( "&[a-zA-Z0-9#]{1,10};" );
plain_text = std::regex_replace( plain_text, html_entities, " " );
std::istringstream iss( plain_text );
std::string word;
while ( iss >> word ) push_processed_word( word );
return session_words.size() > 0;
}
bool load_epub_session( const char* file_path ) {
cleanup_session();
std::string cmd = std::string( "unzip -p \"" ) + file_path + "\" \"*.html\" \"*.xhtml\" \"*.htm\" 2>/dev/null";
FILE* pipe = popen( cmd.c_str(), "r" );
if ( !pipe ) return false;
char buffer[ 2048 ];
std::string html_data;
while ( fgets( buffer, sizeof( buffer ), pipe ) != nullptr ) html_data += buffer;
pclose( pipe );
if ( html_data.empty() ) return false;
std::regex html_tags( "<[^>]+>" );
std::string plain_text = std::regex_replace( html_data, html_tags, " " );
std::regex html_entities( "&[a-zA-Z0-9#]{1,10};" );
plain_text = std::regex_replace( plain_text, html_entities, " " );
std::istringstream iss( plain_text );
std::string word;
while ( iss >> word ) push_processed_word( word );
return session_words.size() > 0;
}
int get_pdf_chapter_count( const char* file_path ) {
if ( !globalParams ) globalParams = std::make_unique<GlobalParams>();
auto goo_file = std::make_unique<GooString>( file_path );
auto doc = std::make_unique<PDFDoc>( std::move( goo_file ) );
if ( !doc->isOk() || !doc->getOutline() ) return 0;
const auto *items = doc->getOutline()->getItems();
if ( !items ) return 0;
std::vector<OutlineItem*> all_items;
flatten_outline( items, all_items );
int count = 0;
std::regex chapter_regex( R"(^\s*(chapter\s+[0-9]+|chapter\s+[ivxlcdm]+|[ivxlcdm]+)\s*$)", std::regex_constants::icase );
for ( OutlineItem *item : all_items ) {
const std::vector<Unicode>& title_uni = item->getTitle();
std::string title_str;
for ( Unicode u : title_uni ) title_str += (char)( u & 0xFF );
if ( std::regex_match( title_str, chapter_regex ) ) count++;
}
return count;
}
bool compile_txt_concurrent( const char* file_path, const char* out_path ) {
std::ifstream file( file_path, std::ios::in | std::ios::binary );
if ( !file ) return false;
std::string raw_text( ( std::istreambuf_iterator<char>( file ) ), std::istreambuf_iterator<char>() );
file.close();
size_t num_threads = std::thread::hardware_concurrency();
if ( num_threads == 0 ) num_threads = 4;
std::vector< std::string > chunks;
size_t chunk_size = raw_text.length() / num_threads;
size_t start = 0;
for ( size_t i = 0; i < num_threads; ++i ) {
if ( start >= raw_text.length() ) break;
size_t end = start + chunk_size;
if ( end < raw_text.length() ) {
while ( end < raw_text.length() && !std::isspace( raw_text[ end ] ) ) {
end++;
}
} else {
end = raw_text.length();
}
chunks.push_back( raw_text.substr( start, end - start ) );
start = end;
}
std::vector< std::string > processed_chunks( chunks.size() );
{
ThreadPool pool( chunks.size() );
for ( size_t i = 0; i < chunks.size(); ++i ) {
std::string chunk_data = chunks[ i ];
pool.enqueue( [ i, chunk_data, &processed_chunks ] () mutable {
std::regex html_entities( "&[a-zA-Z0-9#]{1,10};" );
chunk_data = std::regex_replace( chunk_data, html_entities, " " );
std::replace( chunk_data.begin(), chunk_data.end(), '-', ' ' );
std::stringstream ss( chunk_data );
std::string final_chunk;
std::string temp_word;
while ( ss >> temp_word ) {
final_chunk += temp_word;
final_chunk += '\0';
}
processed_chunks[ i ] = std::move( final_chunk );
} );
}
}
std::ofstream out_file( out_path, std::ios::out | std::ios::binary );
if ( !out_file ) return false;
uint32_t total_words = 0;
for ( const std::string& clean_chunk : processed_chunks ) {
total_words += std::count( clean_chunk.begin(), clean_chunk.end(), '\0' );
}
SRHeader header = { 0 };
header.magic[0] = 'S'; header.magic[1] = 'R'; header.magic[2] = '0'; header.magic[3] = '1';
header.saved_index = 0;
header.total_words = total_words;
header.chapter_count = 0;
out_file.write( reinterpret_cast<char*>( &header ), sizeof( SRHeader ) );
for ( const std::string& clean_chunk : processed_chunks ) {
out_file.write( clean_chunk.c_str(), clean_chunk.length() );
}
out_file.close();
return true;
}
bool compile_epub_concurrent( const char* file_path, const char* out_path ) {
std::vector<std::string> raw_chapters = extract_epub_html_files( file_path );
if ( raw_chapters.empty() ) return false;
std::vector<std::string> processed_chapters( raw_chapters.size() );
{
size_t num_threads = std::thread::hardware_concurrency();
if ( num_threads == 0 ) num_threads = 4;
ThreadPool pool( num_threads );
for ( size_t i = 0; i < raw_chapters.size(); ++i ) {
std::string chapter_html = raw_chapters[ i ];
pool.enqueue( [ i, chapter_html, &processed_chapters ] () mutable {
std::regex html_tags( "<[^>]+>" );
chapter_html = std::regex_replace( chapter_html, html_tags, " " );
std::regex html_entities( "&[a-zA-Z0-9#]{1,10};" );
chapter_html = std::regex_replace( chapter_html, html_entities, " " );
std::replace( chapter_html.begin(), chapter_html.end(), '-', ' ' );
std::stringstream ss( chapter_html );
std::string final_chunk;
std::string temp_word;
while ( ss >> temp_word ) {
final_chunk += temp_word;
final_chunk += '\0';
}
processed_chapters[ i ] = std::move( final_chunk );
} );
}
}
std::ofstream out_file( out_path, std::ios::out | std::ios::binary );
if ( !out_file ) return false;
uint32_t total_words = 0;
for ( const std::string& clean_chapter : processed_chapters ) {
total_words += std::count( clean_chapter.begin(), clean_chapter.end(), '\0' );
}
SRHeader header = { 0 };
header.magic[0] = 'S'; header.magic[1] = 'R'; header.magic[2] = '0'; header.magic[3] = '1';
header.saved_index = 0;
header.total_words = total_words;
header.chapter_count = 0;
out_file.write( reinterpret_cast<char*>( &header ), sizeof( SRHeader ) );
for ( const std::string& clean_chapter : processed_chapters ) {
out_file.write( clean_chapter.c_str(), clean_chapter.length() );
}
out_file.close();
return true;
}
} // extern "C"