-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc.py
More file actions
executable file
·252 lines (185 loc) · 7.95 KB
/
doc.py
File metadata and controls
executable file
·252 lines (185 loc) · 7.95 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
"""
Yfirlestur: Online spelling and grammar correction for Icelandic
Copyright (C) 2020-2025 Miðeind ehf.
This software is licensed under the MIT License:
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This module contains code to extract text from documents
such as plain text, html, rtf, pdf, odt, and docx files.
"""
from typing import Callable, List, Union, Dict, Type, Mapping, Any, cast
import re
import abc
from io import BytesIO, StringIO
from zipfile import ZipFile
from html2text import HTML2Text
from striprtf.striprtf import rtf_to_text
from odf import teletype
from odf import text as odf_text
from odf.opendocument import load as load_odf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument as PDFMinerDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from defusedxml import ElementTree # Use defusedxml module to prevent parsing of malicious XML
DEFAULT_TEXT_ENCODING = "UTF-8"
DocumentType = Type["Document"]
class MalformedDocumentError(Exception):
pass
class Document(abc.ABC):
"""Abstract base class for documents."""
def __init__(self, path_or_bytes: Union[str, bytes]) -> None:
"""Accepts either a file path or bytes object"""
if isinstance(path_or_bytes, str):
# It's a file path
with open(path_or_bytes, "rb") as file:
self.data: bytes = file.read()
else:
# It's a byte stream
self.data: bytes = path_or_bytes
@staticmethod
def for_mimetype(mime_type: str) -> DocumentType:
return doc_class_for_mime_type(mime_type)
@staticmethod
def for_suffix(suffix: str) -> DocumentType:
return doc_class_for_suffix(suffix)
@abc.abstractmethod
def extract_text(self) -> str:
"""All subclasses must implement this method."""
raise NotImplementedError
def write_to_file(self, path: str) -> None:
with open(path, "wb") as f:
f.write(self.data)
class PlainTextDocument(Document):
"""Plain text document."""
def extract_text(self) -> str:
return self.data.decode(DEFAULT_TEXT_ENCODING)
class HTMLDocument(Document):
"""HTML document."""
@staticmethod
def _remove_header_prefixes(text: str) -> str:
"""Removes '#' in all lines starting with '#'. Annoyingly,
html2text adds markdown-style headers for <h*> tags."""
lines = text.split("\n")
for i, line in enumerate(lines):
if line.startswith("#"):
lines[i] = re.sub(r"[#]+\s", "", line)
return "\n".join(lines)
def extract_text(self) -> str:
html = self.data.decode(DEFAULT_TEXT_ENCODING)
h = HTML2Text()
# See https://github.com/Alir3z4/html2text/blob/master/html2text/cli.py
h.ignore_links = True
h.ignore_emphasis = True
h.ignore_images = True
h.unicode_snob = True
h.ignore_tables = True
h.decode_errors = "ignore" # type: ignore
h.body_width = 0
txt = h.handle(html)
return self._remove_header_prefixes(txt)
class RTFDocument(Document):
"""Rich text document."""
def extract_text(self) -> str:
txt = self.data.decode(DEFAULT_TEXT_ENCODING)
# Hack to handle Apple's extensions to the RTF format
txt = txt.replace("\\\n\\\n", "\\\n\\par\n")
return cast(Callable[[str], str], rtf_to_text)(txt)
class PDFDocument(Document):
"""Adobe PDF document."""
def extract_text(self) -> str:
output_string = StringIO()
parser = PDFParser(BytesIO(self.data))
doc = PDFMinerDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc): # type: ignore
interpreter.process_page(page) # type: ignore
# Postprocessing
txt = output_string.getvalue()
txt = txt.replace("\n", " ")
return txt
class DocxDocument(Document):
"""Microsoft docx document."""
DOCXML_PATH = "word/document.xml"
WORD_NAMESPACE = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
PARAGRAPH_TAG = WORD_NAMESPACE + "p"
TEXT_TAG = WORD_NAMESPACE + "t"
BREAK_TAG = WORD_NAMESPACE + "br"
def extract_text(self) -> str:
zipfile = ZipFile(BytesIO(self.data), "r")
# Verify that archive contains document.xml
if self.DOCXML_PATH not in zipfile.namelist():
raise MalformedDocumentError("Malformed docx file")
# Read xml file from archive
content = zipfile.read(self.DOCXML_PATH)
zipfile.close()
# Parse it
tree: Any = ElementTree.fromstring(content) # type: ignore
# Extract text elements from all paragraphs
# (with special handling of line breaks)
paragraphs: List[str] = []
p: Any
for p in tree.iter(self.PARAGRAPH_TAG):
texts: List[str] = []
for node in p.iter():
if node.tag.endswith(self.TEXT_TAG) and node.text:
texts.append(node.text)
elif node.tag.endswith(self.BREAK_TAG):
texts.append("\n")
if texts:
paragraphs.append("".join(texts))
return "\n\n".join(paragraphs)
class ODTDocument(Document):
"""OpenDocument format."""
def extract_text(self) -> str:
textdoc = load_odf(BytesIO(self.data))
# Find all paragraphs
paragraphs = textdoc.getElementsByType(odf_text.P) # type: ignore
ptexts = [teletype.extractText(p) for p in paragraphs] # type: ignore
return "\n\n".join(ptexts)
# Map file mime type to document class
MIMETYPE_TO_DOC_CLASS: Dict[str, DocumentType] = {
"text/plain": PlainTextDocument,
"text/html": HTMLDocument,
"text/rtf": RTFDocument,
"application/pdf": PDFDocument,
"application/x-pdf": PDFDocument,
"application/rtf": RTFDocument,
"application/vnd.oasis.opendocument.text": ODTDocument,
# Yes, really! Mime type naming by committee...
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": DocxDocument,
}
SUPPORTED_DOC_MIMETYPES = frozenset(MIMETYPE_TO_DOC_CLASS.keys())
def doc_class_for_mime_type(mime_type: str) -> DocumentType:
assert mime_type in SUPPORTED_DOC_MIMETYPES
return MIMETYPE_TO_DOC_CLASS[mime_type]
SUFFIX_TO_DOC_CLASS: Mapping[str, DocumentType] = {
"txt": PlainTextDocument,
"html": HTMLDocument,
"rtf": RTFDocument,
"pdf": PDFDocument,
"odt": ODTDocument,
"docx": DocxDocument,
}
SUPPORTED_DOC_SUFFIXES = frozenset(SUFFIX_TO_DOC_CLASS.keys())
def doc_class_for_suffix(suffix: str) -> Type[Document]:
assert suffix in SUPPORTED_DOC_SUFFIXES
return SUFFIX_TO_DOC_CLASS[suffix]