File size: 1,651 Bytes
405a3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)