-
Notifications
You must be signed in to change notification settings - Fork 203
/
contenfilter.py
262 lines (164 loc) · 5.06 KB
/
contenfilter.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import nltk,re
from nltk.corpus import stopwords
# In[2]:
import os,shutil
import sys
import logging
import six
import pdfminer.settings
pdfminer.settings.STRICT = False
import pdfminer.high_level
import pdfminer.layout
from pdfminer.image import ImageWriter
# In[3]:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer,TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
# In[4]:
stoplist = stopwords.words('english')
stoplist.append('\n')
# In[5]:
skill=open('skills.txt','r')
#print skill.read()
# In[6]:
dir='textresume'
if os.path.exists(dir):
shutil.rmtree(dir)
os.mkdir(dir)
# In[7]:
#os.path.basename(os.listdir('mlresume'))
files_no_ext = [".".join(f.split(".")[:-1]) for f in os.listdir('mlresume')]
print(files_no_ext)
# In[8]:
for f in files_no_ext:
a=open('textresume/'+f+'.txt','a')
a.close()
# In[9]:
resume_pdf=os.listdir('mlresume')
resume_txt=os.listdir('textresume')
# In[10]:
def extract_text(files=[], outfile=[],
_py2_no_more_posargs=None, # Bloody Python2 needs a shim
no_laparams=False, all_texts=None, detect_vertical=None, # LAParams
word_margin=None, char_margin=None, line_margin=None, boxes_flow=None, # LAParams
output_type='text', codec='utf-8', strip_control=False,
maxpages=0, page_numbers=None, password="", scale=1.0, rotation=0,
layoutmode='normal', output_dir=None, debug=False,
disable_caching=False, **other):
if _py2_no_more_posargs is not None:
raise ValueError("Too many positional arguments passed.")
"""if not files:
raise ValueError("Must provide files to work upon!")"""
# If any LAParams group arguments were passed, create an LAParams object and
# populate with given args. Otherwise, set it to None.
if not no_laparams:
laparams = pdfminer.layout.LAParams()
for param in ("all_texts", "detect_vertical", "word_margin", "char_margin", "line_margin", "boxes_flow"):
paramv = locals().get(param, None)
if paramv is not None:
setattr(laparams, param, paramv)
else:
laparams = None
imagewriter = None
if output_dir:
imagewriter = ImageWriter(output_dir)
"""if output_type == "text" and outfile != "-":
for override, alttype in ( (".htm", "html"),
(".html", "html"),
(".xml", "xml"),
(".tag", "tag"),
(".txt","text")):
if outfile.endswith(override):
output_type = alttype"""
if outfile == []:
outfp = sys.stdout
if outfp.encoding is not None:
codec = 'utf-8'
else:
i=0
for outfi in outfile:
fname=files[i]
i+=1
outfp = open('textresume/'+outfi, "w")
with open('mlresume/'+fname, "rb") as fp:
pdfminer.high_level.extract_text_to_fp(fp, **locals())
return
# In[44]:
output=extract_text(resume_pdf,resume_txt)
# In[45]:
for f in resume_txt:
file=open('textresume/'+f,'r+')
data=file.read()
data=re.sub(r'[^\x00-\x7F]+',' ', data)
data=data.replace('\n',' ')
file.seek(0)
file.write(data)
# In[94]:
skill.seek(0)
cv=CountVectorizer(token_pattern = r"(?u)\b\w+\b",stop_words=stoplist)
cv.fit(skill)
# In[95]:
skill.seek(0)
c=cv.transform(skill)
df=pd.DataFrame( columns=cv.get_feature_names())
s1=pd.DataFrame(c.toarray(), columns=cv.get_feature_names())
# In[96]:
for f in os.listdir('textresume'):
file = open('textresume/'+f,'r')
file.seek(0)
y=cv.transform(file)
x=y.toarray().sum(axis=0)
df.loc[len(df)]=x
print df
# In[97]:
skill.seek(0)
tfv=TfidfVectorizer(token_pattern = r"(?u)\b\w+\b",stop_words=stoplist)
tfv.fit(skill)
# In[98]:
print tfv.get_feature_names()
# In[99]:
skill.seek(0)
y=tfv.transform(skill)
# In[100]:
df2=pd.DataFrame(columns=tfv.get_feature_names())
s2=pd.DataFrame(y.toarray(), columns=tfv.get_feature_names())
# In[101]:
for f in os.listdir('textresume'):
file = open('textresume/'+f,'r')
file.seek(0)
y=tfv.transform(file)
x=y.toarray().sum(axis=0)
df2.loc[len(df2)]=x
print df2
# In[141]:
li=[]
for i in range(0,len(df2)):
li.append((s2.loc[0]*df2.loc[i]).sum())
# In[143]:
rating=dict(zip(os.listdir('mlresume'),li))
rating=sorted(rating.items(), key=lambda x:x[1])
rating=rating[::-1]
print rating
# In[144]:
from gensim.summarization import summarize
# In[149]:
"""k=open('textresume/resume5.txt','r')
sumz = summarize(k.read())
v=open('sumz1.txt','w')
v.write(sumz)
v.close()"""
# In[154]:
"""a=open('sumz1.txt','r')
c=cv.transform(a)
m=pd.DataFrame(columns=cv.get_feature_names())
m.loc[len(m)]=c.toarray().sum(axis=0)
print m"""
# In[138]:
# In[ ]:
# In[ ]: