generated from CS196Illinois/SP24-Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextextractor.py
55 lines (40 loc) · 1.88 KB
/
textextractor.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
from pypdf import PdfReader
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import re
import pdfplumber
from bot import Chatbot
class Extractor:
def clean_text(self,text):
# Replace newlines with spaces
text = text.replace("\n", " ")
text = text.replace("\t", " ")
text = re.sub(r'\s+', ' ', text)
text = text.encode("ascii", "ignore").decode()
return text.strip()
def __init__(self, resume, job_desc):
#Needed for lengths
self.reader = PdfReader(resume)
with pdfplumber.open(resume) as pdf:
first_page = pdf.pages[0]
raw_text = first_page.extract_text()
self.resume = self.clean_text(raw_text)
with pdfplumber.open(job_desc) as pdf:
first_page = pdf.pages[0]
raw_text = first_page.extract_text()
self.job_desc = self.clean_text(raw_text)
def pagesLen(self):
return (len(self.reader.pages))
def calculate_similarity(self):
#TF = number of times a term appears / total number of terms
#ID = measures how important a word is across document with less importance to little words
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform([self.resume, self.job_desc])
similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])
return similarity[0][0]
# sampleResume = "/Users/sriramnatarajan/Documents/FA24-Group8/uploads/sampleResume.pdf"
# jobDesc = "/Users/sriramnatarajan/Documents/FA24-Group8/uploads/sampleJobDesc.pdf"
# nlpBot = Chatbot()
# tester = Extractor(sampleResume, jobDesc)
# print(nlpBot.resumeReview(tester.resume, tester.job_desc, tester.calculate_similarity()))
# print(bot(tester.get_resume(), tester.job_desc(jobDesc), tester.calculate_similarity(jobDesc)))