SentinelReview-AI / src /preprocessing.py
amadmateen's picture
Refactor: Production-grade modular layout, NLTK permissions fix, and editorial audit dashboard overhaul
405a3f0
Raw History Blame Contribute Delete
1.65 kB
import string
import torch
import numpy as np
def extract_text_features(review_text, sentiment_analyzer):
"""
Extracts 6 review-level metadata features to match inputs expected by the model:
1. Review Length (character count)
2. Sentiment Compound Score (VADER Compound value)
3. Punctuation count
4. Average word length
5. Capitalization ratio
6. Days since first review (static placeholder value = 0)
Returns:
torch.Tensor of shape [1, 6] containing the normalized metadata vector.
"""
# 1. Review character length
review_length = len(review_text)
# 2. VADER Sentiment Compound Score
if sentiment_analyzer is not None:
sentiment = sentiment_analyzer.polarity_scores(review_text)['compound']
else:
sentiment = 0.0
# 3. Punctuation count
punctuation_count = sum(1 for char in review_text if char in string.punctuation)
# 4. Average word length
words = review_text.split()
avg_word_length = np.mean([len(word) for word in words]) if words else 0.0
# 5. Capitalization ratio
capital_letters = sum(1 for char in review_text if char.isupper())
capitalization_ratio = capital_letters / len(review_text) if review_text else 0.0
# 6. Days since first review placeholder
days_since_first_review = 0.0
features = [
float(review_length),
float(sentiment),
float(punctuation_count),
float(avg_word_length),
float(capitalization_ratio),
float(days_since_first_review)
]
return torch.tensor(features, dtype=torch.float32).unsqueeze(0)