Hakyung Sung commited on
Commit
08b406d
·
1 Parent(s): 24a7c39

Correct app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -30,6 +30,7 @@ def get_highlighted_text(doc):
30
  """
31
  Wraps detected ASCs in each sentence with a span tag containing an inline style.
32
  The inline style sets a custom background color based on the tag.
 
33
  """
34
  highlighted_sentences = []
35
  for sent in doc.sents:
@@ -37,17 +38,19 @@ def get_highlighted_text(doc):
37
  # Find all entities fully contained within this sentence.
38
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
39
  if ents_in_sent:
40
- # Process entities in reverse order so the character offsets remain valid.
41
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
42
  for ent in ents_in_sent:
43
  ent_start = ent.start_char - sent.start_char
44
  ent_end = ent.end_char - sent.start_char
45
  # Get the color from our scheme; use default if not defined.
46
  color = color_scheme.get(ent.label_, "#cccccc")
 
47
  s = (
48
  s[:ent_start]
49
  + f'<span style="background-color: {color}; font-weight: bold;" title="{ent.label_}">'
50
  + s[ent_start:ent_end]
 
51
  + '</span>'
52
  + s[ent_end:]
53
  )
@@ -79,39 +82,28 @@ sample_text = (
79
  "But this wasn't just any baker—Oliver had a magical secret. Every loaf of bread he baked had the power to grant a wish, "
80
  "but only if it was made with true joy in his heart. The villagers adored Oliver’s bread, though they had no idea about its magic. "
81
  "They just knew it tasted unlike anything else—warm, soft, and with a hint of something... extraordinary. Only a few people had ever discovered "
82
- "its secret, and even fewer had made wishes, because the key was in asking for something truly selfless.\n\n"
83
-
84
  "One crisp autumn morning, a traveling musician named Lyla passed through the village. She had a worn-out lute, tattered clothes, "
85
- "and a bright spark of hope in her eyes despite her hardships. Drawn by the smell of freshly baked bread, she wandered into Oliver’s bakery.\n\n"
86
-
87
- "Oliver, busy dusting flour off his hands, smiled warmly. “What can I do for you?” he asked.\n\n"
88
-
89
  "Lyla’s stomach growled in response, and she laughed, a sound as melodic as her lute playing. “Just one loaf of bread, please,” she said, "
90
- "placing a single coin on the counter, all she had left.\n\n"
91
-
92
  "Oliver, feeling the joy in the simple act of serving someone in need, baked the loaf with care. As the bread came out of the oven, a golden glow "
93
- "shimmered around it for just a moment. Lyla didn’t notice, but Oliver did, and he smiled to himself.\n\n"
94
-
95
  "As Lyla took her first bite outside the shop, she felt warmth spread through her whole being. She thought of how she missed playing her lute to "
96
  "cheer others up, but it was broken and beyond repair. Without knowing the magic at work, she whispered under her breath, 'I just wish I could bring "
97
- "music to everyone again.'\n\n"
98
-
99
  "That evening, as Lyla set up by the village fountain to play, something miraculous happened. Her old, broken lute began to transform, glowing just "
100
  "like the bread had. In her hands now was the most beautiful lute she had ever seen, shimmering with gold and silver strings. When she strummed it, "
101
- "the music that flowed out wasn’t just sound—it was pure joy.\n\n"
102
-
103
  "The village gathered around her, drawn by the enchanting melodies that seemed to fill their hearts with warmth and happiness. People laughed, danced, "
104
- "and even sang along, forgetting their worries.\n\n"
105
-
106
  "Oliver watched from his bakery window, smiling to himself. He had no idea what Lyla had wished for, but he could see its magic at work. He knew his "
107
- "bread had once again brought a little bit of magic to the world.\n\n"
108
-
109
  "And so, every day after that, Lyla would play her magical lute in the village square, bringing joy to everyone who heard her music. People came from "
110
- "distant lands just to hear her play, and with every note, the village grew a little brighter, a little happier.\n\n"
111
-
112
  "As for Oliver, he continued to bake with joy in his heart, knowing that sometimes the smallest acts of kindness—like baking a loaf of bread—could "
113
- "make the biggest difference in someone’s life.\n\n"
114
-
115
  "And so, the village became known not just for its delicious bread, but for its music, magic, and the happiness that lingered in every corner. The end. "
116
  "(Generated by ChatGPT 4o 🤖)"
117
  )
 
30
  """
31
  Wraps detected ASCs in each sentence with a span tag containing an inline style.
32
  The inline style sets a custom background color based on the tag.
33
+ Additionally, the entity's tag name is appended as a small superscript to make it always visible.
34
  """
35
  highlighted_sentences = []
36
  for sent in doc.sents:
 
38
  # Find all entities fully contained within this sentence.
39
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
40
  if ents_in_sent:
41
+ # Process entities in reverse order so character offsets remain valid.
42
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
43
  for ent in ents_in_sent:
44
  ent_start = ent.start_char - sent.start_char
45
  ent_end = ent.end_char - sent.start_char
46
  # Get the color from our scheme; use default if not defined.
47
  color = color_scheme.get(ent.label_, "#cccccc")
48
+ # Wrap the entity with inline style and append its label as a superscript.
49
  s = (
50
  s[:ent_start]
51
  + f'<span style="background-color: {color}; font-weight: bold;" title="{ent.label_}">'
52
  + s[ent_start:ent_end]
53
+ + f'<sup style="font-size:0.6em; margin-left:2px;">{ent.label_}</sup>'
54
  + '</span>'
55
  + s[ent_end:]
56
  )
 
82
  "But this wasn't just any baker—Oliver had a magical secret. Every loaf of bread he baked had the power to grant a wish, "
83
  "but only if it was made with true joy in his heart. The villagers adored Oliver’s bread, though they had no idea about its magic. "
84
  "They just knew it tasted unlike anything else—warm, soft, and with a hint of something... extraordinary. Only a few people had ever discovered "
85
+ "its secret, and even fewer had made wishes, because the key was in asking for something truly selfless. "
 
86
  "One crisp autumn morning, a traveling musician named Lyla passed through the village. She had a worn-out lute, tattered clothes, "
87
+ "and a bright spark of hope in her eyes despite her hardships. Drawn by the smell of freshly baked bread, she wandered into Oliver’s bakery. "
88
+ "Oliver, busy dusting flour off his hands, smiled warmly. “What can I do for you?” he asked. "
 
 
89
  "Lyla’s stomach growled in response, and she laughed, a sound as melodic as her lute playing. “Just one loaf of bread, please,” she said, "
90
+ "placing a single coin on the counter, all she had left. "
 
91
  "Oliver, feeling the joy in the simple act of serving someone in need, baked the loaf with care. As the bread came out of the oven, a golden glow "
92
+ "shimmered around it for just a moment. Lyla didn’t notice, but Oliver did, and he smiled to himself. "
 
93
  "As Lyla took her first bite outside the shop, she felt warmth spread through her whole being. She thought of how she missed playing her lute to "
94
  "cheer others up, but it was broken and beyond repair. Without knowing the magic at work, she whispered under her breath, 'I just wish I could bring "
95
+ "music to everyone again.' "
 
96
  "That evening, as Lyla set up by the village fountain to play, something miraculous happened. Her old, broken lute began to transform, glowing just "
97
  "like the bread had. In her hands now was the most beautiful lute she had ever seen, shimmering with gold and silver strings. When she strummed it, "
98
+ "the music that flowed out wasn’t just sound—it was pure joy."
 
99
  "The village gathered around her, drawn by the enchanting melodies that seemed to fill their hearts with warmth and happiness. People laughed, danced, "
100
+ "and even sang along, forgetting their worries. "
 
101
  "Oliver watched from his bakery window, smiling to himself. He had no idea what Lyla had wished for, but he could see its magic at work. He knew his "
102
+ "bread had once again brought a little bit of magic to the world. "
 
103
  "And so, every day after that, Lyla would play her magical lute in the village square, bringing joy to everyone who heard her music. People came from "
104
+ "distant lands just to hear her play, and with every note, the village grew a little brighter, a little happier. "
 
105
  "As for Oliver, he continued to bake with joy in his heart, knowing that sometimes the smallest acts of kindness—like baking a loaf of bread—could "
106
+ "make the biggest difference in someone’s life. "
 
107
  "And so, the village became known not just for its delicious bread, but for its music, magic, and the happiness that lingered in every corner. The end. "
108
  "(Generated by ChatGPT 4o 🤖)"
109
  )