cv_quality / personal_information.py
Nassiraaa's picture
Update personal_information.py
b16cf34 verified
import json
import re
from openai_utils import get_ai_response
from cv_prompt import get_personal_info_prompt
from cv_quality import CV
# Load the scoring data
with open('personal_info_scores.json', 'r') as f:
score_data = json.load(f)
def extract_email(text):
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
emails = re.findall(email_pattern, text)
return emails[0] if emails else None
def extract_phone(text):
phone_pattern = r'\b(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}\b'
phones = re.findall(phone_pattern, text)
return phones[0] if phones else None
def extract_location(text):
prompt = get_personal_info_prompt(text)
messages = [
{"role": "user", "content": prompt}
]
response = get_ai_response(messages)
if response:
try:
location_data = json.loads(response)
city_present = any(location_data.get('city', {}).values())
country_present = any(location_data.get('country', {}).values())
except json.JSONDecodeError:
print("Failed to parse JSON from response")
city_present, country_present = False, False
else:
city_present, country_present = False, False
return city_present, country_present
def calculate_score(email_exists, phone_exists, city_exists, country_exists):
score = 0
if email_exists:
score += score_data['email']
if phone_exists:
score += score_data['phone']
if city_exists:
score += score_data['city']
if country_exists:
score += score_data['country']
return score
def analyze_personal_info(file_path):
cv = CV(file_path)
text = cv.get_cv_text()
email = extract_email(text)
phone = extract_phone(text)
city_present, country_present = extract_location(text)
email_exists = email is not None
phone_exists = phone is not None
score = calculate_score(email_exists, phone_exists, city_present, country_present)
result = {
"email": email_exists,
"phone": phone_exists,
"city": city_present,
"country": country_present,
"personal_info_score": score
}
return result