Repository navigation
Expand file tree
/
Copy pathexample_usage.py
More file actions
412 lines (319 loc) · 13 KB
/
Copy pathexample_usage.py
File metadata and controls
412 lines (319 loc) · 13 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
"""
Example usage scripts for the Universal Document Parser.
Demonstrates various use cases and integration patterns.
"""
import os
import sys
import json
import time
from pathlib import Path
from typing import List, Dict, Any
# Add the parser to the path
sys.path.append(str(Path(__file__).parent))
from main import UniversalDocumentParser
from utils.config import ParserConfig, OCRConfig, PDFConfig, PerformanceConfig
from models.parse_result import ParseResult
def example_basic_usage():
"""Basic usage example - parse files in current directory."""
print("=== Basic Usage Example ===")
# Initialize with default configuration
parser = UniversalDocumentParser()
# Find all files in current directory
current_dir = Path(".")
supported_extensions = ['.pdf', '.docx', '.txt', '.jpg', '.png']
files_to_parse = []
for ext in supported_extensions:
files_to_parse.extend(current_dir.glob(f"*{ext}"))
if not files_to_parse:
print("No supported files found in current directory")
return
print(f"Found {len(files_to_parse)} files to parse")
# Parse each file
for file_path in files_to_parse:
print(f"\nParsing: {file_path.name}")
result = parser.parse_file(file_path)
if result.success:
print(f" ✓ Success ({result.parser_used})")
print(f" ✓ Content length: {len(result.content)} characters")
print(f" ✓ Tables: {len(result.tables)}")
print(f" ✓ Images: {len(result.images)}")
print(f" ✓ Processing time: {result.parsing_time:.2f}s")
else:
print(f" ✗ Failed: {result.error}")
def example_advanced_configuration():
"""Advanced configuration example with custom settings."""
print("\n=== Advanced Configuration Example ===")
# Create custom configuration
config = ParserConfig(
enable_ocr=True,
parallel_processing=True,
max_workers=4,
max_file_size=50 * 1024 * 1024, # 50MB limit
# OCR settings
ocr=OCRConfig(
enabled=True,
languages=["en", "ar"],
engine="easyocr",
confidence_threshold=0.7,
preprocessing=True
),
# PDF settings
pdf=PDFConfig(
prefer_pdfplumber=True,
extract_tables=True,
extract_images=True
),
# Performance settings
performance=PerformanceConfig(
parallel_processing=True,
max_workers=4,
memory_limit_mb=512,
timeout_seconds=300
)
)
# Initialize parser with custom config
parser = UniversalDocumentParser(config)
# Show configuration
print("Configuration:")
print(f" OCR enabled: {config.enable_ocr}")
print(f" OCR languages: {config.ocr.languages}")
print(f" Max workers: {config.max_workers}")
print(f" Max file size: {config.max_file_size // (1024*1024)}MB")
# Get supported formats
supported_formats = parser.get_supported_formats()
print(f"\nSupported formats: {supported_formats}")
def example_batch_processing():
"""Batch processing example with progress tracking."""
print("\n=== Batch Processing Example ===")
# Create a test directory structure if it doesn't exist
test_dir = Path("test_documents")
if not test_dir.exists():
print(f"Directory {test_dir} not found. Create it and add some documents to test.")
return
# Initialize parser
config = ParserConfig(parallel_processing=True, max_workers=4)
parser = UniversalDocumentParser(config)
# Get all files recursively
all_files = []
for pattern in ["*.pdf", "*.docx", "*.txt", "*.jpg", "*.png", "*.csv"]:
all_files.extend(test_dir.rglob(pattern))
if not all_files:
print(f"No files found in {test_dir}")
return
print(f"Processing {len(all_files)} files...")
# Process with progress tracking
start_time = time.time()
results = []
for i, file_path in enumerate(all_files, 1):
print(f"Processing [{i}/{len(all_files)}]: {file_path.name}")
result = parser.parse_file(file_path)
results.append(result)
# Show progress
if result.success:
print(f" ✓ {result.parser_used} - {len(result.content)} chars")
else:
print(f" ✗ {result.error}")
# Generate report
processing_time = time.time() - start_time
report = parser.generate_report(results)
print(f"\n=== Batch Processing Report ===")
print(f"Total time: {processing_time:.2f}s")
print(f"Files processed: {report['summary']['total_files']}")
print(f"Success rate: {report['summary']['success_rate']:.1f}%")
print(f"Average time per file: {report['summary']['average_time_per_file']:.2f}s")
# Show results by file type
print("\nResults by file type:")
for file_type, stats in report['by_file_type'].items():
print(f" {file_type}: {stats['count']} files, {stats['success_rate']:.1f}% success")
def example_ocr_focused():
"""OCR-focused example for image documents."""
print("\n=== OCR Processing Example ===")
# OCR configuration
ocr_config = OCRConfig(
enabled=True,
languages=["en", "ar"], # English and Arabic
engine="easyocr", # or "tesseract"
confidence_threshold=0.6,
preprocessing=True
)
config = ParserConfig(
enable_ocr=True,
ocr=ocr_config
)
parser = UniversalDocumentParser(config)
# Test OCR capabilities
print("OCR Configuration:")
ocr_info = parser.parsers['image'].get_ocr_info()
print(f" Engines available: {ocr_info['available_engines']}")
print(f" Languages: {ocr_info['supported_languages']}")
print(f" Confidence threshold: {ocr_info['confidence_threshold']}")
# Find image files
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff']
image_files = []
for ext in image_extensions:
image_files.extend(Path(".").glob(f"*{ext}"))
if not image_files:
print("No image files found for OCR testing")
return
print(f"\nProcessing {len(image_files)} image files with OCR:")
for image_file in image_files:
print(f"\nProcessing: {image_file.name}")
result = parser.parse_file(image_file)
if result.success and result.content:
print(f" ✓ Extracted text ({len(result.content)} chars):")
print(f" {result.content[:200]}...")
if len(result.content) > 200:
print(" [truncated]")
elif result.success:
print(" ✓ Processed but no text found")
else:
print(f" ✗ Failed: {result.error}")
def example_data_extraction():
"""Example focusing on structured data extraction."""
print("\n=== Structured Data Extraction Example ===")
# Configuration optimized for data extraction
config = ParserConfig(
pdf=PDFConfig(
prefer_pdfplumber=True, # Better for tables
extract_tables=True,
extract_images=False # Skip images for faster processing
)
)
parser = UniversalDocumentParser(config)
# Look for files with likely structured data
data_files = []
for pattern in ["*.pdf", "*.xlsx", "*.csv", "*.docx"]:
data_files.extend(Path(".").glob(pattern))
if not data_files:
print("No data files found")
return
print(f"Analyzing {len(data_files)} files for structured data:")
total_tables = 0
total_images = 0
for file_path in data_files:
print(f"\nAnalyzing: {file_path.name}")
result = parser.parse_file(file_path)
if result.success:
print(f" ✓ Parser: {result.parser_used}")
print(f" ✓ Tables found: {len(result.tables)}")
print(f" ✓ Images found: {len(result.images)}")
# Show table details
for i, table in enumerate(result.tables):
print(f" Table {i+1}: {len(table.headers)} columns × {len(table.rows)} rows")
if table.headers:
print(f" Headers: {', '.join(table.headers[:5])}{'...' if len(table.headers) > 5 else ''}")
total_tables += len(result.tables)
total_images += len(result.images)
else:
print(f" ✗ Failed: {result.error}")
print(f"\nSummary:")
print(f" Total tables extracted: {total_tables}")
print(f" Total images found: {total_images}")
def example_export_results():
"""Example showing how to export results in different formats."""
print("\n=== Export Results Example ===")
parser = UniversalDocumentParser()
# Parse some files
files = list(Path(".").glob("*.pdf")) + list(Path(".").glob("*.docx"))
if not files:
print("No files to process for export example")
return
results = []
for file_path in files[:3]: # Limit to first 3 files
result = parser.parse_file(file_path)
results.append(result)
# Export to JSON
json_output = {
"parsing_session": {
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"files_processed": len(results),
"successful": sum(1 for r in results if r.success)
},
"results": [result.to_dict() for result in results]
}
with open("parsing_results.json", "w", encoding="utf-8") as f:
json.dump(json_output, f, indent=2, ensure_ascii=False)
print("Results exported to parsing_results.json")
# Export tables to CSV
table_count = 0
for i, result in enumerate(results):
if result.success and result.tables:
for j, table in enumerate(result.tables):
csv_filename = f"table_{i}_{j}.csv"
with open(csv_filename, "w", newline="", encoding="utf-8") as csvfile:
import csv
writer = csv.writer(csvfile)
# Write headers
if table.headers:
writer.writerow(table.headers)
# Write data rows
writer.writerows(table.rows)
table_count += 1
print(f"Table exported to {csv_filename}")
print(f"Exported {table_count} tables to CSV files")
def example_error_handling():
"""Example demonstrating error handling and recovery."""
print("\n=== Error Handling Example ===")
parser = UniversalDocumentParser()
# Test with various problematic scenarios
test_cases = [
"nonexistent_file.pdf",
Path("."), # Directory instead of file
__file__, # Python file (should work with text parser)
]
for test_case in test_cases:
print(f"\nTesting: {test_case}")
try:
result = parser.parse_file(test_case)
if result.success:
print(f" ✓ Success: {result.parser_used}")
print(f" ✓ Content length: {len(result.content)}")
else:
print(f" ✗ Failed: {result.error}")
print(f" ✗ Parser attempted: {result.parser_used or 'None'}")
except Exception as e:
print(f" ✗ Exception: {str(e)}")
# Test batch processing with mixed success/failure
mixed_files = ["nonexistent1.pdf", __file__, "nonexistent2.docx"]
print(f"\nBatch processing with mixed results:")
results = []
for file_path in mixed_files:
try:
result = parser.parse_file(file_path)
results.append(result)
except Exception as e:
print(f"Exception processing {file_path}: {e}")
# Generate report including failures
if results:
report = parser.generate_report(results)
print(f"\nMixed batch report:")
print(f" Success rate: {report['summary']['success_rate']:.1f}%")
print(f" Failed files: {len(report['failed_files'])}")
for failed in report['failed_files']:
print(f" {failed['file']}: {failed['error']}")
def main():
"""Run all examples."""
print("Universal Document Parser - Example Usage")
print("=" * 50)
examples = [
example_basic_usage,
example_advanced_configuration,
example_batch_processing,
example_ocr_focused,
example_data_extraction,
example_export_results,
example_error_handling
]
for example_func in examples:
try:
example_func()
except KeyboardInterrupt:
print("\nStopped by user")
break
except Exception as e:
print(f"\nExample failed: {str(e)}")
print("\n" + "-" * 50)
print("\nAll examples completed!")
if __name__ == "__main__":
main()