เมธอด wordshape สามารถใช้ร่วมกับเงื่อนไขเฉพาะ เช่น HAS_TITLE_CASE, IS_NUMERIC_VALUE, หรือ HAS_SOME_PUNCT_OR_SYMBOL เพื่อดูว่าสตริงมีคุณสมบัติเฉพาะหรือไม่
อ่านเพิ่มเติม: TensorFlow คืออะไร และ Keras ทำงานกับ TensorFlow อย่างไรในการสร้าง Neural Networks?
เราจะใช้ Keras Sequential API ซึ่งช่วยสร้าง Sequential Model ที่ใช้ทำงานกับ Stack ของ Layers แบบธรรมดา โดยแต่ละ Layer มี Input Tensor และ Output Tensor อย่างละหนึ่งตัว
Neural Network ที่มีอย่างน้อยหนึ่ง Layer ที่เป็น Convolutional Layer เรียกว่า Convolutional Neural Network เราสามารถใช้ CNN สร้าง Learning Model ได้
TensorFlow Text มีชุดคลาสและ Ops ที่เกี่ยวข้องกับข้อความ ใช้ร่วมกับ TensorFlow 2.0 ได้ TensorFlow Text ใช้สำหรับ Preprocess Sequence Modelling
เราใช้ Google Colaboratory รันโค้ดด้านล่าง Google Colab ช่วยรัน Python Code บน Browser ไม่ต้อง Config และเข้าถึง GPU ฟรี Colab สร้างบน Jupyter Notebook
Tokenization คือวิธีแยกสตริงเป็น Tokens Tokens อาจเป็น คำ, ตัวเลข, หรือ Punctuation Interface หลักคือ Tokenizer และ TokenizerWithOffsets แต่ละอันมีเมธอดเดียวคือ tokenize และ tokenize_with_offsets ตามลำดับ มี Tokenizers หลายตัว แต่ละตัว Implement TokenizerWithOffsets (ซึ่ง Extends Tokenizer Class) รวมถึงตัวเลือกได้ Byte Offsets เข้าสู่สตริงต้นฉบับ ช่วยรู้ว่า Byte ไหนในสตริงต้นฉบับที่ Token ถูกสร้างขึ้น
Feature ที่พบบ่อยในโมเดล Natural Language Understanding บางอย่าง คือ ดูว่าสตริงข้อความมีคุณสมบัติเฉพาะหรือไม่ Wordshape กำหนด Regular Expression Based Helper Functions หลากหลาย สำหรับจับคู่ Pattern ที่เกี่ยวข้องใน Input Text นี่คือตัวอย่างบางส่วน
ตัวอย่าง
print("Whitespace tokenizer is being called")
tokenizer = text.WhitespaceTokenizer()
print("Tokens being generated")
tokens = tokenizer.tokenize(['Everything that is not saved will be lost.', u'Sad☹'.encode('UTF-8')])
print("Checking if it is capitalized")
f1 = text.wordshape(tokens, text.WordShape.HAS_TITLE_CASE)
print("Checking if all the letters are uppercase")
f2 = text.wordshape(tokens, text.WordShape.IS_UPPERCASE)
print("Checking if the tokens contain punctuation")
f3 = text.wordshape(tokens, text.WordShape.HAS_SOME_PUNCT_OR_SYMBOL)
print("Checking if the token is a number")
f4 = text.wordshape(tokens, text.WordShape.IS_NUMERIC_VALUE)
print("Printing the results")
print(f1.to_list())
print(f2.to_list())
print(f3.to_list())
print(f4.to_list())
Code credit − https://www.tensorflow.org/tutorials/tensorflow_text/intro
ผลลัพธ์
Whitespace tokenizer is being called Tokens being generated Checking if it is capitalized Checking if all the letters are uppercase Checking if the tokens contain punctuation Checking if the token is a number Printing the results [[True, False, False, False, False, False, False, False], [True]] [[False, False, False, False, False, False, False, False], [False]] [[False, False, False, False, False, False, False, True], [True]] [[False, False, False, False, False, False, False, False], [False]]
คำอธิบาย
- เรียก 'WhitespaceTokenizer' และสร้าง Tokens
- ตรวจสอบตัวอักษรว่ามีตัวพิมพ์ใหญ่หรือไม่
- ตรวจสอบ Punctuation และว่าเป็นตัวเลขหรือไม่
- หลังการคำนวณ แสดงค่า Boolean ออกมา