Spaces:
Running
Running
Update highlighter.py
Browse files- 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,
|
44 |
color_map = {}
|
45 |
color_index = 0
|
46 |
highlighted_html = []
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
-
for
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
)
|
77 |
|
78 |
-
final_html = "<br>".join(
|
79 |
return f'''
|
80 |
-
<div style="border: solid 1px #; padding: 16px; background-color:
|
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 |
'''
|
|