-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jd_reader.py
196 lines (171 loc) · 5.29 KB
/
Jd_reader.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
import textract
import re
import sys
import pandas as pd
from pymongo import MongoClient
from bson.objectid import ObjectId
def convert(fname):
try:
text = textract.process(fname, method='pdfminer')
return text
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def connect_db():
try:
client = MongoClient(
'mongodb+srv://admin:[email protected]/Resume-parser?retryWrites=true&w=majority')
return client
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def insert_jd(client, details):
try:
db = client["Resume-parser"]
jd_collection = db.jobs
details['_id'] = ObjectId()
resume_id = jd_collection.insert_one(details).inserted_id
return resume_id
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def getSkills(resume_content, skills_list):
try:
skills = []
punctuations = '''!()|-[]{};:'"\,<>./?@$%^&_~'''
for st in resume_content:
no_punct = ""
for char in st:
if char not in punctuations:
no_punct = no_punct + char
words = no_punct.split(' ')
l = no_punct.split()
result = []
for i in range(len(l) - 1):
result.append(l[i] + ' ' + l[i + 1])
words = list(set(list(set(words)) + list(set(result))))
for word in words:
if (word.lower().strip() in skills_list or word.strip() in skills_list):
skills.append(word.lower().strip())
return list(set(skills))
except ValueError:
print("Value Error :", sys.exc_info()[0])
return []
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def read_skills_dataset():
try:
df = pd.read_csv("./Datasets/new_skills.csv")
skills_list = list(df["skills"].values)
skills_list_lower = []
for i in skills_list:
skills_list_lower.append(str(i).lower())
return skills_list_lower
except ValueError:
print("Value Error :", sys.exc_info()[0])
return []
except:
print("Unexpected error:", sys.exc_info()[0])
return []
def getCompanyName(jd_arr):
try:
ans = ""
for elem in jd_arr:
print(elem)
arr = elem.split(":")
if (len(arr) and arr[0].strip().lower() == "company name"):
ans = arr[1].strip()
break
return ans
except ValueError:
print("Value Error :", sys.exc_info()[0])
return ""
except:
print("Unexpected error:", sys.exc_info()[0])
return ""
def getCategory(jd_arr):
try:
ans = ""
for elem in jd_arr:
arr = elem.split(":")
if (len(arr) and arr[0].strip().lower() == "category"):
for i in range(1, len(arr)):
ans = ans + arr[i] + " "
return ans
except ValueError:
print("Value Error :", sys.exc_info()[0])
return ""
except:
print("Unexpected error:", sys.exc_info()[0])
return ""
def getDescription(jd_arr):
try:
ans = ""
for elem in jd_arr:
arr = elem.split(":")
if (len(arr) and arr[0].strip().lower() == "description"):
for i in range(1, len(arr)):
ans = ans + arr[i].strip() + " "
break
return ans
except ValueError:
print("Value Error :", sys.exc_info()[0])
return ""
except:
print("Unexpected error:", sys.exc_info()[0])
return ""
def getInfo(jd_arr):
try:
data = {}
data["company_name"] = getCompanyName(jd_arr)
data["category"] = getCategory(jd_arr)
description = getDescription(jd_arr)
skills_list = read_skills_dataset()
description = description.split("\n")
print("Description : ", description)
skills_list = getSkills(description, skills_list)
data["skills"] = skills_list
return data
except ValueError:
print("Value Error :", sys.exc_info()[0])
return {}
except:
print("Unexpected error:", sys.exc_info()[0])
return {}
def insertJdIntoDb(data):
try:
connect = connect_db()
insert_jd(connect, data)
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def parseJd(filename, insert=1):
try:
text = convert("./JD/" + filename).decode('utf-8')
jd_arr = re.split(r'[\n\r]{2,}', text)
data = getInfo(jd_arr)
data["filename"] = filename
print(data)
if (insert):
insertJdIntoDb(data)
except ValueError:
print("Value Error :", sys.exc_info()[0])
raise
except:
print("Unexpected error:", sys.exc_info()[0])
raise
if __name__ == "__main__":
parseJd("jd02.pdf", 0)