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'''

{title}

{final_html}
''' import re def highlight_common_words_dict(common_words, selected_sentences, discarded_sentences, title): color_map = {} color_index = 0 highlighted_html = [] def highlight_sentences(sentences, start_idx, section_title): nonlocal color_index nonlocal color_map highlighted_sentences = [f'

{section_title}

'] for idx, (sentence, score) in enumerate(sentences.items(), start=start_idx): 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_sentences.append( f'
' f'{highlighted_sentence}' f'
' f'Entailment Score: {score}
' ) return highlighted_sentences selected_html = highlight_sentences(selected_sentences, 1, "Selected Sentences") discarded_html = highlight_sentences(discarded_sentences, 1, "Discarded Sentences") final_html = "
".join(selected_html + discarded_html) return f'''

{title}

{final_html}
'''