aiisc-watermarking-model / highlighter.py
BheemaShankerNeyigapula's picture
Upload folder using huggingface_hub
ea6afa4 verified
import re
def highlight_common_words(common_words, sentences, title):
color_map = {}
highlighted_html = []
for idx, sentence in enumerate(sentences, start=1):
highlighted_sentence = f"{idx}. {sentence}"
for index, word in common_words:
if word not in color_map:
# Assign color using HSL for better visual distinction
color_map[word] = f'hsl({(len(color_map) % 6) * 60}, 70%, 80%)'
# Create a regex pattern for the word
escaped_word = re.escape(word)
pattern = rf'\b{escaped_word}\b'
color = color_map[word]
# Use a lambda function for word highlighting
highlighted_sentence = re.sub(
pattern,
lambda m: (f'<span style="background-color: {color}; font-weight: bold;'
' padding: 2px 4px; border-radius: 2px; position: relative;">'
f'<span style="background-color: black; color: white; border-radius: 50%;'
' padding: 2px 5px; margin-right: 5px;">{index}</span>'
f'{m.group(0)}'
'</span>'),
highlighted_sentence,
flags=re.IGNORECASE
)
highlighted_html.append(highlighted_sentence)
# Construct the final HTML output
return generate_html(title, highlighted_html)
def highlight_common_words_dict(common_words, sentences, title):
color_map = {}
highlighted_html = []
for idx, (sentence, score) in enumerate(sentences.items(), start=1):
highlighted_sentence = f"{idx}. {sentence}"
for index, word in common_words:
if word not in color_map:
color_map[word] = f'hsl({(len(color_map) % 6) * 60}, 70%, 80%)'
escaped_word = re.escape(word)
pattern = rf'\b{escaped_word}\b'
color = color_map[word]
highlighted_sentence = re.sub(
pattern,
lambda m: (f'<span style="background-color: {color}; font-weight: bold;'
' padding: 1px 2px; border-radius: 2px; position: relative;">'
f'<span style="background-color: black; color: white; border-radius: 50%;'
' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{index}</span>'
f'{m.group(0)}'
'</span>'),
highlighted_sentence,
flags=re.IGNORECASE
)
highlighted_html.append(
f'<div style="margin-bottom: 5px;">'
f'{highlighted_sentence}'
f'<div style="display: inline-block; margin-left: 5px; padding: 3px 5px; border-radius: 3px;'
' background-color: white; font-size: 0.9em;">Entailment Score: {score}</div></div>'
)
return generate_html(title, highlighted_html)
def generate_html(title, highlighted_html):
final_html = "<br><br>".join(highlighted_html)
return f'''
<div style="border: solid 1px #ccc; padding: 16px; background-color: #FFFFFF; color: #374151;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
<h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
<div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
</div>
'''
def reparaphrased_sentences_html(sentences):
formatted_sentences = [f"{idx + 1}. {sentence}" for idx, sentence in enumerate(sentences)]
final_html = "<br><br>".join(formatted_sentences)
return f'''
<div style="border: solid 1px #ccc; padding: 16px; background-color: #FFFFFF; color: #374151;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
<div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
</div>
'''