File size: 22,150 Bytes
3c3b760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f100a963",
   "metadata": {},
   "source": [
    "# Test Post-processing: PREFIX & TIME/DATE trong ngoặc []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94f79399",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "^C\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting gradio\n",
      "  Downloading gradio-6.14.0-py3-none-any.whl.metadata (17 kB)\n",
      "Requirement already satisfied: anyio<5.0,>=3.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (4.13.0)\n",
      "Collecting brotli>=1.1.0 (from gradio)\n",
      "  Downloading brotli-1.2.0-cp311-cp311-win_amd64.whl.metadata (6.3 kB)\n",
      "Collecting fastapi<1.0,>=0.115.2 (from gradio)\n",
      "  Downloading fastapi-0.136.1-py3-none-any.whl.metadata (28 kB)\n",
      "Collecting gradio-client==2.5.0 (from gradio)\n",
      "  Downloading gradio_client-2.5.0-py3-none-any.whl.metadata (7.1 kB)\n",
      "Collecting groovy~=0.1 (from gradio)\n",
      "  Downloading groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n",
      "Collecting hf-gradio<1.0,>=0.4.1 (from gradio)\n",
      "  Downloading hf_gradio-0.4.1-py3-none-any.whl.metadata (428 bytes)\n",
      "Requirement already satisfied: httpx<1.0,>=0.24.1 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (0.28.1)\n",
      "Requirement already satisfied: huggingface-hub<2.0,>=0.33.5 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (1.12.0)\n",
      "Requirement already satisfied: jinja2<4.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (3.1.6)\n",
      "Requirement already satisfied: markupsafe<4.0,>=2.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (3.0.3)\n",
      "Requirement already satisfied: numpy<3.0,>=1.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (2.4.4)\n",
      "Collecting orjson~=3.0 (from gradio)\n",
      "  Downloading orjson-3.11.9-cp311-cp311-win_amd64.whl.metadata (43 kB)\n",
      "Requirement already satisfied: packaging in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (26.0)\n",
      "Collecting pandas<4.0,>=1.0 (from gradio)\n",
      "  Using cached pandas-3.0.2-cp311-cp311-win_amd64.whl.metadata (19 kB)\n",
      "Collecting pillow<13.0,>=8.0 (from gradio)\n",
      "  Using cached pillow-12.2.0-cp311-cp311-win_amd64.whl.metadata (9.0 kB)\n",
      "Requirement already satisfied: pydantic<=3.0,>=2.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (2.13.3)\n",
      "Collecting pydub<1.0 (from gradio)\n",
      "  Using cached pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
      "Collecting python-multipart<1.0,>=0.0.18 (from gradio)\n",
      "  Downloading python_multipart-0.0.27-py3-none-any.whl.metadata (2.1 kB)\n",
      "Collecting pytz>=2017.2 (from gradio)\n",
      "  Downloading pytz-2026.2-py2.py3-none-any.whl.metadata (22 kB)\n",
      "Requirement already satisfied: pyyaml<7.0,>=5.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (6.0.3)\n",
      "Collecting safehttpx<0.2.0,>=0.1.7 (from gradio)\n",
      "  Downloading safehttpx-0.1.7-py3-none-any.whl.metadata (4.2 kB)\n",
      "Collecting semantic-version~=2.0 (from gradio)\n",
      "  Using cached semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
      "Collecting starlette<2.0,>=0.40.0 (from gradio)\n",
      "  Downloading starlette-1.0.0-py3-none-any.whl.metadata (6.3 kB)\n",
      "Collecting tomlkit<0.15.0,>=0.12.0 (from gradio)\n",
      "  Using cached tomlkit-0.14.0-py3-none-any.whl.metadata (2.8 kB)\n",
      "Requirement already satisfied: typer<1.0,>=0.12 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio) (0.25.0)\n",
      "Requirement already satisfied: typing-extensions~=4.0 in C:\\Users\\ADMIN\\AppData\\Roaming\\Python\\Python311\\site-packages (from gradio) (4.15.0)\n",
      "Collecting uvicorn>=0.14.0 (from gradio)\n",
      "  Downloading uvicorn-0.46.0-py3-none-any.whl.metadata (6.7 kB)\n",
      "Requirement already satisfied: fsspec in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from gradio-client==2.5.0->gradio) (2026.3.0)\n",
      "Requirement already satisfied: idna>=2.8 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from anyio<5.0,>=3.0->gradio) (3.13)\n",
      "Requirement already satisfied: typing-inspection>=0.4.2 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from fastapi<1.0,>=0.115.2->gradio) (0.4.2)\n",
      "Requirement already satisfied: annotated-doc>=0.0.2 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from fastapi<1.0,>=0.115.2->gradio) (0.0.4)\n",
      "Requirement already satisfied: certifi in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from httpx<1.0,>=0.24.1->gradio) (2026.4.22)\n",
      "Requirement already satisfied: httpcore==1.* in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from httpx<1.0,>=0.24.1->gradio) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from httpcore==1.*->httpx<1.0,>=0.24.1->gradio) (0.16.0)\n",
      "Requirement already satisfied: filelock>=3.10.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (3.29.0)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.4.3 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (1.4.3)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (4.67.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in C:\\Users\\ADMIN\\AppData\\Roaming\\Python\\Python311\\site-packages (from pandas<4.0,>=1.0->gradio) (2.9.0.post0)\n",
      "Collecting tzdata (from pandas<4.0,>=1.0->gradio)\n",
      "  Downloading tzdata-2026.2-py2.py3-none-any.whl.metadata (1.4 kB)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from pydantic<=3.0,>=2.0->gradio) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.46.3 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from pydantic<=3.0,>=2.0->gradio) (2.46.3)\n",
      "Requirement already satisfied: click>=8.2.1 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from typer<1.0,>=0.12->gradio) (8.3.3)\n",
      "Requirement already satisfied: shellingham>=1.3.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
      "Requirement already satisfied: rich>=13.8.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from typer<1.0,>=0.12->gradio) (15.0.0)\n",
      "Requirement already satisfied: colorama in C:\\Users\\ADMIN\\AppData\\Roaming\\Python\\Python311\\site-packages (from click>=8.2.1->typer<1.0,>=0.12->gradio) (0.4.6)\n",
      "Requirement already satisfied: six>=1.5 in C:\\Users\\ADMIN\\AppData\\Roaming\\Python\\Python311\\site-packages (from python-dateutil>=2.8.2->pandas<4.0,>=1.0->gradio) (1.17.0)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from rich>=13.8.0->typer<1.0,>=0.12->gradio) (4.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in C:\\Users\\ADMIN\\AppData\\Roaming\\Python\\Python311\\site-packages (from rich>=13.8.0->typer<1.0,>=0.12->gradio) (2.19.2)\n",
      "Requirement already satisfied: mdurl~=0.1 in C:\\Users\\ADMIN\\miniconda3\\envs\\quantize\\Lib\\site-packages (from markdown-it-py>=2.2.0->rich>=13.8.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
      "Downloading gradio-6.14.0-py3-none-any.whl (19.7 MB)\n",
      "   ---------------------------------------- 0.0/19.7 MB ? eta -:--:--\n",
      "   -- ------------------------------------- 1.0/19.7 MB 12.7 MB/s eta 0:00:02\n",
      "   ------------- -------------------------- 6.6/19.7 MB 21.2 MB/s eta 0:00:01\n",
      "   --------------------- ------------------ 10.5/19.7 MB 19.8 MB/s eta 0:00:01\n",
      "   --------------------------------- ------ 16.5/19.7 MB 22.6 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 19.7/19.7 MB 21.1 MB/s  0:00:01\n",
      "Downloading gradio_client-2.5.0-py3-none-any.whl (59 kB)\n",
      "Downloading fastapi-0.136.1-py3-none-any.whl (117 kB)\n",
      "Downloading groovy-0.1.2-py3-none-any.whl (14 kB)\n",
      "Downloading hf_gradio-0.4.1-py3-none-any.whl (4.5 kB)\n",
      "Downloading orjson-3.11.9-cp311-cp311-win_amd64.whl (127 kB)\n",
      "Using cached pandas-3.0.2-cp311-cp311-win_amd64.whl (9.9 MB)\n",
      "Using cached pillow-12.2.0-cp311-cp311-win_amd64.whl (7.1 MB)\n",
      "Using cached pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Downloading python_multipart-0.0.27-py3-none-any.whl (29 kB)\n",
      "Downloading safehttpx-0.1.7-py3-none-any.whl (9.0 kB)\n",
      "Using cached semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
      "Downloading starlette-1.0.0-py3-none-any.whl (72 kB)\n",
      "Using cached tomlkit-0.14.0-py3-none-any.whl (39 kB)\n",
      "Downloading brotli-1.2.0-cp311-cp311-win_amd64.whl (369 kB)\n",
      "Downloading pytz-2026.2-py2.py3-none-any.whl (510 kB)\n",
      "Downloading uvicorn-0.46.0-py3-none-any.whl (70 kB)\n",
      "Downloading tzdata-2026.2-py2.py3-none-any.whl (349 kB)\n",
      "Installing collected packages: pytz, pydub, brotli, tzdata, tomlkit, semantic-version, python-multipart, pillow, orjson, groovy, uvicorn, starlette, pandas, safehttpx, fastapi, gradio-client, hf-gradio, gradio\n",
      "\n",
      "   ----------------------------------------  0/18 [pytz]\n",
      "   ---- -----------------------------------  2/18 [brotli]\n",
      "   ------ ---------------------------------  3/18 [tzdata]\n",
      "   -------- -------------------------------  4/18 [tomlkit]\n",
      "   --------------- ------------------------  7/18 [pillow]\n",
      "   --------------- ------------------------  7/18 [pillow]\n",
      "   --------------- ------------------------  7/18 [pillow]\n",
      "   ------------------------ --------------- 11/18 [starlette]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   -------------------------- ------------- 12/18 [pandas]\n",
      "   ---------------------------- ----------- 13/18 [safehttpx]\n",
      "   --------------------------------- ------ 15/18 [gradio-client]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ------------------------------------- -- 17/18 [gradio]\n",
      "   ---------------------------------------- 18/18 [gradio]\n",
      "\n",
      "Successfully installed brotli-1.2.0 fastapi-0.136.1 gradio-6.14.0 gradio-client-2.5.0 groovy-0.1.2 hf-gradio-0.4.1 orjson-3.11.9 pandas-3.0.2 pillow-12.2.0 pydub-0.25.1 python-multipart-0.0.27 pytz-2026.2 safehttpx-0.1.7 semantic-version-2.10.0 starlette-1.0.0 tomlkit-0.14.0 tzdata-2026.2 uvicorn-0.46.0\n"
     ]
    }
   ],
   "source": [
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e8be21f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ Import thành công!\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Import trực tiếp các hàm cần thiết từ app.py mà không kéo theo gradio/torch.\n",
    "\"\"\"\n",
    "import re, os, sys, ipaddress, unicodedata\n",
    "from datetime import datetime\n",
    "from typing import Dict, List, Optional, Tuple, Callable, Pattern\n",
    "from dataclasses import dataclass\n",
    "from urllib.parse import urlparse\n",
    "\n",
    "_app_path = r\"d:\\CODE\\DEMO_HF\\GLiNER2-multi\\app.py\"\n",
    "with open(_app_path, encoding=\"utf-8\") as f:\n",
    "    _source = f.read()\n",
    "\n",
    "# Tách phần từ section 2 đến hết section 2d (trước section 3 - Model Wrappers)\n",
    "_cut = _source.find(\"class _BaseWrapper\")\n",
    "_pure_source = _source[_source.find(\"# ======\"):_cut] if _cut > 0 else \"\"\n",
    "\n",
    "# Bỏ dòng gán WhitespaceTokenSplitter (cần gliner2)\n",
    "_pure_source = \"\\n\".join(\n",
    "    line for line in _pure_source.splitlines()\n",
    "    if \"WhitespaceTokenSplitter\" not in line\n",
    ")\n",
    "\n",
    "_ns = {\n",
    "    \"re\": re, \"os\": os, \"ipaddress\": ipaddress, \"unicodedata\": unicodedata,\n",
    "    \"datetime\": datetime, \"Dict\": Dict, \"List\": List, \"Optional\": Optional,\n",
    "    \"Tuple\": Tuple, \"Callable\": Callable, \"Pattern\": Pattern,\n",
    "    \"dataclass\": dataclass, \"urlparse\": urlparse,\n",
    "    \"defaultdict\": __import__(\"collections\").defaultdict,\n",
    "    \"frozenset\": frozenset,\n",
    "}\n",
    "exec(_pure_source, _ns)\n",
    "\n",
    "apply_advanced_postprocessing = _ns[\"apply_advanced_postprocessing\"]\n",
    "detect_by_rules = _ns[\"detect_by_rules\"]\n",
    "resolve_span_conflicts = _ns[\"resolve_span_conflicts\"]\n",
    "clean_text_with_mapping = _ns[\"clean_text_with_mapping\"]\n",
    "normalize_predictions = _ns[\"normalize_predictions\"]\n",
    "LABELS = _ns[\"LABELS\"]\n",
    "RULE_BASED_LABELS = _ns[\"RULE_BASED_LABELS\"]\n",
    "\n",
    "print(\"✅ Import thành công!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1e398d1",
   "metadata": {},
   "source": [
    "## Câu 1: Test PREFIX — \"Đại diện Công ty TNHH Giải pháp AI là ông Lê Văn Á\"\n",
    "PREFIX `ông` phải được giữ lại vì theo sau bởi PERSONAL_NAME `Lê Văn Á`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "16631f6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gap giữa PREFIX và PERSONAL_NAME: 1 ký tự\n",
      "  text[38:41] = 'ông' → label: PREFIX\n",
      "  text[42:50] = 'Lê Văn Á' → label: PERSONAL_NAME\n",
      "\n",
      "--- Trước postprocessing ---\n",
      "  [PREFIX] 'ông'\n",
      "  [PERSONAL_NAME] 'Lê Văn Á'\n",
      "\n",
      "--- Sau postprocessing ---\n",
      "  [PREFIX] 'ông'\n",
      "  [PERSONAL_NAME] 'Lê Văn Á'\n",
      "\n",
      "✅ PREFIX 'ông' được giữ lại (có PERSONAL_NAME theo sau)\n"
     ]
    }
   ],
   "source": [
    "text1 = \"Đại diện Công ty TNHH Giải pháp AI là ông Lê Văn Á\"\n",
    "\n",
    "# Tính vị trí chính xác — \"ông\" standalone (không phải trong \"Công\")\n",
    "s_prefix = text1.index(\" ông \") + 1  # bỏ space đầu\n",
    "e_prefix = s_prefix + len(\"ông\")\n",
    "s_name = text1.index(\"Lê Văn Á\")\n",
    "e_name = s_name + len(\"Lê Văn Á\")\n",
    "\n",
    "print(f\"Gap giữa PREFIX và PERSONAL_NAME: {s_name - e_prefix} ký tự\")\n",
    "\n",
    "entities1 = [\n",
    "    {\"text\": \"ông\", \"label\": \"PREFIX\", \"start\": s_prefix, \"end\": e_prefix, \"score\": 0.85},\n",
    "    {\"text\": \"Lê Văn Á\", \"label\": \"PERSONAL_NAME\", \"start\": s_name, \"end\": e_name, \"score\": 0.95},\n",
    "]\n",
    "\n",
    "for e in entities1:\n",
    "    print(f\"  text[{e['start']}:{e['end']}] = '{text1[e['start']:e['end']]}' → label: {e['label']}\")\n",
    "\n",
    "print(\"\\n--- Trước postprocessing ---\")\n",
    "for e in entities1:\n",
    "    print(f\"  [{e['label']}] '{e['text']}'\")\n",
    "\n",
    "result1 = apply_advanced_postprocessing(entities1, text1)\n",
    "\n",
    "print(\"\\n--- Sau postprocessing ---\")\n",
    "for e in result1:\n",
    "    print(f\"  [{e['label']}] '{e['text']}'\")\n",
    "\n",
    "assert len(result1) == 2, f\"Expected 2, got {len(result1)}\"\n",
    "assert result1[0][\"label\"] == \"PREFIX\"\n",
    "print(\"\\n✅ PREFIX 'ông' được giữ lại (có PERSONAL_NAME theo sau)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "869d4a22",
   "metadata": {},
   "source": [
    "## Câu 2: Test TIME trong ngoặc [] — \"[9:40]: tôi muốn đổi số tài khoản thành 39752987599\"\n",
    "TIME `9:40` phải bị loại bỏ vì nằm trong `[...]`. ACCOUNT_NUMBER `39752987599` phải được giữ lại."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ba4f8b4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  text[1:5] = '9:40' (context: '[9:40]') → TIME\n",
      "  text[40:51] = '39752987599' (context: ' 39752987599') → ACCOUNT_NUMBER\n",
      "\n",
      "--- Trước postprocessing ---\n",
      "  [TIME] '9:40'\n",
      "  [ACCOUNT_NUMBER] '39752987599'\n",
      "\n",
      "--- Sau postprocessing ---\n",
      "  [ACCOUNT_NUMBER] '39752987599'\n",
      "\n",
      "✅ TIME '9:40' bị loại bỏ (nằm trong [...])\n",
      "✅ ACCOUNT_NUMBER '39752987599' được giữ lại\n"
     ]
    }
   ],
   "source": [
    "text2 = \"[9:40]: tôi muốn đổi số tài khoản thành 39752987599\"\n",
    "\n",
    "s_time = text2.index(\"9:40\")\n",
    "e_time = s_time + len(\"9:40\")\n",
    "s_acc = text2.index(\"39752987599\")\n",
    "e_acc = s_acc + len(\"39752987599\")\n",
    "\n",
    "entities2 = [\n",
    "    {\"text\": \"9:40\", \"label\": \"TIME\", \"start\": s_time, \"end\": e_time, \"score\": 0.9},\n",
    "    {\"text\": \"39752987599\", \"label\": \"ACCOUNT_NUMBER\", \"start\": s_acc, \"end\": e_acc, \"score\": 0.88},\n",
    "]\n",
    "\n",
    "for e in entities2:\n",
    "    ctx_start = max(0, e[\"start\"] - 1)\n",
    "    ctx_end = min(len(text2), e[\"end\"] + 1)\n",
    "    print(f\"  text[{e['start']}:{e['end']}] = '{text2[e['start']:e['end']]}' (context: '{text2[ctx_start:ctx_end]}') → {e['label']}\")\n",
    "\n",
    "print(\"\\n--- Trước postprocessing ---\")\n",
    "for e in entities2:\n",
    "    print(f\"  [{e['label']}] '{e['text']}'\")\n",
    "\n",
    "result2 = apply_advanced_postprocessing(entities2, text2)\n",
    "\n",
    "print(\"\\n--- Sau postprocessing ---\")\n",
    "for e in result2:\n",
    "    print(f\"  [{e['label']}] '{e['text']}'\")\n",
    "\n",
    "assert len(result2) == 1, f\"Expected 1, got {len(result2)}\"\n",
    "assert result2[0][\"label\"] == \"ACCOUNT_NUMBER\"\n",
    "print(\"\\n✅ TIME '9:40' bị loại bỏ (nằm trong [...])\")\n",
    "print(\"✅ ACCOUNT_NUMBER '39752987599' được giữ lại\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "quantize",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}