-
Notifications
You must be signed in to change notification settings - Fork 203
/
search.py
343 lines (288 loc) · 10.7 KB
/
search.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import glob
import os
import warnings
import textract
import requests
from flask import (Flask, json, Blueprint, jsonify, redirect, render_template, request,
url_for)
from gensim.summarization import summarize
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.neighbors import NearestNeighbors
from werkzeug import secure_filename
import pdf2txt as pdf
import PyPDF2
from autocorrect import spell
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'Original_Resumes/'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
class ResultElement:
def __init__(self, rank, filename):
self.rank = rank
self.filename = filename
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
import re, string, unicodedata
import nltk
import contractions
import inflect
from bs4 import BeautifulSoup
from nltk import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import LancasterStemmer, WordNetLemmatizer
def remove_non_ascii(words):
"""Remove non-ASCII characters from list of tokenized words"""
new_words = []
for word in words:
new_word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8', 'ignore')
new_words.append(new_word)
return new_words
def to_lowercase(words):
"""Convert all characters to lowercase from list of tokenized words"""
new_words = []
for word in words:
new_word = word.lower()
new_words.append(new_word)
return new_words
def remove_punctuation(words):
"""Remove punctuation from list of tokenized words"""
new_words = []
for word in words:
new_word = re.sub(r'[^\w\s]', '', word)
if new_word != '':
new_words.append(new_word)
return new_words
def replace_numbers(words):
"""Replace all interger occurrences in list of tokenized words with textual representation"""
p = inflect.engine()
new_words = []
for word in words:
if word.isdigit():
new_word = p.number_to_words(word)
new_words.append(new_word)
else:
new_words.append(word)
return new_words
def remove_stopwords(words):
"""Remove stop words from list of tokenized words"""
new_words = []
for word in words:
# print(word)
if word not in stopwords.words('english'):
new_words.append(word)
return new_words
def stem_words(words):
"""Stem words in list of tokenized words"""
stemmer = LancasterStemmer()
stems = []
for word in words:
stem = stemmer.stem(word)
stems.append(stem)
return stems
def lemmatize_verbs(words):
"""Lemmatize verbs in list of tokenized words"""
lemmatizer = WordNetLemmatizer()
lemmas = []
for word in words:
lemma = lemmatizer.lemmatize(word, pos='v')
lemmas.append(lemma)
return lemmas
def normalize(words):
words = remove_non_ascii(words)
words = to_lowercase(words)
words = remove_punctuation(words)
# words = replace_numbers(words)
words = remove_stopwords(words)
words = stem_words(words)
words = lemmatize_verbs(words)
return words
def getfilepath(loc):
temp = str(loc)
temp = temp.replace('\\', '/')
return temp
def res(jobfile):
Final_Array = []
def lcs(X, Y):
try:
mat = []
for i in range(0,len(X)):
row = []
for j in range(0,len(Y)):
if X[i] == Y[j]:
if i == 0 or j == 0:
row.append(1)
else:
val = 1 + int( mat[i-1][j-1] )
row.append(val)
else:
row.append(0)
mat.append(row)
new_mat = []
for r in mat:
r.sort()
r.reverse()
new_mat.append(r)
lcs = 0
for r in new_mat:
if lcs < r[0]:
lcs = r[0]
return lcs
except:
return -9999
def spellCorrect(string):
words = string.split(" ")
correctWords = []
for i in words:
correctWords.append(spell(i))
return " ".join(correctWords)
def semanticSearch(searchString, searchSentencesList):
result = None
searchString = spellCorrect(searchString)
bestScore = 0
for i in searchSentencesList:
score = lcs(searchString, i)
print(score , i[0:100])
print("")
temp = [score]
Final_Array.extend(temp)
if score > bestScore:
bestScore = score
result = i
return result
app.config['UPLOAD_FOLDER'] = 'Original_Resumes/'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
Resume_Vector = []
Ordered_list_Resume = []
Ordered_list_Resume_Score = []
LIST_OF_FILES = []
LIST_OF_FILES_PDF = []
LIST_OF_FILES_DOC = []
LIST_OF_FILES_DOCX = []
Resumes_File_Names = []
Resumes = []
Temp_pdf = ''
os.chdir('./Original_Resumes')
for file in glob.glob('**/*.pdf', recursive=True):
LIST_OF_FILES_PDF.append(file)
for file in glob.glob('**/*.doc', recursive=True):
LIST_OF_FILES_DOC.append(file)
for file in glob.glob('**/*.docx', recursive=True):
LIST_OF_FILES_DOCX.append(file)
LIST_OF_FILES = LIST_OF_FILES_DOC + LIST_OF_FILES_DOCX + LIST_OF_FILES_PDF
# LIST_OF_FILES.remove("antiword.exe")
print("This is LIST OF FILES")
print(LIST_OF_FILES)
# print("Total Files to Parse\t" , len(LIST_OF_PDF_FILES))
print("####### PARSING ########")
for nooo,i in enumerate(LIST_OF_FILES):
Ordered_list_Resume.append(i)
Temp = i.split(".")
if Temp[1] == "pdf" or Temp[1] == "Pdf" or Temp[1] == "PDF":
try:
print("This is PDF" , nooo)
with open(i,'rb') as pdf_file:
read_pdf = PyPDF2.PdfFileReader(pdf_file)
# page = read_pdf.getPage(0)
# page_content = page.extractText()
# Resumes.extend(Temp_pdf)
number_of_pages = read_pdf.getNumPages()
for page_number in range(number_of_pages):
page = read_pdf.getPage(page_number)
page_content = page.extractText()
page_content = page_content.replace('\n', ' ')
# page_content.replace("\r", "")
Temp_pdf = Temp_pdf + str(page_content)
# Temp_pdf.append(page_content)
# print(Temp_pdf)
Resumes.extend([Temp_pdf])
Temp_pdf = ''
Resumes_File_Names.append(i)
# f = open(str(i)+str("+") , 'w')
# f.write(page_content)
# f.close()
except Exception as e: print(e)
if Temp[1] == "doc" or Temp[1] == "Doc" or Temp[1] == "DOC":
print("This is DOC" , i)
try:
a = textract.process(i)
a = a.replace(b'\n', b' ')
a = a.replace(b'\r', b' ')
b = str(a)
c = [b]
Resumes.extend(c)
Resumes_File_Names.append(i)
except Exception as e: print(e)
if Temp[1] == "docx" or Temp[1] == "Docx" or Temp[1] == "DOCX":
print("This is DOCX" , i)
try:
a = textract.process(i)
a = a.replace(b'\n', b' ')
a = a.replace(b'\r', b' ')
b = str(a)
c = [b]
Resumes.extend(c)
Resumes_File_Names.append(i)
except Exception as e: print(e)
# Resumes.extend(textract.process(i))
if Temp[1] == "ex" or Temp[1] == "Exe" or Temp[1] == "EXE":
# print("This is EXE" , i)
pass
# print("This is length of Resume Vector : " , len(Resumes))
# # # print(Resumes[1][0:10])
# for m , i in enumerate(Resumes):
# print("This is m : " , m , i[0][0:100])
# print("#######################################################################")
for m,i in enumerate(Resumes):
Resumes[m] = nltk.word_tokenize(Resumes[m])
Resumes[m] = normalize(Resumes[m])
Resumes[m] = ' '.join(map(str, Resumes[m]))
jobfile = nltk.word_tokenize(jobfile)
jobfile = normalize(jobfile)
jobfile = ' '.join(map(str, jobfile))
# Resumes2 = np.array(Resumes)
# Resumes2 = Resumes2.ravel()
# print(len(Resumes))
# Resumes = ['microsoft is dumb' , 'google is awesome' , 'facebook is cheater']
print("This is len Resumes : " , len(Resumes))
os.chdir('../')
print("#############################################################")
# a = input("Enter String to Search : ")
print("\n\n")
print("Printing Scores of all Resumes...")
print("\n")
result = semanticSearch(jobfile, Resumes)
print("\n")
print("Printing 1 Best Result.....")
print("\n")
print (result)
print("\n\n")
print("#########################################################")
print("#########################################################")
print("#########################################################")
print("#########################################################")
print("\n\n")
print(Final_Array)
print("This is len Final_Array : " , len(Final_Array))
print(Resumes_File_Names)
print("This is len Ordered_list_Resume : " , len(Resumes_File_Names))
Ordered_list_Resume = Ordered_list_Resume[1:]
# print(Ordered_list_Resume)
Z = [x for _,x in sorted(zip(Final_Array,Resumes_File_Names) , reverse=True)]
flask_return = []
# for n,i in enumerate(Z):
# print("Rankkkkk\t" , n+1, ":\t" , i)
for n,i in enumerate(Z):
# print("Rank\t" , n+1, ":\t" , i)
# flask_return.append(str("Rank\t" , n+1, ":\t" , i))
name = getfilepath(i)
#name = name.split('.')[0]
rank = n
res = ResultElement(rank, name)
flask_return.append(res)
# res.printresult()
# print(f"Rank{res.rank+1} :\t {res.filename}")
return flask_return