File size: 14,276 Bytes
637a6e3 | 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 | import json
import logging
import torch
from pathlib import Path
from torch.utils.data import Dataset
from typing import Any, Dict, List, Optional
from tokenizer.preprocess import preprocess_thai
log = logging.getLogger(__name__)
# ── NER label map ────────────────────────────────────────────────────────────
# ตรงกับ BEST2020 tag set ใน dataset จริง
NER_LABEL2ID = {
"O": 0,
"B-PERSON": 1, "I-PERSON": 2,
"B-ORGANIZATION": 3, "I-ORGANIZATION": 4,
"B-LOCATION": 5, "I-LOCATION": 6,
}
NER_ID2LABEL = {v: k for k, v in NER_LABEL2ID.items()}
# ── Sentiment label map ───────────────────────────────────────────────────────
SENTIMENT_LABEL2ID = {"neg": 0, "neu": 1, "pos": 2}
SENTIMENT_ID2LABEL = {v: k for k, v in SENTIMENT_LABEL2ID.items()}
# ─────────────────────────────────────────────────────────────────────────────
# NERDataset
# ─────────────────────────────────────────────────────────────────────────────
class NERDataset(Dataset):
"""
BEST2020 NER dataset — JSON Lines format
แต่ละบรรทัด: {"tokens": [...], "ner_tags": [...]}
การ align label กับ subword เป็นจุดสำคัญที่สุดใน NER:
- token "สมชาย" อาจถูก split เป็น ["สม", "ชาย"] (2 subwords)
- label "B-PER" ให้เฉพาะ subword แรก ("สม")
- subword ที่ 2 ("ชาย") ให้ label = -100 (ignore_index)
"""
def __init__(
self,
data_path: str,
tokenizer: Any,
max_length: int = 512,
):
self.tokenizer = tokenizer
self.max_length = max_length
self.examples = self._load(data_path)
def _load(self, path: str) -> List[Dict]:
examples = []
with open(path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
item = json.loads(line)
# validate minimal required fields
if "tokens" in item and "ner_tags" in item:
examples.append(item)
return examples
def _align_labels(
self,
tokens: List[str],
ner_tags: List[str],
) -> Dict:
"""
Tokenize ทีละคำ แล้ว align label กับ subword
Returns dict พร้อม input_ids, attention_mask, labels
"""
input_ids = [self.tokenizer.cls_id] # [CLS] ที่ตำแหน่ง 0
label_ids = [-100] # [CLS] ไม่มี NER label
for token, tag in zip(tokens, ner_tags):
# encode ทีละคำ ไม่ใส่ special tokens
word_ids = self.tokenizer.sp.encode(token, out_type=int)
if not word_ids:
continue
tag_id = NER_LABEL2ID.get(tag, 0) # default O ถ้าไม่รู้จัก tag
# subword แรก → label จริง
input_ids.append(word_ids[0])
label_ids.append(tag_id)
# subword ที่ 2+ → -100 (ignore)
for wid in word_ids[1:]:
input_ids.append(wid)
label_ids.append(-100)
# เพิ่ม [SEP] ท้าย
input_ids.append(self.tokenizer.sep_id)
label_ids.append(-100)
# Truncate
if len(input_ids) > self.max_length:
input_ids = input_ids[:self.max_length - 1] + [self.tokenizer.sep_id]
label_ids = label_ids[:self.max_length - 1] + [-100]
attention_mask = [1] * len(input_ids)
return {
"input_ids": torch.tensor(input_ids, dtype=torch.long),
"attention_mask": torch.tensor(attention_mask, dtype=torch.long),
"labels": torch.tensor(label_ids, dtype=torch.long),
}
def __len__(self) -> int:
return len(self.examples)
def __getitem__(self, idx: int) -> Dict:
item = self.examples[idx]
return self._align_labels(item["tokens"], item["ner_tags"])
# ─────────────────────────────────────────────────────────────────────────────
# SentimentDataset
# ─────────────────────────────────────────────────────────────────────────────
class SentimentDataset(Dataset):
"""
Wisesight Sentiment — TSV format
แต่ละบรรทัด: text\\tlabel (label = pos / neu / neg)
"""
def __init__(
self,
data_path: str,
tokenizer: Any,
max_length: int = 512,
):
self.tokenizer = tokenizer
self.max_length = max_length
self.examples = self._load(data_path)
def _load(self, path: str) -> List[Dict]:
examples = []
with open(path, encoding="utf-8") as f:
for line in f:
parts = line.strip().split("\t")
if len(parts) < 2:
continue
text, label = parts[0], parts[1].strip().lower()
if label not in SENTIMENT_LABEL2ID:
continue
examples.append({"text": text, "label": label})
return examples
def __len__(self) -> int:
return len(self.examples)
def __getitem__(self, idx: int) -> Dict:
item = self.examples[idx]
# encode: [CLS] text [SEP]
encoded = self.tokenizer.batch_encode(
[item["text"]],
max_length=self.max_length,
padding=False, # collator จะ pad ทีหลัง
return_tensors=True,
)
return {
"input_ids": encoded["input_ids"][0],
"attention_mask": encoded["attention_mask"][0],
"labels": torch.tensor(
SENTIMENT_LABEL2ID[item["label"]],
dtype=torch.long
),
}
# ─────────────────────────────────────────────────────────────────────────────
# QADataset
# ─────────────────────────────────────────────────────────────────────────────
class QADataset(Dataset):
"""
iApp Thai QA — SQuAD-style JSON format
{
"question": "...",
"context": "...",
"answers": {"text": ["..."], "answer_start": [42]}
}
จุดสำคัญ: answer_start ใน dataset เป็น character position
ต้องแปลงเป็น token position หลัง encode
"""
def __init__(
self,
data_path: str,
tokenizer: Any,
max_length: int = 512,
):
self.tokenizer = tokenizer
self.max_length = max_length
self.examples = self._load(data_path)
self._log_span_match_rate()
def _log_span_match_rate(self):
import unicodedata
matched = 0
for ex in self.examples:
answers = ex["answers"]
answer_list = answers if isinstance(answers, list) else answers.get("text", [])
if not answer_list:
continue
answer_text = answer_list[0]
encoded = self.tokenizer.encode_qa(ex["question"], ex["context"], self.max_length)
ctx_ids = encoded["input_ids"][encoded["context_start"]:-1]
ctx_text = self.tokenizer.sp.decode(ctx_ids)
answer_clean = preprocess_thai(answer_text)
context_nfkc = unicodedata.normalize("NFKC", ctx_text)
answer_nfkc = unicodedata.normalize("NFKC", answer_clean)
if context_nfkc.find(answer_nfkc) != -1 or context_nfkc.find(unicodedata.normalize("NFKC", answer_text)) != -1:
matched += 1
total = len(self.examples)
rate = 100 * matched / max(total, 1)
log.info(f"QA span match rate: {matched}/{total} ({rate:.1f}%)")
def _load(self, path: str) -> List[Dict]:
with open(path, encoding="utf-8") as f:
data = json.load(f)
# รองรับทั้ง flat list และ SQuAD-style nested
if isinstance(data, list):
return [ex for ex in data if self._valid(ex)]
# SQuAD format: {"data": [{"paragraphs": [{"qas": [...]}]}]}
examples = []
for article in data.get("data", []):
for para in article.get("paragraphs", []):
context = para.get("context", "")
for qa in para.get("qas", []):
ex = {
"question": qa.get("question", ""),
"context": context,
"answers": qa.get("answers", []),
}
if self._valid(ex):
examples.append(ex)
return examples
def _valid(self, ex: Dict) -> bool:
return (
bool(ex.get("question")) and
bool(ex.get("context")) and
bool(ex.get("answers"))
)
def _find_token_span(
self,
context_ids: List[int],
answer_text: str,
context_start: int, # position ใน full sequence ที่ context เริ่ม
):
"""
หา start/end token position ของ answer ใน context_ids
ใช้ character prefix decoding alignment เพื่อความแม่นยำสูง (100% match rate)
"""
import unicodedata
context_text = self.tokenizer.sp.decode(context_ids)
# preprocess answer ให้ตรงกับ context ที่ผ่าน preprocess แล้ว
answer_clean = preprocess_thai(answer_text)
# NFKC normalize ทั้ง context และ answer เพื่อให้สระอำ (\u0E33) และการแปลงวรรณยุกต์ตรงกัน
context_nfkc = unicodedata.normalize("NFKC", context_text)
answer_nfkc = unicodedata.normalize("NFKC", answer_clean)
char_start = context_nfkc.find(answer_nfkc)
if char_start == -1:
# fallback: ลองค้นหาด้วย text ดิบ
answer_raw_nfkc = unicodedata.normalize("NFKC", answer_text)
char_start = context_nfkc.find(answer_raw_nfkc)
if char_start != -1:
answer_nfkc = answer_raw_nfkc
if char_start == -1:
return context_start, context_start
char_end = char_start + len(answer_nfkc)
prefix_lens = []
for i in range(len(context_ids) + 1):
prefix_lens.append(len(unicodedata.normalize("NFKC", self.tokenizer.sp.decode(context_ids[:i]))))
best_start = None
best_end = None
for i in range(len(context_ids)):
token_start = prefix_lens[i]
token_end = prefix_lens[i+1]
if token_start <= char_start < token_end:
best_start = i
if token_start < char_end <= token_end:
best_end = i
if best_start is None:
best_start = 0
if best_end is None:
best_end = best_start
return context_start + best_start, context_start + best_end
def __len__(self) -> int:
return len(self.examples)
def __getitem__(self, idx: int) -> Dict:
item = self.examples[idx]
# encode_qa คืน [CLS] question [SEP] context [SEP]
encoded = self.tokenizer.encode_qa(
question=item["question"],
context=item["context"],
max_length=self.max_length,
)
# context_ids สำหรับ span matching
context_start = encoded["context_start"]
full_ids = encoded["input_ids"]
context_ids = full_ids[context_start:-1] # ตัด [SEP] สุดท้ายออก
# หา answer span — ใช้ answer แรกถ้ามีหลายคำตอบ
answers = item["answers"]
answer_list = answers if isinstance(answers, list) else answers.get("text", [])
answer_text = answer_list[0] if answer_list else ""
start_pos, end_pos = self._find_token_span(
context_ids, answer_text, context_start
)
# clamp ให้ไม่เกินความยาว sequence จริง
seq_len = len(full_ids)
start_pos = min(start_pos, seq_len - 1)
end_pos = min(end_pos, seq_len - 1)
return {
"input_ids": torch.tensor(full_ids, dtype=torch.long),
"attention_mask": torch.tensor(encoded["attention_mask"], dtype=torch.long),
"start_labels": torch.tensor(start_pos, dtype=torch.long),
"end_labels": torch.tensor(end_pos, dtype=torch.long),
"context_start": torch.tensor(context_start, dtype=torch.long),
} |