Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils.py
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import glob
|
| 4 |
+
import re
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def extract_employee_data(employee_name):
|
| 8 |
+
"""Extract employee details using name from the Markdown file."""
|
| 9 |
+
details = {}
|
| 10 |
+
directory = "hrdataset/employees"
|
| 11 |
+
|
| 12 |
+
# Search for employee markdown file
|
| 13 |
+
search_pattern = f"{directory}/*_{employee_name.replace(' ', '_')}.md"
|
| 14 |
+
matching_files = glob.glob(search_pattern)
|
| 15 |
+
|
| 16 |
+
if not matching_files:
|
| 17 |
+
return f"Error: Employee file for {employee_name} not found."
|
| 18 |
+
|
| 19 |
+
employee_path = matching_files[0]
|
| 20 |
+
|
| 21 |
+
# Read the employee markdown file
|
| 22 |
+
with open(employee_path, "r", encoding="utf-8") as file:
|
| 23 |
+
content = file.read()
|
| 24 |
+
|
| 25 |
+
# Extract employee details using regex
|
| 26 |
+
details["name"] = re.search(r"\*\*Name:\*\* (.+)", content).group(1)
|
| 27 |
+
details["role"] = re.search(r"\*\*Role:\*\* (.+)", content).group(1)
|
| 28 |
+
details["joining_date"] = re.search(r"\*\*Joining Date:\*\* (\d{4}-\d{2}-\d{2})", content).group(1)
|
| 29 |
+
|
| 30 |
+
# Calculate experience
|
| 31 |
+
joining_year = int(details["joining_date"].split("-")[0])
|
| 32 |
+
details["experience"] = 2024 - joining_year
|
| 33 |
+
|
| 34 |
+
# Extract performance ratings
|
| 35 |
+
rating_match = re.findall(r"\*\*(\d{4}):\*\* ([\d.]+)", content)
|
| 36 |
+
if rating_match:
|
| 37 |
+
latest_rating = sorted(rating_match, key=lambda x: int(x[0]))[-1]
|
| 38 |
+
details["rating"] = float(latest_rating[1])
|
| 39 |
+
|
| 40 |
+
return details
|
| 41 |
+
|
| 42 |
+
def get_survey_sentiment(employee_name):
|
| 43 |
+
"""Retrieve sentiment analysis for an employee from survey data."""
|
| 44 |
+
survey_file_path = "hrdataset/surveys/Employee_Culture_Survey_Responses.csv"
|
| 45 |
+
|
| 46 |
+
if not os.path.exists(survey_file_path):
|
| 47 |
+
return "Error: Survey data not found."
|
| 48 |
+
|
| 49 |
+
survey_df = pd.read_csv(survey_file_path)
|
| 50 |
+
filtered_df = survey_df[survey_df['Employee'].str.strip().str.lower() == employee_name.strip().lower()]
|
| 51 |
+
|
| 52 |
+
if filtered_df.empty:
|
| 53 |
+
return "Error: No survey data found for this employee."
|
| 54 |
+
|
| 55 |
+
sentiment_counts = filtered_df['Sentiment'].str.strip().str.lower().value_counts()
|
| 56 |
+
positive_count = sentiment_counts.get('positive', 0)
|
| 57 |
+
negative_count = sentiment_counts.get('negative', 0)
|
| 58 |
+
|
| 59 |
+
if positive_count > negative_count:
|
| 60 |
+
return "Positive"
|
| 61 |
+
elif negative_count > positive_count:
|
| 62 |
+
return "Negative"
|
| 63 |
+
else:
|
| 64 |
+
return "Neutral"
|