Spaces:
Sleeping
Sleeping
Refactor: Production-grade modular layout, NLTK permissions fix, and editorial audit dashboard overhaul
405a3f0 Download src/preprocessing.py from amadmateen/SentinelReview-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/amadmateen/SentinelReview-AI/resolve/main/src/preprocessing.py
- Command line
-
hf download hf://spaces/amadmateen/SentinelReview-AI/src/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/spaces/amadmateen/SentinelReview-AI/resolve/main/src/preprocessing.py
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) | |