TalentMatch-AI / preprocess.py
Zulkefl07's picture
Upload 6 files
96a2583 verified
Raw
History Blame Contribute Delete
2.35 kB
# ============================================================
# preprocess.py
# This file cleans resume and job description text
# so the AI can understand it better.
# ============================================================
import re
import nltk
# Download required NLTK data (run once)
nltk.download('stopwords', quiet=True)
nltk.download('wordnet', quiet=True)
nltk.download('punkt', quiet=True)
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
# Load English stopwords (common words like "the", "is", "and")
STOPWORDS = set(stopwords.words('english'))
# Lemmatizer converts words to their base form (e.g. "running" β†’ "run")
lemmatizer = WordNetLemmatizer()
def clean_text(text):
"""
Cleans a single piece of text (resume or job description).
Steps: lowercase β†’ remove URLs β†’ remove emails β†’
remove punctuation β†’ remove numbers β†’ remove extra spaces
"""
if not isinstance(text, str):
return "" # return empty string if text is missing/NaN
# Step 1: Convert to lowercase
text = text.lower()
# Step 2: Remove URLs (like https://linkedin.com/...)
text = re.sub(r'http\S+|www\S+', '', text)
# Step 3: Remove email addresses
text = re.sub(r'\S+@\S+', '', text)
# Step 4: Remove punctuation (keep only letters and spaces)
text = re.sub(r'[^a-z\s]', '', text)
# Step 5: Remove extra whitespace
text = re.sub(r'\s+', ' ', text).strip()
return text
def remove_stopwords(text):
"""
Removes common English words that don't add meaning.
Example: 'i am a data scientist' β†’ 'data scientist'
"""
words = text.split()
filtered = [word for word in words if word not in STOPWORDS]
return ' '.join(filtered)
def lemmatize_text(text):
"""
Reduces words to their base/root form.
Example: 'developed building created' β†’ 'develop build create'
"""
words = text.split()
lemmatized = [lemmatizer.lemmatize(word) for word in words]
return ' '.join(lemmatized)
def full_preprocess(text):
"""
Runs the complete NLP preprocessing pipeline:
clean β†’ remove stopwords β†’ lemmatize
Use this function on any resume or job description text.
"""
text = clean_text(text)
text = remove_stopwords(text)
text = lemmatize_text(text)
return text