eljanmahammadli commited on
Commit
a1e484f
1 Parent(s): ae43fce

Update highlighter.py

Browse files
Files changed (1) hide show
  1. highlighter.py +8 -6
highlighter.py CHANGED
@@ -6,11 +6,13 @@ from predictors import predict_proba_quillbot
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
  print(sentences_weights)
15
  return sentences_weights
16
 
@@ -32,7 +34,7 @@ def analyze_and_highlight(text):
32
  if not sentence:
33
  continue
34
 
35
- highlighted_sentence = f'<span style="background-color: {color}; color: black;">{sentence}.</span> '
36
  highlighted_text += highlighted_sentence
37
 
38
  return highlighted_text
 
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, predictor, num_features=20, num_samples=300)
10
+ sentences = [sent for sent in sent_tokenize(text)]
11
+ weights_mapping = exp.as_map()[1]
12
+ sentences_weights = {sentence: 0 for sentence in sentences}
13
+ for idx, weight in weights_mapping:
14
+ if 0 <= idx < len(sentences):
15
+ sentences_weights[sentences[idx]] = weight
16
  print(sentences_weights)
17
  return sentences_weights
18
 
 
34
  if not sentence:
35
  continue
36
 
37
+ highlighted_sentence = f'<span style="background-color: {color}; color: black;">{sentence}</span> '
38
  highlighted_text += highlighted_sentence
39
 
40
  return highlighted_text