-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_convert.py
58 lines (52 loc) · 1.97 KB
/
pdf_convert.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
import os
import logging
import pdf_converter
import pickle
import re
import spacy
from spacy.lang.en import English
import tensorflow_hub as hub
import Timer as t
def convert(file_path):
try:
with open("sentences.txt", 'rb') as f:
sentences = pickle.load(f)
except:
pdf_texts = []
sentences = []
timer_convert = t.Timer()
print(f'PDF conversion start: {timer_convert}.')
for filename in os.listdir(file_path):
if (".pdf" in filename):
print("Converting " + filename + " to pdf.")
print(file_path + "/" + filename)
pdf_texts.append(pdf_converter.convert(file_path + "/" + filename))
if (".txt" in filename):
print("Converting " + filename + " to array.")
print(file_path + "/" + filename)
with open(file_path + "/" + filename) as f:
for line in f:
sentences.append(line)
return sentences
print(f'PDF conversion end: {timer_convert}.')
timer_spacy = t.Timer()
print(f'Spacy load start: {timer_spacy}.')
# python -m spacy download en_core_web_md you will need to install this on first load
nlp = spacy.load('en_core_web_md')
logging.getLogger('tensorflow').disabled = True #OPTIONAL - to disable outputs from Tensorflow
timer_convert = t.Timer()
print(f'Spacy load end: {timer_spacy}.')
timer_nlp = t.Timer()
text = ""
for pdf_text in pdf_texts:
text += pdf_text.lower().replace('\n', ' ').replace('\t', ' ').replace('\xa0',' ')
text += "\n"
text = ' '.join(text.split())
nlp.max_length = 1000000000
doc = nlp(text)
for i in doc.sents:
if len(i) > 1:
sentences.append(i.string.strip())
with open("sentences.txt", 'wb') as f:
pickle.dump(sentences, f)
return sentences