- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument_processor.py
More file actions
Latest commit
executable file
·88 lines (71 loc) · 3.39 KB
/
Copy pathdocument_processor.py
File metadata and controls
executable file
·88 lines (71 loc) · 3.39 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
"""
Document Processing Module
Handles document creation, loading from various formats
"""
importos
fromtypingimportList
fromdocximportDocument
importPyPDF2
classDocumentProcessor:
"""Handles document creation, loading, and basic preprocessing"""
def__init__(self):
self.documents= []
defload_document(self, filepath: str) ->str:
"""Auto-detect file type and load document
Args:
filepath: Path to the document file
Returns:
Extracted text from the document
"""
ifnotos.path.exists(filepath):
raiseFileNotFoundError(f"File not found: {filepath}")
ext=os.path.splitext(filepath)[1].lower()
ifext=='.txt':
returnself.load_from_txt(filepath)
elifext=='.pdf':
returnself.load_from_pdf(filepath)
elifextin ['.docx', '.doc']:
returnself.load_from_docx(filepath)
else:
raiseValueError(f"Unsupported file format: {ext}")
defcreate_sample_document(self) ->str:
"""Create a sample document for demonstration"""
sample_text="""
Albert Einstein was a theoretical physicist who developed the theory of relativity.
He was born in Ulm, Germany on March 14, 1879. Einstein worked at the University
of Zurich and later at Princeton University. He received the Nobel Prize in Physics
in 1921 for his explanation of the photoelectric effect.
Marie Curie was a Polish physicist and chemist who conducted pioneering research
on radioactivity. She was the first woman to win a Nobel Prize and remains the only
person to win Nobel Prizes in two different sciences. Marie Curie worked at the
University of Paris and discovered the elements polonium and radium.
Isaac Newton was an English mathematician, physicist, and astronomer. He formulated
the laws of motion and universal gravitation. Newton studied at Cambridge University
and later became a professor there. His work laid the foundation for classical mechanics.
The theory of relativity revolutionized our understanding of space, time, and gravity.
Einstein published his special theory of relativity in 1905 and the general theory
in 1915. This work had profound implications for physics and cosmology.
"""
print("✓ Sample document created")
returnsample_text
defload_from_txt(self, filepath: str) ->str:
"""Load document from text file"""
withopen(filepath, 'r', encoding='utf-8') asf:
text=f.read()
print(f"✓ Loaded document from {filepath}")
returntext
defload_from_docx(self, filepath: str) ->str:
"""Load document from DOCX file"""
doc=Document(filepath)
text='\n'.join([paragraph.textforparagraphindoc.paragraphs])
print(f"✓ Loaded document from {filepath}")
returntext
defload_from_pdf(self, filepath: str) ->str:
"""Load document from PDF file"""
text=""
withopen(filepath, 'rb') asf:
pdf_reader=PyPDF2.PdfReader(f)
forpageinpdf_reader.pages:
text+=page.extract_text()
print(f"✓ Loaded document from {filepath}")
returntext