Spaces:
Runtime error
Runtime error
File size: 7,233 Bytes
7baf701 1d70c91 7baf701 84105f5 1d70c91 84105f5 7baf701 84105f5 836fa19 ee305a4 84105f5 38c3a0a ee305a4 84105f5 ee305a4 84105f5 ee305a4 38c3a0a ee305a4 38c3a0a ee305a4 84105f5 38c3a0a ee305a4 38c3a0a ee305a4 84105f5 38c3a0a 84105f5 38c3a0a ee305a4 84105f5 436c4c1 0840f0a 38c3a0a 436c4c1 0840f0a 38c3a0a 0840f0a 38c3a0a 0840f0a 38c3a0a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
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'<span style="background-color: {color}; font-weight: bold;'
f' padding: 2px 4px; border-radius: 2px; position: relative;">'
f'<span style="background-color: black; color: white; border-radius: 50%;'
f' padding: 2px 5px; margin-right: 5px;">{idx}</span>'
f'{m.group(0)}'
f'</span>'
),
highlighted_sentence,
flags=re.IGNORECASE
)
highlighted_html.append(highlighted_sentence)
final_html = "<br><br>".join(highlighted_html)
return f'''
<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;">
<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>
'''
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'<span style="background-color: {color}; font-weight: bold;'
# f' padding: 1px 2px; border-radius: 2px; position: relative;">'
# f'<span style="background-color: black; color: white; border-radius: 50%;'
# f' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{idx}</span>'
# f'{m.group(0)}'
# f'</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;">'
# f'Entailment Score: {score}</div></div>'
# )
# final_html = "<br>".join(highlighted_html)
# return f'''
# <div style="background-color: #ffffff; color: #374151;">
# <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 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'<span style="background-color: {color}; font-weight: bold;'
f' padding: 2px 4px; border-radius: 2px; position: relative;">'
f'<span style="background-color: black; color: white; border-radius: 50%;'
f' padding: 2px 5px; margin-right: 5px;">{idx}</span>'
f'{m.group(0)}'
f'</span>'
),
highlighted_sentence,
flags=re.IGNORECASE
)
# Append the sentence and entailment score to the HTML
highlighted_html.append(
f'<div style="margin-bottom: 5px;">'
f'{highlighted_sentence}'
f'<div style="display: inline-block; margin-left: 10px; padding: 3px 8px; border-radius: 5px; background-color: #E5E7EB; font-size: 0.9em; color: #374151;">'
f'Entailment Score: {score}</div></div>'
)
final_html = "<br><br>".join(highlighted_html)
return f'''
<div style="border: solid 1px #E5E7EB; 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 = []
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 = "<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>
'''
# 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) |