jgyasu commited on
Commit
836fa19
·
verified ·
1 Parent(s): 3f4b594

Update highlighter.py

Browse files
Files changed (1) hide show
  1. highlighter.py +42 -32
highlighter.py CHANGED
@@ -38,48 +38,58 @@ def highlight_common_words(common_words, sentences, title):
38
  </div>
39
  '''
40
 
 
41
  import re
42
 
43
- def highlight_common_words_dict(common_words, sentences, title, bg_color):
44
  color_map = {}
45
  color_index = 0
46
  highlighted_html = []
47
 
48
- for idx, (sentence, score) in enumerate(sentences.items(), start=1):
49
- sentence_with_idx = f"{idx}. {sentence}"
50
- highlighted_sentence = sentence_with_idx
 
51
 
52
- for index, word in common_words:
53
- if word not in color_map:
54
- color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
55
- color_index += 1
56
- escaped_word = re.escape(word)
57
- pattern = rf'\b{escaped_word}\b'
58
- highlighted_sentence = re.sub(
59
- pattern,
60
- lambda m, idx=index, color=color_map[word]: (
61
- f'<span style="background-color: {color}; font-weight: bold;'
62
- f' padding: 1px 2px; border-radius: 2px; position: relative;">'
63
- f'<span style="background-color: black; color: white; border-radius: 50%;'
64
- f' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{idx}</span>'
65
- f'{m.group(0)}'
66
- f'</span>'
67
- ),
68
- highlighted_sentence,
69
- flags=re.IGNORECASE
 
 
 
 
 
 
 
 
 
 
70
  )
71
- highlighted_html.append(
72
- f'<div style="margin-bottom: 5px;">'
73
- f'{highlighted_sentence}'
74
- f'<div style="display: inline-block; margin-left: 5px; border: 1px solid #ddd; padding: 3px 5px; border-radius: 3px; background-color: white; font-size: 0.9em;">'
75
- f'Entailment Score: {score}</div></div>'
76
- )
77
 
78
- final_html = "<br>".join(highlighted_html)
79
  return f'''
80
- <div style="border: solid 1px #; padding: 16px; background-color: {bg_color}; color: #374151; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
81
- <h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
82
  <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
83
  </div>
84
  '''
85
-
 
38
  </div>
39
  '''
40
 
41
+
42
  import re
43
 
44
+ def highlight_common_words_dict(common_words, selected_sentences, discarded_sentences, title):
45
  color_map = {}
46
  color_index = 0
47
  highlighted_html = []
48
 
49
+ def highlight_sentences(sentences, start_idx, section_title):
50
+ nonlocal color_index
51
+ nonlocal color_map
52
+ highlighted_sentences = [f'<h4 style="color: #374151; margin-bottom: 5px;">{section_title}</h4>']
53
 
54
+ for idx, (sentence, score) in enumerate(sentences.items(), start=start_idx):
55
+ sentence_with_idx = f"{idx}. {sentence}"
56
+ highlighted_sentence = sentence_with_idx
57
+
58
+ for index, word in common_words:
59
+ if word not in color_map:
60
+ color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
61
+ color_index += 1
62
+ escaped_word = re.escape(word)
63
+ pattern = rf'\b{escaped_word}\b'
64
+ highlighted_sentence = re.sub(
65
+ pattern,
66
+ lambda m, idx=index, color=color_map[word]: (
67
+ f'<span style="background-color: {color}; font-weight: bold;'
68
+ f' padding: 1px 2px; border-radius: 2px; position: relative;">'
69
+ f'<span style="background-color: black; color: white; border-radius: 50%;'
70
+ f' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{idx}</span>'
71
+ f'{m.group(0)}'
72
+ f'</span>'
73
+ ),
74
+ highlighted_sentence,
75
+ flags=re.IGNORECASE
76
+ )
77
+ highlighted_sentences.append(
78
+ f'<div style="margin-bottom: 5px;">'
79
+ f'{highlighted_sentence}'
80
+ f'<div style="display: inline-block; margin-left: 5px; border: 1px solid #ddd; padding: 3px 5px; border-radius: 3px; background-color: white; font-size: 0.9em;">'
81
+ f'Entailment Score: {score}</div></div>'
82
  )
83
+
84
+ return highlighted_sentences
85
+
86
+ selected_html = highlight_sentences(selected_sentences, 1, "Selected Sentences")
87
+ discarded_html = highlight_sentences(discarded_sentences, 1, "Discarded Sentences")
 
88
 
89
+ final_html = "<br>".join(selected_html + discarded_html)
90
  return f'''
91
+ <div style="border: solid 1px #; padding: 16px; background-color: #FFFFFF; color: #374151; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
92
+ <h3 style="margin-top: 0; font-size: 1em; color: #111827; margin-bottom: 10px;">{title}</h3>
93
  <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
94
  </div>
95
  '''