Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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 |
|