นิยาม
Segmentation (การแบ่งส่วน/แบ่งคำ) คือการแยกข้อความเป็นหน่วยคล้ายคำ (word-like units) ซึ่งจำเป็นเพราะ:
- ภาษาบางภาษา (จีน, ญี่ปุ่น, ไทย) ไม่ใช้ space คั่นคำ
- ภาษาบางภาษา (เยอรมัน) มีคำประสมยาวที่ต้องแยกเพื่อวิเคราะห์ความหมาย
- ข้อความบนเว็บมักผสมหลายสคริปต์ (เช่น "NY株価" = New York + กุฑา)
Unicode และ Code Points
Unicode ให้ code point เฉพาะ (0–0x10FFFF) ต่อตัวอักษรทุกภาษา TensorFlow ใช้ tf.string dtype เก็บ byte string (UTF-8 default) และมี ops สำหรับ Unicode
โค้ดตัวอย่าง: Rough Segmentation ด้วย Script Detection
print("Below is the sentence that is processed")
sentence_texts = [u'Hello, there.', u'世界こんにちは']
print("The code point values for characters in the sentence")
sentence_char_codepoint = tf.strings.unicode_decode(sentence_texts, 'UTF-8')
print(sentence_char_codepoint)
print("The unicode script values for characters in the sentence")
sentence_char_script = tf.strings.unicode_script(sentence_char_codepoint)
print(sentence_char_script)ผลลัพธ์
Below is the sentence that is processed The code point values for characters in the sentence The unicode script values for characters in the sentence
(25=COMMON/Latin, 0=COMMON/punct, 17=HAN, 20=HIRAGANA)
คำอธิบาย
unicode_decode: แปลง string → code points (RaggedTensor)unicode_script: หา script ของแต่ละ code point (int32 mapping ไป ICU UScriptCode)- Script change → approximate word boundary (heuristic)
- Space characters เป็น USCRIPT_COMMON (0) ต่างจาก script จริง จึงช่วยแยกคำในภาษาที่มี space
ข้อสังเกต
Wiki: "NY株価" → script sequence: COMMON, COMMON, HAN, HAN → boundary ระหว่าง Y และ 株 ใช้ได้ดี แต่ภาษาไทยต้องใช้ tokenizer เฉพาะ (deepcut, newmm, etc.)