data(train 520·holdout 120), server(발화 생성 서버), eval(grounding_eval), run_eval.py·run_test.sh. logs/ 는 실행 때마다 생기는 기록이라 README.md 만 올리고 나머지는 .gitignore 로 뺀다. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
"""한국어 토큰화와 전처리 규칙. 베이스라인과 개발 모델에 같은 규칙을 적용한다 (성능평가확인서 ROUGE 산출 조건).
|
|
|
|
전처리 (normalize)
|
|
- 유니코드 NFC 정규화, 앞뒤 공백 제거, 연속 공백을 하나로
|
|
- 숫자 천 단위 구분 쉼표 제거: "4,900원" → "4900원" (표기 차이로 점수가 갈리지 않게)
|
|
|
|
토큰화
|
|
- kiwi: Kiwi 형태소 분석 결과의 형태소(form). drop_punct 이면 문장부호·기호 품사(S*: SF, SP, SS, SE, SO, SW)를 뺀다
|
|
(숫자 SN, 외국어 SL, 한자 SH 는 남긴다)
|
|
- eojeol: 공백 기준 어절 (형태소 분석기 없이 비교할 때)
|
|
"""
|
|
import re
|
|
import unicodedata
|
|
from functools import lru_cache
|
|
|
|
PUNCT_TAGS = frozenset({"SF", "SP", "SS", "SSO", "SSC", "SE", "SO", "SW"})
|
|
TOKENIZERS = ("kiwi", "eojeol")
|
|
|
|
_THOUSANDS = re.compile(r"(?<=\d),(?=\d{3}(?!\d))")
|
|
_SPACES = re.compile(r"\s+")
|
|
|
|
|
|
def normalize(text: str) -> str:
|
|
text = unicodedata.normalize("NFC", text or "")
|
|
text = _THOUSANDS.sub("", text)
|
|
return _SPACES.sub(" ", text).strip()
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _kiwi():
|
|
from kiwipiepy import Kiwi
|
|
return Kiwi()
|
|
|
|
|
|
def kiwi_version() -> str:
|
|
import kiwipiepy
|
|
return kiwipiepy.__version__
|
|
|
|
|
|
def tokenize(text: str, method: str = "kiwi", drop_punct: bool = True) -> list[str]:
|
|
text = normalize(text)
|
|
if not text:
|
|
return []
|
|
if method == "eojeol":
|
|
tokens = text.split(" ")
|
|
if drop_punct:
|
|
tokens = [t.strip(".,!?;:\"'()[]{}…~·") for t in tokens]
|
|
return [t for t in tokens if t]
|
|
if method == "kiwi":
|
|
return [t.form for t in _kiwi().tokenize(text)
|
|
if not (drop_punct and t.tag in PUNCT_TAGS)]
|
|
raise ValueError(f"unknown tokenizer: {method} (choose from {TOKENIZERS})")
|