-
Notifications
You must be signed in to change notification settings - Fork 2
/
resume_classification.py
185 lines (162 loc) · 5.67 KB
/
resume_classification.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
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.neighbors import KNeighborsClassifier
import joblib
import sys
from database_functionalities import insertIntoCategoryCollection
def getVectorizer(skills):
try:
skillsSet = []
for skill in skills:
skill = skill.replace(" ", "")
skillsSet.append(skill)
# ... skillSet=["machinelearning","java","c++","nodejs"]
vectorizer = CountVectorizer(tokenizer=lambda txt: txt.split())
# ... Joined skills string = "machinelearning java c++ nodejs"
vocabulary = vectorizer.fit([" ".join(skillsSet)])
# print(vocabulary.get_feature_names())
return vectorizer
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def removePunctuation(s):
try:
newS = ""
punctuations = '''!()|-[]{};:'"\,<>./?@$%^&_~'''
for i in s:
if (i not in punctuations):
newS = newS + i
return newS
except ValueError:
print("Value Error :", sys.exc_info()[0])
return ""
except:
print("Unexpected error:", sys.exc_info()[0])
return ""
def readCategory():
try:
dictCat = {}
idx = 0
data = pd.read_csv("./Datasets/category.csv")
category = data["category"].tolist()
distinct_category = list(set(category))
distinct_category.sort()
for i in distinct_category:
dictCat[i] = idx
idx = idx + 1
# ... Dict category has the format ==> {"software engineer jobs" -> 57}
# To insert the category into category collection uncomment the below line.. ..
# insertIntoCategoryCollection(dictCat)
return distinct_category, dictCat
except ValueError:
print("Value Error :", sys.exc_info()[0])
return [], {}
except:
print("Unexpected error:", sys.exc_info()[0])
return [], {}
def cleanSKillColumn(skills):
try:
cleaned = []
for skill in skills:
temp = skill.replace(",", " ").replace("/", " ").replace(".", " ").replace(" ", "").lower()
cleaned.append(temp)
return cleaned
except ValueError:
print("Value Error :", sys.exc_info()[0])
return []
except:
print("Unexpected error:", sys.exc_info()[0])
return []
def getMatrixInput(skills, test_cat, dictCategory, vectorizer):
mat = []
cat = []
for idx in range(0, len(skills)):
arr = eval(skills[idx])
if (len(arr) != 0):
arr = cleanSKillColumn(arr)
temp = vectorizer.transform([" ".join(arr)]).toarray()
mat.append(temp[0])
cat.append(dictCategory[test_cat[idx]])
return mat, cat
def getStoredEncodedClasses():
data = pd.read_csv("./Datasets/encoded_category.csv")
enc_cat = data["encoded_category"].tolist()
cat = data["category"].tolist()
dict = {}
for idx in range(0, len(cat)):
dict[enc_cat[idx]] = cat[idx]
# ... dict = {57 -> "Software Engineer Jobs"}
# ... cat = ["Softeare enginerr jobs" ,"Softeare enginerr jobs","Java jobs"]
return dict, cat
def getSkillSet():
data = pd.read_csv("./Datasets/new_skills.csv")
skills_data = data["skills"].tolist()
lower_skills = []
for skills in skills_data:
if (skills == skills):
skills = removePunctuation(skills)
skills = skills.lower()
lower_skills.append(skills)
return lower_skills
def getEncodedCat(test_cat, dictCategory):
arr = []
for i in test_cat:
arr.append(dictCategory[i])
return arr
# def trainKnn():
# try:
# category, dictCategory = readCategory()
# skillSet = getSkillSet()
# vectorizer = getVectorizer(skillSet)
# data = pd.read_csv("./Datasets/jobs_skills.csv")
#
# X, Y = getMatrixInput(data["skills"].tolist(), data["category"].tolist(), dictCategory, vectorizer)
#
# # ... X=[
# # [1,1,1,1,1,1],
# # [1,0,1,0,1,1]
# # ]
# # ... Y=[57,40]
#
# X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, shuffle=True)
# knn = KNeighborsClassifier(n_neighbors=9)
# knn.fit(X, Y)
# joblib.dump(knn, './models/knnClassifier.pkl')
#
# except ValueError:
# print("Value Error :",sys.exc_info()[0])
# raise
# except:
# print("Unexpected error:", sys.exc_info()[0])
# raise
def getClassification(skills):
try:
dictEncodedCat, Cat = getStoredEncodedClasses()
# ... dictEncodedCat={57 -> "Software Engineer Jobs"}
knn = joblib.load("./models/knnClassifier.pkl")
skillSet = getSkillSet()
vectorizer = getVectorizer(skillSet)
array = cleanSKillColumn(skills)
vect = vectorizer.transform([" ".join(array)]).toarray()
ans = knn.predict(vect)
# print(ans)
dist, ind = knn.kneighbors(vect)
cat_list = []
for i in range(0, 3):
# print(i,Cat[ind[0][i]])
cat_list.append(Cat[ind[0][i]].replace("Jobs", "Profile"))
# print(dictEncodedCat[ans[0]])
cat_list.append(dictEncodedCat[ans[0]].replace("Jobs", "Profile"))
return list(set(cat_list))
except ValueError:
print("Value Error :", sys.exc_info()[0])
return []
except:
print("Unexpected error:", sys.exc_info()[0])
return []
if __name__ == "__main__":
print(getClassification(["python", "C++", "machine learning", "golang"]))