forked from vedangj044/aider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.py
62 lines (49 loc) · 1.92 KB
/
tokenizer.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
from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path
import os
class reader():
def __init__(self, file):
self.file = file
self.pages = convert_from_path(self.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 extractor():
def __init__(self, output_txt_file):
self.output_txt_file = output_txt_file
self.open_file = self.output_txt_file.split("\n")
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.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(extractor(reader("ds1.pdf").full_text).final_list)