eljanmahammadli commited on
Commit
883ea44
1 Parent(s): b96b103

Update highlighter.py

Browse files
Files changed (1) hide show
  1. highlighter.py +40 -0
highlighter.py CHANGED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from lime.lime_text import LimeTextExplainer
2
+ from nltk.tokenize import sent_tokenize
3
+ from predictors import predict_proba_quillbot
4
+
5
+
6
+ def explainer(text):
7
+ class_names = ['negative', 'positive']
8
+ explainer = LimeTextExplainer(class_names=class_names, split_expression=sent_tokenize)
9
+ exp = explainer.explain_instance(text, predict_proba_quillbot, num_features=20, num_samples=300)
10
+ sentences = [t[0] for t in exp.as_list()]
11
+ attributions = [t[1] for t in exp.as_list()]
12
+ l, weights = zip(*exp.local_exp[exp.available_labels()[0]])
13
+ sentences_weights = {sentences[i]: attributions[i] for i in l}
14
+ return sentences_weights
15
+
16
+
17
+ def analyze_and_highlight(text):
18
+ highlighted_text = ""
19
+ sentences_weights = explainer(text)
20
+ min_weight = min(sentences_weights.values())
21
+ max_weight = max(sentences_weights.values())
22
+
23
+ for sentence, weight in sentences_weights.items():
24
+ normalized_weight = (weight - min_weight) / (max_weight - min_weight)
25
+ if weight >= 0:
26
+ color = f'rgba(255, {255 * (1 - normalized_weight)}, {255 * (1 - normalized_weight)}, 1)'
27
+ else:
28
+ color = f'rgba({255 * normalized_weight}, 255, {255 * normalized_weight}, 1)'
29
+
30
+ sentence = sentence.strip()
31
+ if not sentence:
32
+ continue
33
+
34
+ highlighted_sentence = f'<span style="background-color: {color}; color: black;">{sentence}.</span> '
35
+ highlighted_text += highlighted_sentence
36
+
37
+ return highlighted_text
38
+
39
+
40
+