Nucha commited on
Commit
5481c65
·
verified ·
1 Parent(s): da74fac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -58,21 +58,26 @@ def categorize_entities(entities):
58
 
59
  return hskill, sskill
60
 
61
- # ฟังก์ชันสร้าง HTML สำหรับแสดงผล Entity ที่ถูก Highlight พร้อม Tag
62
  def highlight_entities(text, hskill, sskill):
63
- all_entities = hskill + sskill # รวมทั้งหมดเพื่อไฮไลต์
64
- highlighted_text = text
65
-
66
- for entity in sorted(all_entities, key=lambda e: text.find(e["word"]), reverse=True):
67
- entity_word = entity["word"]
68
- entity_label = "HSKILL" if entity in hskill else "SSKILL"
69
- entity_color = ENTITY_COLORS.get(entity_label, "#D3D3D3")
70
-
71
- # ใช้ HTML + CSS ในการไฮไลต์ Entity และใส่ TAG
72
- highlighted_text = highlighted_text.replace(
73
- entity_word,
74
- f'<span style="background-color: {entity_color}; padding: 2px 5px; border-radius: 5px;">[{entity_word}] ({entity_label.upper()})</span>'
75
- )
 
 
 
 
 
 
76
 
77
  return f'<div style="font-size:16px; line-height:1.6;">{highlighted_text}</div>'
78
 
 
58
 
59
  return hskill, sskill
60
 
 
61
  def highlight_entities(text, hskill, sskill):
62
+ all_entities = sorted(hskill + sskill, key=lambda e: e["start"]) # เรียงตามตำแหน่งเริ่มต้น
63
+
64
+ last_idx = 0
65
+ highlighted_text = ""
66
+
67
+ for entity in all_entities:
68
+ start, end, word, entity_type = entity["start"], entity["end"], entity["word"], entity["entity"]
69
+ color = ENTITY_COLORS["hskill"] if entity in hskill else ENTITY_COLORS["sskill"]
70
+
71
+ # เพิ่มข้อความก่อน Entity
72
+ highlighted_text += text[last_idx:start]
73
+
74
+ # เพิ่ม Entity แบบมีไฮไลต์
75
+ highlighted_text += f'<span style="background-color: {color}; padding: 2px 5px; border-radius: 5px;">[{word}] ({entity_type.upper()})</span>'
76
+
77
+ last_idx = end # อัปเดตตำแหน่งล่าสุด
78
+
79
+ # เพิ่มข้อความที่เหลือหลังจาก Entity สุดท้าย
80
+ highlighted_text += text[last_idx:]
81
 
82
  return f'<div style="font-size:16px; line-height:1.6;">{highlighted_text}</div>'
83