-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDF_Searcher.py
192 lines (126 loc) · 3.99 KB
/
PDF_Searcher.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
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
#!/usr/bin/env python
# coding: utf-8
# In[4]:
# Data-Name-Extraction-Program
# import packages
import os
# define target directory
target = 'C:\\Users\\hille\\Desktop\\Masterarbeit\\Paper Lehrstuhl'
# performe extraction
content = os.listdir(target)
directory = []
data = []
for elem in content:
elem_path = os.path.join(target, elem)
if os.path.isfile(elem_path):
data.append(elem)
if os.path.isdir(elem_path):
directory.append(elem_path)
for basedirectory, directory, data in os.walk(target):
break
print(basedirectory)
print(directory)
print(data)
# In[3]:
# Search-Program
# import packages
import PyPDF2
import re
# define keyterms
keys = ['WTI', 'Brent', 'maize', 'rice', 'soy', 'wheat', 'cocoa', 'coffee', 'cotton', 'sugar', 'cattle', 'corn' , 'hogs']
# checked_kes = ['BIC', 'Akaike information criterion', 'Final Prediction Error criterion', 'FPE', 'Hannan-Quinn criterion'\
# , 'HQ', 'Schwarz information criterion', 'SIC', 'Granger', 'likelihood ratio']
objects = data
for i in keys:
key = i
print('Search item: ' + str(key))
# open the pdf file
for i in objects:
print('PDF: ' + str(i))
object = PyPDF2.PdfFileReader(target + '\\' + i)
# get number of pages
NumPages = object.getNumPages()
# define counter
count = 0
# extract text and do the search
for i in range(0, NumPages):
PageObj = object.getPage(i)
#print("this is page " + str(i))
Text = PageObj.extractText()
#print(Text)
ResSearch = re.search(key, Text)
if ResSearch == None:
count = count
else:
count+=1
#print(ResSearch)
print(count)
print('Search completed')
# In[2]:
# Search-Program-Class
# import packages
import PyPDF2
import re
import pandas as pd
import numpy as np
# define keyterms
keys = ['BIC', 'Akaike information criterion', 'Prediction Error criterion', 'FPE', 'Hannan-Quinn criterion' , 'HQ', 'Schwarz information criterion', 'SIC', 'Granger']
objects = data
def searcher(key, name, target):
print('Search item: ' + str(key))
print('PDF: ' + str(name))
object = PyPDF2.PdfFileReader(target + '\\' + name)
# get number of pages
NumPages = object.getNumPages()
# define counter
count = 0
# extract text and do the search
for i in range(0, NumPages):
PageObj = object.getPage(i)
#print("this is page " + str(i))
Text = PageObj.extractText()
#print(Text)
ResSearch = re.search(key, Text)
if ResSearch == None:
count = count
else:
count+=1
#print(ResSearch)
print(count)
return count
# In[12]:
import pandas as pd
import numpy as np
df = pd.DataFrame(columns=objects)
print(df)
for i in objects:
df.i =
for i in keys:
key = i
df[key].apply(searcher(key, objects))
def list_col(df, column, index, target):
return (df[column]
print (list_col(df, 'F'))
['a', 'b', 'c']
for index, row in df.iterrows():
date = index
Period = row.loc[row.index[0]]
LocationList = row.index[1:]
print(date, Period)
for Location in LocationList :
PeriodDF.loc[(PeriodDF.index == date)&(PeriodDF.Period == Period), Location] = Get_Y(date, Period, Location)
# In[ ]:
for i in keys:
key = i
for i in objects:
name = i
df.apply(searcher(key, name, target), axis=1, args=('col1', 'col2', 'col3'))
searcher(key, name, target)
print('Search completed')
pd.concat([pd.DataFrame([i], columns=['A']) for i in range(5)],ignore_index=True)
print(df)
# In[ ]:
def max_word_length(row, *cols):
return row[list(cols)].map(len).max()
# Make sure `axis=1` so rows are passed in and we can access columns
df.apply(max_word_length, axis=1, args=('col1', 'col2', 'col3'))