File size: 1,590 Bytes
c302b1e
 
b06e000
c302b1e
afa6b01
c302b1e
 
2e4daec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c302b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
from scipy.special import softmax
import emoji
# Define the preprocess function
def preprocess(text):
    new_text = []
    for t in text.split(" "):
        # Check if the token is an emoji
        if emoji.is_emoji(t):
            # Add the emoji to the new text list
            new_text.append(t)
        else:
            # Check if the token starts with '@' and has more than one character
            if t.startswith('@') and len(t) > 1:
                # Replace the token with '@user'
                t = '@user'
            # Check if the token starts with 'http'
            if t.startswith('http'):
                # Replace the token with 'http'
                t = 'http'
            # Add the processed token to the new text list
            new_text.append(t)
    return " ".join(new_text)

# Define the sentiment_analysis function
def sentiment_analysis(text, tokenizer, model):
    text = preprocess(text)
    encoded_input = tokenizer(text, return_tensors='pt')
    output = model(**encoded_input)
    scores_ = output[0][0].detach().numpy()
    scores_ = softmax(scores_)
    labels = ['Negative', 'Positive']
    scores = {l: float(s) for (l, s) in zip(labels, scores_)}
    return scores

# Define the map_sentiment_score_to_rating function
def map_sentiment_score_to_rating(score):
    min_score = 0.0
    max_score = 1.0
    min_rating = 1
    max_rating = 10
    rating = ((score - min_score) / (max_score - min_score)) * (max_rating - min_rating) + min_rating
    return rating