File size: 1,489 Bytes
883ea44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from lime.lime_text import LimeTextExplainer
from nltk.tokenize import sent_tokenize
from predictors import predict_proba_quillbot


def explainer(text):
    class_names = ['negative', 'positive']
    explainer = LimeTextExplainer(class_names=class_names, split_expression=sent_tokenize)
    exp = explainer.explain_instance(text, predict_proba_quillbot, num_features=20, num_samples=300)
    sentences = [t[0] for t in exp.as_list()]
    attributions = [t[1] for t in exp.as_list()]
    l, weights = zip(*exp.local_exp[exp.available_labels()[0]])
    sentences_weights = {sentences[i]: attributions[i] for i in l}
    return sentences_weights


def analyze_and_highlight(text):
    highlighted_text = ""
    sentences_weights = explainer(text)
    min_weight = min(sentences_weights.values())
    max_weight = max(sentences_weights.values())

    for sentence, weight in sentences_weights.items():
        normalized_weight = (weight - min_weight) / (max_weight - min_weight)
        if weight >= 0:
            color = f'rgba(255, {255 * (1 - normalized_weight)}, {255 * (1 - normalized_weight)}, 1)'
        else:
            color = f'rgba({255 * normalized_weight}, 255, {255 * normalized_weight}, 1)'

        sentence = sentence.strip()
        if not sentence:
            continue
    
        highlighted_sentence = f'<span style="background-color: {color}; color: black;">{sentence}.</span> '
        highlighted_text += highlighted_sentence
    
    return highlighted_text