rohitashva commited on
Commit
b65cdda
·
verified ·
1 Parent(s): 3b7725f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -148
app.py CHANGED
@@ -1,4 +1,5 @@
1
- # Import necessary libraries
 
2
  import streamlit as st
3
  import nltk
4
  from gensim.models.doc2vec import Doc2Vec, TaggedDocument
@@ -8,17 +9,10 @@ import pandas as pd
8
  import re
9
  import matplotlib.pyplot as plt
10
  import seaborn as sns
11
- import spacy
12
 
13
- # Download necessary NLTK data
14
  nltk.download('punkt')
15
 
16
- # Define regular expressions for pattern matching
17
- float_regex = re.compile(r'^\d{1,2}(\.\d{1,2})?$')
18
- email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
19
- float_digit_regex = re.compile(r'^\d{10}$')
20
- email_with_phone_regex = re.compile(r'(\d{10}).|.(\d{10})')
21
-
22
  # Function to extract text from a PDF file
23
  def extract_text_from_pdf(pdf_file):
24
  pdf_reader = PyPDF2.PdfReader(pdf_file)
@@ -27,182 +21,222 @@ def extract_text_from_pdf(pdf_file):
27
  text += pdf_reader.pages[page_num].extract_text()
28
  return text
29
 
30
- # Function to tokenize text using the NLP model
31
- def tokenize_text(text, nlp_model):
32
- doc = nlp_model(text, disable=["tagger", "parser"])
33
- tokens = [(token.text.lower(), token.label_) for token in doc.ents]
34
- return tokens
35
-
36
- # Function to extract CGPA from a resume
37
- def extract_cgpa(resume_text):
38
- cgpa_pattern = r'\b(?:CGPA|GPA|C\.G\.PA|Cumulative GPA)\s*:?[\s-]([0-9]+(?:\.[0-9]+)?)\b|\b([0-9]+(?:\.[0-9]+)?)\s(?:CGPA|GPA)\b'
39
- match = re.search(cgpa_pattern, resume_text, re.IGNORECASE)
40
- if match:
41
- cgpa = match.group(1) if match.group(1) else match.group(2)
42
- return float(cgpa)
43
- else:
44
- return None
45
-
46
- # Function to extract skills from a resume
47
  def extract_skills(text, skills_keywords):
48
- skills = [skill.lower() for skill in skills_keywords if re.search(r'\b' + re.escape(skill.lower()) + r'\b', text.lower())]
 
49
  return skills
50
 
51
- # Function to preprocess text
52
  def preprocess_text(text):
53
  return word_tokenize(text.lower())
54
 
55
- # Function to train a Doc2Vec model
 
 
 
 
 
 
 
 
 
 
56
  def train_doc2vec_model(documents):
57
  model = Doc2Vec(vector_size=20, min_count=2, epochs=50)
58
  model.build_vocab(documents)
59
- model.train(documents, total_examples=model.corpus_count, epochs=model.epochs)
 
60
  return model
61
 
62
- # Function to calculate similarity between two texts
63
  def calculate_similarity(model, text1, text2):
64
  vector1 = model.infer_vector(preprocess_text(text1))
65
  vector2 = model.infer_vector(preprocess_text(text2))
66
  return model.dv.cosine_similarities(vector1, [vector2])[0]
67
 
68
- # Function to calculate accuracy
69
  def accuracy_calculation(true_positives, false_positives, false_negatives):
70
  total = true_positives + false_positives + false_negatives
71
  accuracy = true_positives / total if total != 0 else 0
72
  return accuracy
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Streamlit Frontend
75
  st.markdown("# Resume Matching Tool 📃📃")
76
  st.markdown("An application to match resumes with a job description.")
77
 
78
  # Sidebar - File Upload for Resumes
79
  st.sidebar.markdown("## Upload Resumes PDF")
80
- resumes_files = st.sidebar.file_uploader("Upload Resumes PDF", type=["pdf"], accept_multiple_files=True)
 
81
 
82
  if resumes_files:
83
  # Sidebar - File Upload for Job Descriptions
84
  st.sidebar.markdown("## Upload Job Description PDF")
85
- job_descriptions_file = st.sidebar.file_uploader("Upload Job Description PDF", type=["pdf"])
 
86
 
87
  if job_descriptions_file:
88
- # Load the pre-trained NLP model
89
- nlp_model_path = "en_Resume_Matching_Keywords"
90
- nlp = spacy.load(nlp_model_path)
 
91
 
92
  # Backend Processing
93
  job_description_text = extract_text_from_pdf(job_descriptions_file)
94
- resumes_texts = [extract_text_from_pdf(resume_file) for resume_file in resumes_files]
95
- job_description_text = extract_text_from_pdf(job_descriptions_file)
96
- job_description_tokens = tokenize_text(job_description_text, nlp)
97
-
98
- # Initialize counters
99
- overall_skill_matches = 0
100
- overall_qualification_matches = 0
101
-
102
- # Create a list to store individual results
103
- results_list = []
104
- job_skills = set()
105
- job_qualifications = set()
106
-
107
- for job_token, job_label in job_description_tokens:
108
- if job_label == 'QUALIFICATION':
109
- job_qualifications.add(job_token.replace('\n', ' '))
110
- elif job_label == 'SKILLS':
111
- job_skills.add(job_token.replace('\n', ' '))
112
-
113
- job_skills_number = len(job_skills)
114
- job_qualifications_number = len(job_qualifications)
115
-
116
- # Lists to store counts of matched skills for all resumes
117
- skills_counts_all_resumes = []
118
-
119
- # Iterate over all uploaded resumes
120
- for uploaded_resume in resumes_files:
121
- resume_text = extract_text_from_pdf(uploaded_resume)
122
- resume_tokens = tokenize_text(resume_text, nlp)
123
-
124
- # Initialize counters for individual resume
125
- skillMatch = 0
126
- qualificationMatch = 0
127
- cgpa = ""
128
-
129
- # Lists to store matched skills and qualifications for each resume
130
- matched_skills = set()
131
- matched_qualifications = set()
132
- email = set()
133
- phone = set()
134
- name = set()
135
-
136
- # Compare the tokens in the resume with the job description
137
- for resume_token, resume_label in resume_tokens:
138
- for job_token, job_label in job_description_tokens:
139
- if resume_token.lower().replace('\n', ' ') == job_token.lower().replace('\n', ' '):
140
- if resume_label == 'SKILLS':
141
- matched_skills.add(resume_token.replace('\n', ' '))
142
- elif resume_label == 'QUALIFICATION':
143
- matched_qualifications.add(resume_token.replace('\n', ' '))
144
- elif resume_label == 'PHONE' and bool(float_digit_regex.match(resume_token)):
145
- phone.add(resume_token)
146
- elif resume_label == 'QUALIFICATION':
147
- matched_qualifications.add(resume_token.replace('\n', ' '))
148
-
149
- skillMatch = len(matched_skills)
150
- qualificationMatch = len(matched_qualifications)
151
-
152
- # Convert the list of emails to a set
153
- email_set = set(re.findall(email_pattern, resume_text.replace('\n', ' ')))
154
- email.update(email_set)
155
-
156
- numberphone=""
157
- for email_str in email:
158
- numberphone = email_with_phone_regex.search(email_str)
159
- if numberphone:
160
- email.remove(email_str)
161
- val=numberphone.group(1) or numberphone.group(2)
162
- phone.add(val)
163
- email.add(email_str.strip(val))
164
-
165
- # Increment overall counters based on matches
166
- overall_skill_matches += skillMatch
167
- overall_qualification_matches += qualificationMatch
168
-
169
- # Add count of matched skills for this resume to the list
170
- skills_counts_all_resumes.append([resume_text.count(skill.lower()) for skill in job_skills])
171
-
172
- # Create a dictionary for the current resume and append to the results list
173
- result_dict = {
174
- "Resume": uploaded_resume.name,
175
- "Similarity Score": (skillMatch/job_skills_number)*100,
176
- "Skill Matches": skillMatch,
177
- "Matched Skills": matched_skills,
178
- "CGPA": extract_cgpa(resume_text),
179
- "Email": email,
180
- "Phone": phone,
181
- "Qualification Matches": qualificationMatch,
182
- "Matched Qualifications": matched_qualifications
183
- }
184
-
185
- results_list.append(result_dict)
186
-
187
- # Display overall matches
188
- st.subheader("Overall Matches")
189
- st.write(f"Total Skill Matches: {overall_skill_matches}")
190
- st.write(f"Total Qualification Matches: {overall_qualification_matches}")
191
- st.write(f"Job Qualifications: {job_qualifications}")
192
- st.write(f"Job Skills: {job_skills}")
193
-
194
- # Display individual results in a table
195
- results_df = pd.DataFrame(results_list)
196
- st.subheader("Individual Results")
197
- st.dataframe(results_df)
198
- tagged_resumes = [TaggedDocument(words=preprocess_text(text), tags=[str(i)]) for i, text in enumerate(resumes_texts)]
199
  model_resumes = train_doc2vec_model(tagged_resumes)
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  st.subheader("\nHeatmap:")
202
-
 
 
203
  # Get skills keywords from user input
204
- skills_keywords_input = st.text_input("Enter skills keywords separated by commas (e.g., python, java, machine learning):")
205
- skills_keywords = [skill.strip() for skill in skills_keywords_input.split(',') if skill.strip()]
 
 
206
 
207
  if skills_keywords:
208
  # Calculate the similarity score between each skill keyword and the resume text
@@ -210,16 +244,20 @@ if resumes_files:
210
  for resume_text in resumes_texts:
211
  resume_text_similarity_scores = []
212
  for skill in skills_keywords:
213
- similarity_score = calculate_similarity(model_resumes, resume_text, skill)
 
214
  resume_text_similarity_scores.append(similarity_score)
215
  skills_similarity_scores.append(resume_text_similarity_scores)
216
 
217
  # Create a DataFrame with the similarity scores and set the index to the names of the PDFs
218
- skills_similarity_df = pd.DataFrame(skills_similarity_scores, columns=skills_keywords, index=[resume_file.name for resume_file in resumes_files])
 
219
 
220
  # Plot the heatmap
221
  fig, ax = plt.subplots(figsize=(12, 8))
222
- sns.heatmap(skills_similarity_df, cmap='YlGnBu', annot=True, fmt=".2f", ax=ax)
 
 
223
  ax.set_title('Heatmap for Skills Similarity')
224
  ax.set_xlabel('Skills')
225
  ax.set_ylabel('Resumes')
@@ -232,6 +270,7 @@ if resumes_files:
232
  else:
233
  st.write("Please enter at least one skill keyword.")
234
 
 
235
  else:
236
  st.warning("Please upload the Job Description PDF to proceed.")
237
  else:
 
1
+ # Importing necessary libraries
2
+ from collections import Counter
3
  import streamlit as st
4
  import nltk
5
  from gensim.models.doc2vec import Doc2Vec, TaggedDocument
 
9
  import re
10
  import matplotlib.pyplot as plt
11
  import seaborn as sns
 
12
 
13
+ # Downloading the 'punkt' tokenizer from NLTK
14
  nltk.download('punkt')
15
 
 
 
 
 
 
 
16
  # Function to extract text from a PDF file
17
  def extract_text_from_pdf(pdf_file):
18
  pdf_reader = PyPDF2.PdfReader(pdf_file)
 
21
  text += pdf_reader.pages[page_num].extract_text()
22
  return text
23
 
24
+ # Function to extract skills from a text using a list of skill keywords
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def extract_skills(text, skills_keywords):
26
+ skills = [skill.lower()
27
+ for skill in skills_keywords if re.search(r'\b' + re.escape(skill.lower()) + r'\b', text.lower())]
28
  return skills
29
 
30
+ # Function to preprocess text by tokenizing and converting to lowercase
31
  def preprocess_text(text):
32
  return word_tokenize(text.lower())
33
 
34
+ # Function to extract mobile numbers from a text
35
+ def extract_mobile_numbers(text):
36
+ mobile_pattern = r'\b\d{10}\b|\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b'
37
+ return re.findall(mobile_pattern, text)
38
+
39
+ # Function to extract emails from a text
40
+ def extract_emails(text):
41
+ email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
42
+ return re.findall(email_pattern, text)
43
+
44
+ # Function to train a Doc2Vec model on a list of tagged documents
45
  def train_doc2vec_model(documents):
46
  model = Doc2Vec(vector_size=20, min_count=2, epochs=50)
47
  model.build_vocab(documents)
48
+ model.train(documents, total_examples=model.corpus_count,
49
+ epochs=model.epochs)
50
  return model
51
 
52
+ # Function to calculate the cosine similarity between two texts using a trained Doc2Vec model
53
  def calculate_similarity(model, text1, text2):
54
  vector1 = model.infer_vector(preprocess_text(text1))
55
  vector2 = model.infer_vector(preprocess_text(text2))
56
  return model.dv.cosine_similarities(vector1, [vector2])[0]
57
 
58
+ # Function to calculate accuracy based on true positives, false positives, and false negatives
59
  def accuracy_calculation(true_positives, false_positives, false_negatives):
60
  total = true_positives + false_positives + false_negatives
61
  accuracy = true_positives / total if total != 0 else 0
62
  return accuracy
63
 
64
+ # Function to extract CGPA from a text
65
+ def extract_cgpa(resume_text):
66
+ # Define a regular expression pattern for CGPA extraction
67
+ cgpa_pattern = r'\b(?:CGPA|GPA|C.G.PA|Cumulative GPA)\s*:?[\s-]* ([0-9]+(?:\.[0-9]+)?)\b|\b([0-9]+(?:\.[0-9]+)?)\s*(?:CGPA|GPA)\b'
68
+
69
+ # Search for CGPA pattern in the text
70
+ match = re.search(cgpa_pattern, resume_text, re.IGNORECASE)
71
+
72
+ # Check if a match is found
73
+ if match:
74
+ cgpa = match.group(1)
75
+ if cgpa is not None:
76
+ return float(cgpa)
77
+ else:
78
+ return float(match.group(2))
79
+ else:
80
+ return None
81
+
82
+ # Regular expressions for email and phone number patterns
83
+ email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
84
+ phone_pattern = r'\b\d{10}\b|\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b'
85
+
86
  # Streamlit Frontend
87
  st.markdown("# Resume Matching Tool 📃📃")
88
  st.markdown("An application to match resumes with a job description.")
89
 
90
  # Sidebar - File Upload for Resumes
91
  st.sidebar.markdown("## Upload Resumes PDF")
92
+ resumes_files = st.sidebar.file_uploader(
93
+ "Upload Resumes PDF", type=["pdf"], accept_multiple_files=True)
94
 
95
  if resumes_files:
96
  # Sidebar - File Upload for Job Descriptions
97
  st.sidebar.markdown("## Upload Job Description PDF")
98
+ job_descriptions_file = st.sidebar.file_uploader(
99
+ "Upload Job Description PDF", type=["pdf"])
100
 
101
  if job_descriptions_file:
102
+ # Sidebar - Sorting Options
103
+ sort_options = ['Weighted Score', 'Similarity Score']
104
+ selected_sort_option = st.sidebar.selectbox(
105
+ "Sort results by", sort_options)
106
 
107
  # Backend Processing
108
  job_description_text = extract_text_from_pdf(job_descriptions_file)
109
+ resumes_texts = [extract_text_from_pdf(
110
+ resume_file) for resume_file in resumes_files]
111
+
112
+ tagged_resumes = [TaggedDocument(words=preprocess_text(
113
+ text), tags=[str(i)]) for i, text in enumerate(resumes_texts)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  model_resumes = train_doc2vec_model(tagged_resumes)
115
 
116
+ true_positives_mobile = 0
117
+ false_positives_mobile = 0
118
+ false_negatives_mobile = 0
119
+
120
+ true_positives_email = 0
121
+ false_positives_email = 0
122
+ false_negatives_email = 0
123
+
124
+ results_data = {'Resume': [], 'Similarity Score': [],
125
+ 'Weighted Score': [], 'Email': [], 'Contact': [], 'CGPA': []}
126
+
127
+ for i, resume_text in enumerate(resumes_texts):
128
+ extracted_mobile_numbers = set(extract_mobile_numbers(resume_text))
129
+ extracted_emails = set(extract_emails(resume_text))
130
+ extracted_cgpa = extract_cgpa(resume_text)
131
+
132
+ ground_truth_mobile_numbers = {'1234567890', '9876543210'}
133
+ ground_truth_emails = {
134
135
+
136
+ true_positives_mobile += len(
137
+ extracted_mobile_numbers.intersection(ground_truth_mobile_numbers))
138
+ false_positives_mobile += len(
139
+ extracted_mobile_numbers.difference(ground_truth_mobile_numbers))
140
+ false_negatives_mobile += len(
141
+ ground_truth_mobile_numbers.difference(extracted_mobile_numbers))
142
+
143
+ true_positives_email += len(
144
+ extracted_emails.intersection(ground_truth_emails))
145
+ false_positives_email += len(
146
+ extracted_emails.difference(ground_truth_emails))
147
+ false_negatives_email += len(
148
+ ground_truth_emails.difference(extracted_emails))
149
+
150
+ similarity_score = calculate_similarity(
151
+ model_resumes, resume_text, job_description_text)
152
+
153
+ other_criteria_score = 0
154
+
155
+ weighted_score = (0.6 * similarity_score) + \
156
+ (0.4 * other_criteria_score)
157
+
158
+ results_data['Resume'].append(resumes_files[i].name)
159
+ results_data['Similarity Score'].append(similarity_score * 100)
160
+ results_data['Weighted Score'].append(weighted_score)
161
+
162
+ emails = ', '.join(re.findall(email_pattern, resume_text))
163
+ contacts = ', '.join(re.findall(phone_pattern, resume_text))
164
+ results_data['Email'].append(emails)
165
+ results_data['Contact'].append(contacts)
166
+ results_data['CGPA'].append(extracted_cgpa)
167
+
168
+ results_df = pd.DataFrame(results_data)
169
+
170
+ if selected_sort_option == 'Similarity Score':
171
+ results_df = results_df.sort_values(
172
+ by='Similarity Score', ascending=False)
173
+ else:
174
+ results_df = results_df.sort_values(
175
+ by='Weighted Score', ascending=False)
176
+
177
+ st.subheader(f"Results Table (Sorted by {selected_sort_option}):")
178
+
179
+ # Define a custom function to highlight maximum values in the specified columns
180
+ def highlight_max(data, color='grey'):
181
+ is_max = data == data.max()
182
+ return [f'background-color: {color}' if val else '' for val in is_max]
183
+
184
+ # Apply the custom highlighting function to the DataFrame
185
+ st.dataframe(results_df.style.apply(highlight_max, subset=[
186
+ 'Similarity Score', 'Weighted Score', 'CGPA']))
187
+
188
+
189
+ highest_score_index = results_df['Similarity Score'].idxmax()
190
+ highest_score_resume_name = resumes_files[highest_score_index].name
191
+
192
+ st.subheader("\nDetails of Highest Similarity Score Resume:")
193
+ st.write(f"Resume Name: {highest_score_resume_name}")
194
+ st.write(
195
+ f"Similarity Score: {results_df.loc[highest_score_index, 'Similarity Score']:.2f}")
196
+
197
+ if 'Weighted Score' in results_df.columns:
198
+ weighted_score_value = results_df.loc[highest_score_index,
199
+ 'Weighted Score']
200
+ st.write(f"Weighted Score: {weighted_score_value:.2f}" if pd.notnull(
201
+ weighted_score_value) else "Weighted Score: Not Mentioned")
202
+ else:
203
+ st.write("Weighted Score: Not Mentioned")
204
+
205
+ if 'Email' in results_df.columns:
206
+ email_value = results_df.loc[highest_score_index, 'Email']
207
+ st.write(f"Email: {email_value}" if pd.notnull(
208
+ email_value) else "Email: Not Mentioned")
209
+ else:
210
+ st.write("Email: Not Mentioned")
211
+
212
+ if 'Contact' in results_df.columns:
213
+ contact_value = results_df.loc[highest_score_index, 'Contact']
214
+ st.write(f"Contact: {contact_value}" if pd.notnull(
215
+ contact_value) else "Contact: Not Mentioned")
216
+ else:
217
+ st.write("Contact: Not Mentioned")
218
+
219
+ if 'CGPA' in results_df.columns:
220
+ cgpa_value = results_df.loc[highest_score_index, 'CGPA']
221
+ st.write(f"CGPA: {cgpa_value}" if pd.notnull(
222
+ cgpa_value) else "CGPA: Not Mentioned")
223
+ else:
224
+ st.write("CGPA: Not Mentioned")
225
+
226
+ mobile_accuracy = accuracy_calculation(
227
+ true_positives_mobile, false_positives_mobile, false_negatives_mobile)
228
+ email_accuracy = accuracy_calculation(
229
+ true_positives_email, false_positives_email, false_negatives_email)
230
+
231
  st.subheader("\nHeatmap:")
232
+ # st.write(f"Mobile Number Accuracy: {mobile_accuracy:.2%}")
233
+ # st.write(f"Email Accuracy: {email_accuracy:.2%}")
234
+
235
  # Get skills keywords from user input
236
+ skills_keywords_input = st.text_input(
237
+ "Enter skills keywords separated by commas (e.g., python, java, machine learning):")
238
+ skills_keywords = [skill.strip()
239
+ for skill in skills_keywords_input.split(',') if skill.strip()]
240
 
241
  if skills_keywords:
242
  # Calculate the similarity score between each skill keyword and the resume text
 
244
  for resume_text in resumes_texts:
245
  resume_text_similarity_scores = []
246
  for skill in skills_keywords:
247
+ similarity_score = calculate_similarity(
248
+ model_resumes, resume_text, skill)
249
  resume_text_similarity_scores.append(similarity_score)
250
  skills_similarity_scores.append(resume_text_similarity_scores)
251
 
252
  # Create a DataFrame with the similarity scores and set the index to the names of the PDFs
253
+ skills_similarity_df = pd.DataFrame(
254
+ skills_similarity_scores, columns=skills_keywords, index=[resume_file.name for resume_file in resumes_files])
255
 
256
  # Plot the heatmap
257
  fig, ax = plt.subplots(figsize=(12, 8))
258
+
259
+ sns.heatmap(skills_similarity_df,
260
+ cmap='YlGnBu', annot=True, fmt=".2f", ax=ax)
261
  ax.set_title('Heatmap for Skills Similarity')
262
  ax.set_xlabel('Skills')
263
  ax.set_ylabel('Resumes')
 
270
  else:
271
  st.write("Please enter at least one skill keyword.")
272
 
273
+
274
  else:
275
  st.warning("Please upload the Job Description PDF to proceed.")
276
  else: