-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract.py
67 lines (52 loc) · 2.28 KB
/
extract.py
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
from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path
import os
class Pdf_reader():
def __init__(self, pdf_file):
self.pdf_file = pdf_file
self.pages = convert_from_path(self.pdf_file, 500)
self.image_counter = 1
for self.page in self.pages:
self.filename = "page_"+str(self.image_counter)+".jpg"
self.page.save(self.filename, 'JPEG')
self.image_counter = self.image_counter + 1
self.filelimit = self.image_counter-1
self.full_text = ""
for i in range(1, self.filelimit + 1):
self.filename = "page_"+str(i)+".jpg"
self.text = str(((pytesseract.image_to_string(Image.open(self.filename)))))
self.text = self.text.replace('-\n', '')
self.full_text+=self.text
class Topic_extractor():
def __init__(self, output_txt_file):
self.output_txt_file = output_txt_file
self.open_file = self.output_txt_file.split("\n") # opnen(self.output_txt_file, "r")
self.list_of_index = []
self.index_count = 0
self.read_lines_of_txt = self.open_file
for i in self.read_lines_of_txt:
if 'unit' in i.lower():
self.list_of_index.append(self.index_count)
self.index_count+=1
# self.successive_diff = [self.list_of_index[i + 1] - self.list_of_index[i] for i in range(len(self.list_of_index)-1)]
# self.successive_diff = int(sum(self.successive_diff)/len(self.successive_diff))+1
# self.list_of_index.append(self.list_of_index[-1]+self.successive_diff)
self.list_of_topics()
def list_of_topics(self):
self.list_of_topic = []
self.final_list = []
for i in range(0, len(self.list_of_index)-1):
self.temp_string = ""
for j in self.read_lines_of_txt[self.list_of_index[i]+1:self.list_of_index[i+1]]:
if j == '\n':
continue
self.temp_string+=j.replace('\n', '')
self.list_of_topic.append(self.temp_string)
for i in self.list_of_topic:
if i == "":
continue
self.final_list.append(i)
return self.final_list
# print(Topic_extractor(Pdf_reader("ds.pdf").full_text).final_list)