แนวคิด
ใช้ WhitespaceTokenizer สร้าง tokenizer เรียก tokenize บน string
ใช้ Keras Sequential API, CNN, TensorFlow Text, Google Colab เหมือนเดิม
Tokenization แบ่ง string เป็น tokens (คำ, ตัวเลข, ปunctuation) interfaces หลัก: Tokenizer และ TokenizerWithOffsets มีเมธอด tokenize และ tokenize_with_offsets มีหลาย tokenizer implements TokenizerWithOffsets ให้ byte offsets ของ string ต้นทาง
N-grams คือคำต่อเนื่องเมื่อ sliding window size n รวม tokens ได้ 3 reduction mechanisms สำหรับ text ใช้ Reduction.STRING_JOIN คั่นด้วย space (เปลี่ยนได้ด้วย string_separater) อื่นๆ: Reduction.SUM, Reduction.MEAN สำหรับตัวเลข
ตัวอย่างโค้ด
print("Whitespace tokenizer is being called")
tokenizer = text.WhitespaceTokenizer()
tokens = tokenizer.tokenize(['Everything not saved will be lost.', u'Sad☹'.encode('UTF-8')])
print("Here, n is 2, hence it is bigram")
bigrams = text.ngrams(tokens, 2, reduction_type=text.Reduction.STRING_JOIN)
print("The bigrams are converted to a list")
print(bigrams.to_list())
ผลลัพธ์
Whitespace tokenizer is being called Here, n is 2, hence it is bigram The bigrams are converted to a list [[b'Everything not', b'not saved', b'saved will', b'will be', b'be lost.'], []]
คำอธิบาย
- เรียก Whitespace tokenizer
- ตั้ง n=2 เรียกว่า bigram
- เก็บ tokens ใน list แสดงผล