Skip to main content
🐍

Python

Python bindings ของ kham สร้างด้วย PyO3 เปิดให้ใช้งาน API ตัดคำแบบเต็มรูปแบบ รวมถึง Token object พร้อม kind, POS, NE และข้อมูล span

1

ติดตั้ง

pip install kham
2

ตัดคำพื้นฐาน

import kham

words = kham.segment("กินข้าวกับปลา")
print(words)
# ['กิน', 'ข้าว', 'กับ', 'ปลา']

mixed = kham.segment("Hello กรุงเทพ 2024!")
print(mixed)
3

Token object แบบ rich

import kham

tokens = kham.segment_tokens("กินข้าวกับปลา")
for tok in tokens:
    print(f"{tok.text!r:10}  kind={tok.kind:12}  chars={tok.char_start}..{tok.char_end}")
4

Field ของ Token

tok.text        # str  — ข้อความ token
tok.kind        # str  — "Thai" | "Latin" | "Number" | "Punctuation" | ...
tok.char_start  # int  — จุดเริ่มต้น Unicode (ใช้กับ str.slice())
tok.char_end    # int  — จุดสิ้นสุด Unicode
tok.byte_start  # int  — byte offset UTF-8 เริ่มต้น
tok.byte_end    # int  — byte offset UTF-8 สิ้นสุด