Pijush2023 commited on
Commit
f5c8bff
1 Parent(s): 99813dd

Update highlighter.py

Browse files
Files changed (1) hide show
  1. highlighter.py +38 -41
highlighter.py CHANGED
@@ -1,107 +1,104 @@
1
  import re
2
 
3
- def generate_color(index):
4
- """Generate a unique color using HSL."""
5
- return f'hsl({index * 60 % 360}, 70%, 80%)'
6
-
7
-
8
  def highlight_common_words(common_words, sentences, title):
9
- """Highlight common words in sentences and generate an HTML block."""
10
  color_map = {}
11
  color_index = 0
12
  highlighted_html = []
13
-
14
  for idx, sentence in enumerate(sentences, start=1):
15
  sentence_with_idx = f"{idx}. {sentence}"
16
  highlighted_sentence = sentence_with_idx
17
-
18
  for index, word in common_words:
19
  if word not in color_map:
20
- color_map[word] = generate_color(color_index)
21
  color_index += 1
22
-
23
- # Highlight word with the assigned color and index
24
- pattern = rf'\b{re.escape(word)}\b'
25
  highlighted_sentence = re.sub(
26
  pattern,
27
  lambda m, idx=index, color=color_map[word]: (
28
  f'<span style="background-color: {color}; font-weight: bold;'
29
- f' padding: 2px 4px; border-radius: 2px;">'
30
  f'<span style="background-color: black; color: white; border-radius: 50%;'
31
  f' padding: 2px 5px; margin-right: 5px;">{idx}</span>'
32
- f'{m.group(0)}</span>'
 
33
  ),
34
  highlighted_sentence,
35
  flags=re.IGNORECASE
36
  )
37
  highlighted_html.append(highlighted_sentence)
38
-
39
  final_html = "<br><br>".join(highlighted_html)
40
  return f'''
41
- <div style="border: solid 1px #ccc; padding: 16px; background-color: #FFFFFF; color: #374151;
42
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
43
- <h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
44
- <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
45
  </div>
46
  '''
47
 
48
 
 
 
 
49
  def highlight_common_words_dict(common_words, sentences, title):
50
- """Highlight common words in sentences with scores and generate an HTML block."""
51
  color_map = {}
52
  color_index = 0
53
  highlighted_html = []
54
-
55
  for idx, (sentence, score) in enumerate(sentences.items(), start=1):
56
  sentence_with_idx = f"{idx}. {sentence}"
57
  highlighted_sentence = sentence_with_idx
58
-
59
  for index, word in common_words:
60
  if word not in color_map:
61
- color_map[word] = generate_color(color_index)
62
  color_index += 1
63
-
64
- # Highlight word with the assigned color and index
65
- pattern = rf'\b{re.escape(word)}\b'
66
  highlighted_sentence = re.sub(
67
  pattern,
68
  lambda m, idx=index, color=color_map[word]: (
69
  f'<span style="background-color: {color}; font-weight: bold;'
70
- f' padding: 1px 2px; border-radius: 2px;">'
71
  f'<span style="background-color: black; color: white; border-radius: 50%;'
72
  f' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{idx}</span>'
73
- f'{m.group(0)}</span>'
 
74
  ),
75
  highlighted_sentence,
76
  flags=re.IGNORECASE
77
  )
78
-
79
  highlighted_html.append(
80
  f'<div style="margin-bottom: 5px;">'
81
  f'{highlighted_sentence}'
82
- f'<div style="display: inline-block; margin-left: 5px; padding: 3px 5px; border-radius: 3px; background-color: white; font-size: 0.9em;">'
83
  f'Entailment Score: {score}</div></div>'
84
  )
85
-
86
  final_html = "<br>".join(highlighted_html)
87
  return f'''
88
  <div style="background-color: #ffffff; color: #374151;">
89
- <h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
90
- <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
91
  </div>
92
  '''
93
 
94
-
95
- def generate_paraphrased_sentences_html(sentences):
96
- """Generate an HTML block for paraphrased sentences."""
97
- formatted_sentences = [
98
- f"{idx}. {sentence}" for idx, sentence in enumerate(sentences, start=1)
99
- ]
100
-
 
 
101
  final_html = "<br><br>".join(formatted_sentences)
 
102
  return f'''
103
  <div style="border: solid 1px #ccc; padding: 16px; background-color: #FFFFFF; color: #374151;
104
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
105
  <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
106
  </div>
107
- '''
 
1
  import re
2
 
 
 
 
 
 
3
  def highlight_common_words(common_words, sentences, title):
 
4
  color_map = {}
5
  color_index = 0
6
  highlighted_html = []
7
+
8
  for idx, sentence in enumerate(sentences, start=1):
9
  sentence_with_idx = f"{idx}. {sentence}"
10
  highlighted_sentence = sentence_with_idx
11
+
12
  for index, word in common_words:
13
  if word not in color_map:
14
+ color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
15
  color_index += 1
16
+ escaped_word = re.escape(word)
17
+ pattern = rf'\b{escaped_word}\b'
 
18
  highlighted_sentence = re.sub(
19
  pattern,
20
  lambda m, idx=index, color=color_map[word]: (
21
  f'<span style="background-color: {color}; font-weight: bold;'
22
+ f' padding: 2px 4px; border-radius: 2px; position: relative;">'
23
  f'<span style="background-color: black; color: white; border-radius: 50%;'
24
  f' padding: 2px 5px; margin-right: 5px;">{idx}</span>'
25
+ f'{m.group(0)}'
26
+ f'</span>'
27
  ),
28
  highlighted_sentence,
29
  flags=re.IGNORECASE
30
  )
31
  highlighted_html.append(highlighted_sentence)
32
+
33
  final_html = "<br><br>".join(highlighted_html)
34
  return f'''
35
+ <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;">
36
+ <h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
37
+ <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
 
38
  </div>
39
  '''
40
 
41
 
42
+
43
+ import re
44
+
45
  def highlight_common_words_dict(common_words, sentences, title):
 
46
  color_map = {}
47
  color_index = 0
48
  highlighted_html = []
49
+
50
  for idx, (sentence, score) in enumerate(sentences.items(), start=1):
51
  sentence_with_idx = f"{idx}. {sentence}"
52
  highlighted_sentence = sentence_with_idx
53
+
54
  for index, word in common_words:
55
  if word not in color_map:
56
+ color_map[word] = f'hsl({color_index * 60 % 360}, 70%, 80%)'
57
  color_index += 1
58
+ escaped_word = re.escape(word)
59
+ pattern = rf'\b{escaped_word}\b'
 
60
  highlighted_sentence = re.sub(
61
  pattern,
62
  lambda m, idx=index, color=color_map[word]: (
63
  f'<span style="background-color: {color}; font-weight: bold;'
64
+ f' padding: 1px 2px; border-radius: 2px; position: relative;">'
65
  f'<span style="background-color: black; color: white; border-radius: 50%;'
66
  f' padding: 1px 3px; margin-right: 3px; font-size: 0.8em;">{idx}</span>'
67
+ f'{m.group(0)}'
68
+ f'</span>'
69
  ),
70
  highlighted_sentence,
71
  flags=re.IGNORECASE
72
  )
 
73
  highlighted_html.append(
74
  f'<div style="margin-bottom: 5px;">'
75
  f'{highlighted_sentence}'
76
+ f'<div style="display: inline-block; margin-left: 5px; padding: 3px 5px; border-radius: 3px; background-color: white; font-size: 0.9em;">'
77
  f'Entailment Score: {score}</div></div>'
78
  )
79
+
80
  final_html = "<br>".join(highlighted_html)
81
  return f'''
82
  <div style="background-color: #ffffff; color: #374151;">
83
+ <h3 style="margin-top: 0; font-size: 1em; color: #111827;">{title}</h3>
84
+ <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
85
  </div>
86
  '''
87
 
88
+ def reparaphrased_sentences_html(sentences):
89
+
90
+ formatted_sentences = []
91
+
92
+ for idx, sentence in enumerate(sentences, start=1):
93
+ # Add index to each sentence
94
+ sentence_with_idx = f"{idx}. {sentence}"
95
+ formatted_sentences.append(sentence_with_idx)
96
+
97
  final_html = "<br><br>".join(formatted_sentences)
98
+
99
  return f'''
100
  <div style="border: solid 1px #ccc; padding: 16px; background-color: #FFFFFF; color: #374151;
101
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px;">
102
  <div style="background-color: #F5F5F5; line-height: 1.6; padding: 15px; border-radius: 8px;">{final_html}</div>
103
  </div>
104
+ '''