-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_processor.py
More file actions
147 lines (117 loc) · 5.11 KB
/
pdf_processor.py
File metadata and controls
147 lines (117 loc) · 5.11 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
import os
import requests
try:
import fitz
FITZ_AVAILABLE = True
PDF_LIB = 'fitz'
except ImportError:
try:
import pdfplumber
FITZ_AVAILABLE = False
PDFPLUMBER_AVAILABLE = True
PDF_LIB = 'pdfplumber'
except ImportError:
FITZ_AVAILABLE = False
PDFPLUMBER_AVAILABLE = False
PDF_LIB = None
print("Warning: No PDF library available. PDF processing will be limited.")
from urllib.parse import urlparse
from pathlib import Path
import hashlib
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from sumy.nlp.stemmers import Stemmer
import re
class PDFProcessor:
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
})
self.stemmer = Stemmer("german")
self.summarizer = LsaSummarizer(self.stemmer)
self.summarizer.stop_words = self._get_german_stopwords()
def download_pdf(self, pdf_url, download_folder):
if not pdf_url:
return None
try:
response = self.session.get(pdf_url, stream=True)
response.raise_for_status()
url_hash = hashlib.md5(pdf_url.encode()).hexdigest()[:8]
filename = f"document_{url_hash}.pdf"
filepath = os.path.join(download_folder, filename)
with open(filepath, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return filepath
except Exception as e:
print(f"Fehler beim Download der PDF {pdf_url}: {e}")
return None
def extract_text(self, pdf_path):
if not pdf_path or not os.path.exists(pdf_path):
return ""
if PDF_LIB == 'fitz':
return self._extract_text_fitz(pdf_path)
elif PDF_LIB == 'pdfplumber':
return self._extract_text_pdfplumber(pdf_path)
else:
print("No PDF library available for text extraction")
return ""
def _extract_text_fitz(self, pdf_path):
try:
doc = fitz.open(pdf_path)
text = ""
for page_num in range(doc.page_count):
page = doc[page_num]
text += page.get_text()
doc.close()
text = self._clean_text(text)
return text
except Exception as e:
print(f"Fehler beim Extrahieren des Textes aus {pdf_path}: {e}")
return ""
def _extract_text_pdfplumber(self, pdf_path):
try:
import pdfplumber
text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
text = self._clean_text(text)
return text
except Exception as e:
print(f"Fehler beim Extrahieren des Textes aus {pdf_path}: {e}")
return ""
def _clean_text(self, text):
text = re.sub(r'\s+', ' ', text)
text = re.sub(r'[^\w\säöüÄÖÜß.,!?;:()\-]', '', text)
text = text.strip()
return text
def summarize_text(self, text, sentence_count=3):
if not text or len(text.strip()) < 100:
return "Zu wenig Text für eine Zusammenfassung verfügbar."
try:
parser = PlaintextParser.from_string(text, Tokenizer("german"))
summary = self.summarizer(parser.document, sentence_count)
summary_text = " ".join([str(sentence) for sentence in summary])
if not summary_text.strip():
return "Zusammenfassung konnte nicht erstellt werden."
return summary_text
except Exception as e:
print(f"Fehler bei der Zusammenfassung: {e}")
return f"Fehler bei der Zusammenfassung: {str(e)}"
def _get_german_stopwords(self):
return {
'aber', 'alle', 'allem', 'allen', 'aller', 'alles', 'als', 'also', 'am', 'an',
'ander', 'andere', 'anderem', 'anderen', 'anderer', 'anderes', 'anderm', 'andern',
'anders', 'auch', 'auf', 'aus', 'bei', 'bin', 'bis', 'bist', 'da', 'damit',
'dann', 'der', 'den', 'des', 'dem', 'die', 'das', 'dass', 'daß', 'du', 'er',
'eine', 'ein', 'einem', 'einen', 'einer', 'eines', 'für', 'hatte', 'habe',
'haben', 'hat', 'hier', 'ich', 'ihm', 'ihn', 'ihnen', 'ihr', 'ihre', 'im',
'in', 'ist', 'kann', 'mich', 'mir', 'mit', 'nach', 'nicht', 'noch', 'nur',
'oder', 'sie', 'sind', 'so', 'über', 'um', 'und', 'uns', 'unse', 'unser',
'unses', 'von', 'vor', 'war', 'waren', 'wir', 'wird', 'zu', 'zum', 'zur'
}