-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimilar_resume.py
74 lines (56 loc) · 2.04 KB
/
similar_resume.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
"""Author: Harshendra"""
import nltk
import string
import os
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from nltk.stem.porter import PorterStemmer
path = '/home/vg/work/IIITH/Sematic-Job-Recommendation-Engine/data/txts'
token_dict = {}
stemmer = PorterStemmer()
def stem_tokens(tokens, stemmer):
stemmed = []
for item in tokens:
stemmed.append(stemmer.stem(item).lower())
return stemmed
def tokenize(text):
#tokens = nltk.word_tokenize(text)
tokens = text.split(' ')
#stems = stem_tokens(tokens, stemmer)
return tokens
for subdir, dirs, files in os.walk(path):
for file in files:
file_path = subdir + os.path.sep + file
shakes = open(file_path, 'r')
text = shakes.read()
lowers = text.lower()
#no_punctuation = lowers.translate(None, string.punctuation)
#print string.punctuation
delimeters = ":,;()\"\'\$-%\.{|}"
for ch in list(delimeters):
if ch in lowers:
lowers = lowers.replace(ch," ")
#print lowers
token_dict[file] = lowers
#this can take some time
tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
tfs = tfidf.fit_transform(token_dict.values())
print tfs.shape
while True:
file_path = raw_input("file path> ")
with open(file_path) as d:
text = d.read()
lowers = text.lower()
#no_punctuation = lowers.translate(None, string.punctuation)
#print string.punctuation
delimeters = ":,;()\"\'\$-%\.{|}"
for ch in list(delimeters):
if ch in lowers:
lowers = lowers.replace(ch," ")
test = lowers
response = tfidf.transform([test])
cosine_similarities = linear_kernel(response, tfs).flatten()
related_docs_indices = cosine_similarities.argsort()[:-10:-1]
print 'Similar resumes'
for i in related_docs_indices:
print '%-50s %.4f' % (token_dict.keys()[i].split('.')[0],cosine_similarities[i])