import re
def highlight_common_words(common_words, sentences, title):
color_map = {}
color_index = 0
highlighted_html = []
for idx, sentence in enumerate(sentences, start=1):
sentence_with_idx = f"{idx}. {sentence}"
highlighted_sentence = sentence_with_idx
for index, word in common_words:
if word not in color_map:
color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
color_index += 1
escaped_word = re.escape(word)
pattern = rf'\b{escaped_word}\b'
highlighted_sentence = re.sub(
pattern,
lambda m, idx=index, color=color_map[word]: (
f''
f'{idx}'
f'{m.group(0)}'
f''
),
highlighted_sentence,
flags=re.IGNORECASE
)
highlighted_html.append(highlighted_sentence)
final_html = "
".join(highlighted_html)
return f'''
'''
import re
# def highlight_common_words_dict(common_words, sentences, title):
# color_map = {}
# color_index = 0
# highlighted_html = []
# # Ensure indices in common_words are integers
# sanitized_common_words = [(int(index), word) for index, word in common_words]
# for idx, (sentence, score) in enumerate(sentences.items(), start=1):
# sentence_with_idx = f"{idx}. {sentence}"
# highlighted_sentence = sentence_with_idx
# for index, word in sanitized_common_words:
# if word not in color_map:
# color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
# color_index += 1
# escaped_word = re.escape(word)
# pattern = rf'\b{escaped_word}\b'
# highlighted_sentence = re.sub(
# pattern,
# lambda m, idx=index, color=color_map[word]: (
# f''
# f'{idx}'
# f'{m.group(0)}'
# f''
# ),
# highlighted_sentence,
# flags=re.IGNORECASE
# )
# highlighted_html.append(
# f''
# f'{highlighted_sentence}'
# f'
'
# f'Entailment Score: {score}
'
# )
# final_html = "
".join(highlighted_html)
# return f'''
#
#
{title}
#
{final_html}
#
# '''
def highlight_common_words_dict(common_words, sentences, title):
color_map = {}
color_index = 0
highlighted_html = []
for idx, (sentence, score) in enumerate(sentences.items(), start=1):
sentence_with_idx = f"{idx}. {sentence}"
highlighted_sentence = sentence_with_idx
for index, word in common_words:
if word not in color_map:
color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
color_index += 1
escaped_word = re.escape(word)
pattern = rf'\b{escaped_word}\b'
highlighted_sentence = re.sub(
pattern,
lambda m, idx=index, color=color_map[word]: (
f''
f'{idx}'
f'{m.group(0)}'
f''
),
highlighted_sentence,
flags=re.IGNORECASE
)
# Append the sentence and entailment score to the HTML
highlighted_html.append(
f''
f'{highlighted_sentence}'
f'
'
f'Entailment Score: {score}
'
)
final_html = "
".join(highlighted_html)
return f'''
'''
def reparaphrased_sentences_html(sentences):
formatted_sentences = []
for idx, sentence in enumerate(sentences, start=1):
# Add index to each sentence
sentence_with_idx = f"{idx}. {sentence}"
formatted_sentences.append(sentence_with_idx)
final_html = "
".join(formatted_sentences)
return f'''
'''
# common_words = [(1, "highlight"), (2, "numbering")]
# sentences = ["This is a test to highlight words.", "Numbering is important for clarity."]
# # Test highlight_common_words
# highlighted_html = highlight_common_words(common_words, sentences, "Test Highlighting")
# print(highlighted_html)
# # Test highlight_common_words_dict
# sentences_with_scores = {"Highlight words in this text.": 0.95, "Number sentences for clarity.": 0.8}
# highlighted_html_dict = highlight_common_words_dict(common_words, sentences_with_scores, "Test Dict Highlighting")
# print(highlighted_html_dict)