Update highlighter.py
Browse files- highlighter.py +48 -2
highlighter.py
CHANGED
@@ -32,8 +32,54 @@ def highlight_common_words(common_words, sentences, title):
|
|
32 |
|
33 |
final_html = "<br><br>".join(highlighted_html)
|
34 |
return f'''
|
35 |
-
<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:
|
36 |
<h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
|
37 |
-
<div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius:
|
38 |
</div>
|
39 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
final_html = "<br><br>".join(highlighted_html)
|
34 |
return f'''
|
35 |
+
<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;">
|
36 |
<h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
|
37 |
+
<div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
|
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 |
+
|