blue balls คือ: คุณกำลังดูกระทู้
บทความโดย อ.ดร.ณัฐโชติ พรหมฤทธิ์
ภาควิชาคอมพิวเตอร์
คณะวิทยาศาสตร์
มหาวิทยาลัยศิลปากร
NLP (Natural Language Processing) เป็น Field หนึ่งของ Artificial Intelligence (AI) ที่จะทำให้มนุษย์มีปฏิสัมพันธ์กับคอมพิวเตอร์ด้วยภาษาธรรมชาติ หรือช่วยให้คอมพิวเตอร์เข้าใจภาษาธรรมชาติของมนุษย์
โดยในการแก้ปัญหาของงานทางด้าน NLP สมัยใหม่ มักใช้เทคนิคทาง Machine Learning เพื่อสร้าง Model จากข้อมูลอย่างเช่น ข้อความ (Text) คำพูด (Speech) หรือแม้แต่รูปภาพ (Image) หรือ Video
ปัจจุบันมี Application ทางด้าน NLP มากมาย เช่น
Speech Recognition
มีการวิจัยและพัฒนาทางด้าน Speech Recognition ในหลายสิบปีที่ผ่านมา แต่ไม่กี่ปีมานี้เราเพิ่งได้เห็นความก้าวหน้าในงานทางด้านนี้อย่างชัดเจน ปัจจุบันเรามี Software ทางด้าน Speech Recognition ที่หลากหลายบนโทรศัพท์มือถือ รวมทั้ง Virtual Assistance, Home Automation และ Video Game เป็นต้น
Document Summarization
Document Summarization คือ การสรุปเนื้อหาในเอกสารให้สั้นลง โดยยังคงได้ความหมายหลักเหมือนเอกสารต้นฉบับ แต่การทำ Document Summarization ด้วยมือ เป็นงานที่ค่อนข้างใช้เวลาและใช้แรงงานอย่างมาก การทำ Document Summarization แบบอัตโนมัติจึงได้รับความสนใจจากนักวิจัยในปัจจุบัน งานที่จะใช้ประโยชน์ต่อจากการทำ Document Summarization เช่น การแยกประเภทข้อความ (Text Classification) การถามตอบ (Question Answering) และการสรุปข่าว (News Summarization) เป็นต้น
Machine Translation
การจะให้ Computer แปลภาษามนุษย์จากภาษาหนึ่งไปอีกภาษาหนึง เป็นเรื่องที่สะดวกในปัจจุบัน ตัวอย่างของ Application ทางด้านนี้ที่ประสบความสำเร็จ คือ Google Translate แต่การแปลให้ดียิ่งขึ้นเพื่อให้เข้าใจความหมายที่แท้จริงในประโยคที่มีความคลุมเครือ และความยืดหยุ่นในการใช้ภาษาของมนุษย์ยังคงเป็นเรื่องท้าทาย
Spam Detection
Spam Detection เป็นการตรวจจับข้อความไม่พึงประสงค์ที่มักจะส่งมาทาง Email เป็นจำนวนมาก ซึ่งเมื่อตรวจจับข้อความเหล่านี้ได้จะมีการคัดแยกไว้ใน Junk Mail Box แต่ความผิดพลาดจากการทำ Spam Detection โดยเฉพาะเมื่อมีการนำนาย Email สำคัญๆ เป็น Junk Mail (False Positive) ก็อาจก่อให้เกิดความเสียหายได้ไม่น้อยครับ
Question Answering
ภาพจาก : https://towardsdatascience.com/building-a-question-answering-system-part-1-9388aadff507
Question Answering เป็นการค้นหาคำตอบที่ถูกต้องเพื่อตอบคำถามของมนุษย์โดยอัตโนมัติ ปัจจุบันการทำ Question Answering มีความก้าวหน้าไปพอสมควร อย่างเช่น Siri ที่สามารถตอบคำถามมนุษย์ในบางเรื่องได้ ในอนาคตเราอาจได้เห็นระบบ Question Answering ที่เต็มรูปแบบยิ่งขึ้น
Autocomplete Search
Autocomplete Search เป็นการเดาข้อความ เพื่อช่วยแนะนำ Keyword ที่สอดคล้องกับข้อความที่ผู้ใช้ได้พิมพ์ไปก่อนหน้า และอาจจะมีการแสดงข้อความแนะนำโดยเรียงตามลำดับคะแนน การเดาข้อความที่ดีจะช่วยผู้ใช้ค้นหาสิ่งที่ต้องการได้อย่างมีประสิทธิภาพครับ
Predictive Typing
Predictive Typing จะมีลักษณะที่คล้ายกับการทำ Autocomplete Search แต่มีวัตถุประสงค์ที่ต่างกัน โดย Predictive Typing จะมีการเดาข้อความเพื่อให้เราเลือก สำหรับสร้างข้อความให้สมบูรณ์ขึ้นเพื่อลดเวลาในการพิมพ์
จาก Application ทางด้าน NLP เหล่านี้ หลาย Application จะมี Input แบบ Text ซึ่งจะต้องมีกระบวนการปรับเปลี่ยนข้อมูล หรือดึง Information บางอย่างออกมา เพื่อนำไปใช้งานในขั้นต่อไป (Text Preprocessing)
Table of Contents
Text Preprocessing
โดยผู้เขียนจะขอยกตัวอย่างเครื่องมือในการทำ Text Preprocessing ได้แก่
- การตัดประโยค
- การตัดคำ
- การเปลี่ยนรูปคำ
- การกำจัดคำที่ไม่สำคัญ (Stopwords)
- Regular Expression
- การลบ Whitespaces
- การสร้าง Bag of Word
- การสร้าง Vector แบบ TF-IDF
- การทำ Part of Speech Tagging (POS)
- การจัดกลุ่ม (Shallow Parsing)
- การทำ Named Entity Recognition
- Context Free Grammar
ก่อนอื่นเราจะ Import Natural Language Toolkit Libraries ตามตัวอย่างด้านล่าง
import nltk
#Sentence tokenizer
nltk.download('punkt')
1 การตัดประโยค
ในภาษาอังกฤษเราสามารถตัดประโยคได้อย่างง่ายๆ ด้วย nltk Library ซึ่งภาษาอังกฤษมีการจบประโยคโดยใช้เครื่องหมาย ‘.’ หรือ ‘?’ แต่เราไม่สามาถตัดประโยคภาษาไทยได้โดยใช้ ‘.’ หรือ ‘?’
ปัจจุบันยังไม่มีเครื่องมือสำหรับการตัดประโยคภาษาไทยที่มีประสิทธิภาพเพียงพอครับ
text = "Let’s start with an important point for NLP in general — this past year there has been progress in NLP by scaling up transformer type models such that each larger model, progressively improved final task accuracy by simply building a larger and larger pre-trained model. In the original BERT paper, they showed that larger hidden sizes, more hidden layers and more attention heads resulted in progressive improvements and tested up to 1024 hidden size."
sentences = nltk.sent_tokenize(text)
for sentence in sentences:
print(sentence)
text2 = 'ประเทศไทยตอนบนมีฝนฟ้าคะนองและลมกระโชกแรงบางแห่ง ขอให้ระวังอันตรายจากฝนฟ้าคะนองและลมกระโชกแรง ภาคใต้ยังมีฝนต่อเนื่อง'
sentences2 = nltk.sent_tokenize(text2)
for sentence in sentences2:
print(sentence)
print()
2 การตัดคำ
เราสามารถใช้ nltk ตัดคำในภาษาอังกฤษได้เช่นเดียวกับการตัดประโยค เพราะภาษาอังฤษมีการเว้นวรรคระหว่างคำ ขณะที่การตัดคำภาษาไทยนั้นค่อนข้างซับซ้อนกว่า เพราะการเขียนประโยคภาษาไทยจะไม่มีการเว้นวรรคระหว่างคำเหมือนในภาษาอังกฤษ แต่ปัจจุบันก็มีการพัฒนา Package/Library โดยฝีมือคนไทยสำหรับการตัดคำภาษาไทย ที่มีความสามารถค่อนข้างดี เช่น Deepcut และ PyThaiNLP เป็นต้น
การตัดคำภาษาอังกฤษโดยใช้ nltk.word_tokenize
for sentence in sentences:
words = nltk.word_tokenize(sentence)
print(words)
การตัดคำภาษาไทยโดยใช้ nltk.word_tokenize
for sentence in sentences2[0]:
words = nltk.word_tokenize(sentence)
print(words)
การตัดคำภาษาไทยโดยใช้ deepcut และ pythainlp
# ตัดคำด้วย deepcut
# pip install deepcut
import deepcut
tokens = deepcut.tokenize(sentences2[0])
tokens
# ตัดคำด้วย pythainlp
# pip install pythainlp
from pythainlp.tokenize import word_tokenize
text='ผมรักคุณนะครับโอเคบ่พวกเราเป็นคนไทยรักภาษาไทยภาษาบ้านเกิด'
a=word_tokenize(text)
a
3 การเปลี่ยนรูปคำ
ด้วยไวยากรณ์ทางภาษา เช่นในภาษาอังกฤษ ทำให้แต่ละประโยคอาจมีรูปของคำที่แตกต่างกัน เช่น problems, problem, seen และ see ฯลฯ ซึ่งในบางครั้งความหลากหลายของรูปคำเช่นนี้อาจส่งผลต่อประสิทธิภาพในการทำนายของ Model ได้ ดังนั้นเราอาจต้องแปลงคำเหล่านี้เป็นรูปคำพื้นฐาน โดยการทำ Stemming และ Lemmatization
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.corpus import wordnet
nltk.download('wordnet')
3.1 Stemming (การตัดส่วนขยาย)
เช่น ตัด s, es, ing หรือ ed
3.2 Lemmatization (การเปลี่ยนรูปคำให้อยู่ในรูปแบบดั้งเดิม)
am, are, is, was ‐> be
saw, seen ‐> see
def compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word, pos):
print("Stemmer:", stemmer.stem(word))
print("Lemmatizer:", lemmatizer.lemmatize(word, pos))
print("\n")
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "problems", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "seen", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "drove", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "are", pos = wordnet.VERB)
4 การกำจัดคำที่ไม่สำคัญ (Stopwords)
Stop Word เป็นคำที่เรานำออกไปจากข้อความแล้วยังทำให้สามารถเข้าใจความหมายของมันได้ แต่ถ้าคำเหล่านี้ยังอยู่ก็อาจจะทำให้เกิด Noise เมื่อมีการใช้เทคนิคอย่างเช่น Machine Learning
from nltk.corpus import stopwords
nltk.download('stopwords')
print(stopwords.words("english"))
stop_words = set(stopwords.words("english"))
sentence = "Our mission is to ensure that artificial general intelligence benefits all of humanity."
words = nltk.word_tokenize(sentence)
without_stop_words = []
for word in words:
if word not in stop_words:
without_stop_words.append(word)
print(without_stop_words)
5 Regular Expression
เป็นการเขียนลำดับของตัวอักษรเพื่อสร้าง Pattern ที่ Match กับข้อความ (Text) สำหรับการลบอักขระ ตัวเลข หรือการค้นหาข้อความที่ตรงกับ Pattern ที่กำหนด ฯลฯ ดังเช่นตัวอย่างต่อไปนี้
5.1 ลบอักขระอื่นที่ไม่ใช่ตัวอักษรภาษาอังกฤษและตัวเลข
import re
sentence = "Give 100%!"
pattern = r"[^\w]"
print(re.sub(pattern, " ", sentence))
5.2 ลบตัวเลข
input_str = "Box A contains 30 red and 5 white balls, while Box B contains 4 red and 2 blue balls."
result = re.sub(r"\d", "", input_str)
print(result)
5.3 การค้นหา
txt = "ประเทศไทย ตอนบนมีฝนฟ้าคะนอง"
x = re.search("^ประเทศไทย.*ฝนฟ้าคะนอง$", txt)
# ^ Matches the start of the string
# . Any character
# + one or more
# * Zero or more occurrences
# $ Ends with
if (x):
print("YES! We have a match!")
else:
print("No match")
6 ลบ Whitespaces
input_str = " \t Hello NLP in AI course\t "
input_str = input_str.strip()
input_str
7 Bag of Word
เทคนิคทาง Machine Learnig ต่างๆ จะไม่สามารถประมวลผลข้อมูลได้จากข้อความ (Text) โดยตรง ดังนั้นเราจึงจำเป็นต้องเปลี่ยนรูปของ Text ให้เป็นกลุ่มของตัวเลข หรือ Vector โดย Bag of Word เป็นเทคนิคสำหรับการสร้าง Vector ของคำหรือประโยคที่นิยมให้กันในยุคแรกๆ ของงานทางด้าน NLP
7.1 Load Data
Load Data จาก http://gitlab.cpsudevops.com/nuttachot/intro-to-npl.git แล้วอ่านไฟล์
with open("test.txt", "r") as file:
documents = file.read().splitlines()
print(documents)
import pandas as pd
df = pd.DataFrame(documents, columns = ['sentence'])
df
7.2 การสร้าง Bag of Word และแสดงข้อความด้วย Vector
from sklearn.feature_extraction.text import CountVectorizer
count_vectorizer = CountVectorizer()
bag_of_words = count_vectorizer.fit_transform(documents)
feature_names = count_vectorizer.get_feature_names()
feature_names
print(df)
bag_of_words.toarray()
#แปลงเป็น Pandas Frame
wc = pd.DataFrame(bag_of_words.toarray(), columns = feature_names)
wc
8 Vector แบบ TF-IDF
อย่างไรก็ตาม Vector แบบ Bag of Word นั้นยังขาด Information ของคำสำหรับการสร้าง Model ที่มีความแม่นยำ วิธีการหนึ่งที่จะช่วยสร้าง Vector คือ เทคนิคแบบ TF-IDF
โดย TF-IDF จะใช้วิธีการทางสถิติในการประมาณความสำคัญของคำในเอกสารที่เป็นส่วนหนึ่งของ Text Dataset หรือ Corpus
สมการ TF-IDF
ตัวอย่างการคำนวน TF-IDF ของ “cat” ในประโยค “The cat sat on my face”
tf = 1/6
N = 2
df = 1
TF-IDF = 1/6*log(2/1) = 0.050172
TF-IDF Vector
9 Part of Speech Tagging (POS)
เป็นวิธีการกำกับหน้าที่ของคำในประโยค เช่น Noun, Adjective และ Verb เป็นต้น โดยจะขอแสดงตัวอย่างการกำกับหน้าที่ของคำในประโยคโดยใช้ textblob Libraries ด้วยคำสั่งต่อไปนี้
# ภาษาอังกฤษ Work!
# !pip install textblob
from textblob import TextBlob
input_str="I like this movie, it's funny"
result = TextBlob(input_str)
print(result.tags)
#ภาษาไทยยังไม่ Work!
input_str="ฉัน ชอบ กิน ข้าว"
result = TextBlob(input_str)
print(result.tags)
POS tag:
CC coordinating conjunction
CD cardinal digit
DT determiner (คำนำหน้านาม)
EX existential there (like: "there is" ... think of it like "there exists")
FW foreign word
IN preposition/subordinating conjunction
JJ adjective 'big' (คำคุณศัพท์)
JJR adjective, comparative 'bigger'
JJS adjective, superlative 'biggest'
LS list marker 1)
MD modal could, will
NN noun, singular 'desk' (คำนาม)
NNS noun plural 'desks'
NNP proper noun, singular 'Harrison'
NNPS proper noun, plural 'Americans'
PDT predeterminer 'all the kids'
POS possessive ending parent\'s
PRP personal pronoun I, he, she (คำสรรพนาม)
PRP$ possessive pronoun my, his, hers
RB adverb very, silently,
RBR adverb, comparative better
RBS adverb, superlative best
RP particle give up
TO to go 'to' the store.
UH interjection errrrrrrrm
VB verb, base form take
VBD verb, past tense took
VBG verb, gerund/present participle taking
VBN verb, past participle taken
VBP verb, sing. present, non-3d take (คำกิริยา)
VBZ verb, 3rd person sing. present takes (คำกิริยา)
WDT wh-determiner which
WP wh-pronoun who, what
WP$ possessive wh-pronoun whose
WRB wh-abverb where, when
10 การจัดกลุ่ม (Shallow Parsing)
เป็นการจัดกลุ่มของคำตามไวยกรณ์ที่กำหนดเอง โดยก่อนจะจัดกลุ่ม เราจะต้องทำ Part of Speech Tagging กับประโยคหรือวลี แล้วนำเข้าเป็น Input เพื่อจัดกลุ่มตามไวยกรณ์ที่กำหนดไว้ต่อไป
from textblob import TextBlob
input_str="I like this movie, it's funny."
result = TextBlob(input_str)
print(result.tags)
การทำ Speech Tagging
reg_exp = "NP: {<DT>?<JJ>*<NN>}"
rp = nltk.RegexpParser(reg_exp)
res = rp.parse(result.tags)
print(res)
res.draw()
การทำ Shallow Parsing
? มีหรือไม่มีก็ได้
* มีอย่างน้อย 1
แสดง Shallow Parsing แบบ Tree
11 Named Entity Recognition
Named Entity คือ คำที่มีความหมายเฉพาะ เช่น ชื่อคน ชื่อหน่วยงาน เวลา หรือ ตัวเลข ฯลฯ โดยการทำ Named Entity Recognition จะช่วยในการตอบคำถามของมนุษย์ เช่น ศรีสุวรรณ ไปพักค้างคืนที่ไหน จากวรรคหนึ่งของกลอนเรื่องพระอภัยมณี ดังต่อไปนี้ “ศรีสุวรรณนั้นมาค้างอยู่ปรางศ์มาส“
from nltk import word_tokenize, pos_tag, ne_chunk
nltk.download('maxent_ne_chunker')
nltk.download('words')
input_str = "Bob works for Silpakorn University so he went to Japan for a conference."
print(ne_chunk(pos_tag(word_tokenize(input_str))))
ตัวอย่าง NE tags
geo = Geographical Entity
org/ORGANIZATION = Organization
per/PERSON = Person
gpe = Geopolitical Entity
tim = Time indicator
art = Artifact
eve = Event
nat = Natural Phenomenon
12 Context Free Grammar
Context Free Grammar (CFG) เป็นเครื่องมือในการนิยามไวยากรณ์สำหรับภาษาต่างๆ
my_grammar = nltk.CFG.fromstring("""
S -> N V O
N -> "ฉัน" | "สุนัข" | "แมว"
V -> "กิน" | "เล่น" | "เรียน"
O -> "ข้าว" | "เกม" | "หนังสือ"
""")
N : Noun (นาม)
V : Verb (กริยา)
O : OBJECT (กรรม)
sent = "ฉัน เล่น เกม".split()
rd_parser = nltk.RecursiveDescentParser(my_grammar)
for tree in rd_parser.parse(sent):
print(tree)
การสร้างประโยคอย่างง่ายจาก Context Free Gramma
from nltk.parse.generate import generate
for sentence in generate(my_grammar, n=20):
print(' '.join(sentence))
ลองทำดูครับ
“เป็นการเขียนลำดับของตัวอักษรเพื่อสร้าง Pattern ที่ Match กับข้อความ (Text) ใช้สำหรับการลบอักขระ ตัวเลข การค้นหา เป็นต้น”
จากข้อความด้านบน ลองเขียน Program โดยใช้ Regular Expression เพื่อให้ได้ผลลัพธ์ ดังนี้
Guideline
- แยกภาษาอังกฤษออกจากภาษาไทย
- แปลงข้อความภาษาอังกฤษ เป็นตัวพิมพ์เล็กทั้งหมด
- ตัดคำ และนับคำภาษาอังกฤษ
- ตัดคำ และนับคำภาษาไทย
เฉลย!
Bonus -> TF-IDF from Scratch
[NEW] Introduction to NLP | blue balls คือ – NATAVIGUIDES
บทความโดย อ.ดร.ณัฐโชติ พรหมฤทธิ์
ภาควิชาคอมพิวเตอร์
คณะวิทยาศาสตร์
มหาวิทยาลัยศิลปากร
NLP (Natural Language Processing) เป็น Field หนึ่งของ Artificial Intelligence (AI) ที่จะทำให้มนุษย์มีปฏิสัมพันธ์กับคอมพิวเตอร์ด้วยภาษาธรรมชาติ หรือช่วยให้คอมพิวเตอร์เข้าใจภาษาธรรมชาติของมนุษย์
โดยในการแก้ปัญหาของงานทางด้าน NLP สมัยใหม่ มักใช้เทคนิคทาง Machine Learning เพื่อสร้าง Model จากข้อมูลอย่างเช่น ข้อความ (Text) คำพูด (Speech) หรือแม้แต่รูปภาพ (Image) หรือ Video
ปัจจุบันมี Application ทางด้าน NLP มากมาย เช่น
Speech Recognition
มีการวิจัยและพัฒนาทางด้าน Speech Recognition ในหลายสิบปีที่ผ่านมา แต่ไม่กี่ปีมานี้เราเพิ่งได้เห็นความก้าวหน้าในงานทางด้านนี้อย่างชัดเจน ปัจจุบันเรามี Software ทางด้าน Speech Recognition ที่หลากหลายบนโทรศัพท์มือถือ รวมทั้ง Virtual Assistance, Home Automation และ Video Game เป็นต้น
Document Summarization
Document Summarization คือ การสรุปเนื้อหาในเอกสารให้สั้นลง โดยยังคงได้ความหมายหลักเหมือนเอกสารต้นฉบับ แต่การทำ Document Summarization ด้วยมือ เป็นงานที่ค่อนข้างใช้เวลาและใช้แรงงานอย่างมาก การทำ Document Summarization แบบอัตโนมัติจึงได้รับความสนใจจากนักวิจัยในปัจจุบัน งานที่จะใช้ประโยชน์ต่อจากการทำ Document Summarization เช่น การแยกประเภทข้อความ (Text Classification) การถามตอบ (Question Answering) และการสรุปข่าว (News Summarization) เป็นต้น
Machine Translation
การจะให้ Computer แปลภาษามนุษย์จากภาษาหนึ่งไปอีกภาษาหนึง เป็นเรื่องที่สะดวกในปัจจุบัน ตัวอย่างของ Application ทางด้านนี้ที่ประสบความสำเร็จ คือ Google Translate แต่การแปลให้ดียิ่งขึ้นเพื่อให้เข้าใจความหมายที่แท้จริงในประโยคที่มีความคลุมเครือ และความยืดหยุ่นในการใช้ภาษาของมนุษย์ยังคงเป็นเรื่องท้าทาย
Spam Detection
Spam Detection เป็นการตรวจจับข้อความไม่พึงประสงค์ที่มักจะส่งมาทาง Email เป็นจำนวนมาก ซึ่งเมื่อตรวจจับข้อความเหล่านี้ได้จะมีการคัดแยกไว้ใน Junk Mail Box แต่ความผิดพลาดจากการทำ Spam Detection โดยเฉพาะเมื่อมีการนำนาย Email สำคัญๆ เป็น Junk Mail (False Positive) ก็อาจก่อให้เกิดความเสียหายได้ไม่น้อยครับ
Question Answering
ภาพจาก : https://towardsdatascience.com/building-a-question-answering-system-part-1-9388aadff507
Question Answering เป็นการค้นหาคำตอบที่ถูกต้องเพื่อตอบคำถามของมนุษย์โดยอัตโนมัติ ปัจจุบันการทำ Question Answering มีความก้าวหน้าไปพอสมควร อย่างเช่น Siri ที่สามารถตอบคำถามมนุษย์ในบางเรื่องได้ ในอนาคตเราอาจได้เห็นระบบ Question Answering ที่เต็มรูปแบบยิ่งขึ้น
Autocomplete Search
Autocomplete Search เป็นการเดาข้อความ เพื่อช่วยแนะนำ Keyword ที่สอดคล้องกับข้อความที่ผู้ใช้ได้พิมพ์ไปก่อนหน้า และอาจจะมีการแสดงข้อความแนะนำโดยเรียงตามลำดับคะแนน การเดาข้อความที่ดีจะช่วยผู้ใช้ค้นหาสิ่งที่ต้องการได้อย่างมีประสิทธิภาพครับ
Predictive Typing
Predictive Typing จะมีลักษณะที่คล้ายกับการทำ Autocomplete Search แต่มีวัตถุประสงค์ที่ต่างกัน โดย Predictive Typing จะมีการเดาข้อความเพื่อให้เราเลือก สำหรับสร้างข้อความให้สมบูรณ์ขึ้นเพื่อลดเวลาในการพิมพ์
จาก Application ทางด้าน NLP เหล่านี้ หลาย Application จะมี Input แบบ Text ซึ่งจะต้องมีกระบวนการปรับเปลี่ยนข้อมูล หรือดึง Information บางอย่างออกมา เพื่อนำไปใช้งานในขั้นต่อไป (Text Preprocessing)
Text Preprocessing
โดยผู้เขียนจะขอยกตัวอย่างเครื่องมือในการทำ Text Preprocessing ได้แก่
- การตัดประโยค
- การตัดคำ
- การเปลี่ยนรูปคำ
- การกำจัดคำที่ไม่สำคัญ (Stopwords)
- Regular Expression
- การลบ Whitespaces
- การสร้าง Bag of Word
- การสร้าง Vector แบบ TF-IDF
- การทำ Part of Speech Tagging (POS)
- การจัดกลุ่ม (Shallow Parsing)
- การทำ Named Entity Recognition
- Context Free Grammar
ก่อนอื่นเราจะ Import Natural Language Toolkit Libraries ตามตัวอย่างด้านล่าง
import nltk
#Sentence tokenizer
nltk.download('punkt')
1 การตัดประโยค
ในภาษาอังกฤษเราสามารถตัดประโยคได้อย่างง่ายๆ ด้วย nltk Library ซึ่งภาษาอังกฤษมีการจบประโยคโดยใช้เครื่องหมาย ‘.’ หรือ ‘?’ แต่เราไม่สามาถตัดประโยคภาษาไทยได้โดยใช้ ‘.’ หรือ ‘?’
ปัจจุบันยังไม่มีเครื่องมือสำหรับการตัดประโยคภาษาไทยที่มีประสิทธิภาพเพียงพอครับ
text = "Let’s start with an important point for NLP in general — this past year there has been progress in NLP by scaling up transformer type models such that each larger model, progressively improved final task accuracy by simply building a larger and larger pre-trained model. In the original BERT paper, they showed that larger hidden sizes, more hidden layers and more attention heads resulted in progressive improvements and tested up to 1024 hidden size."
sentences = nltk.sent_tokenize(text)
for sentence in sentences:
print(sentence)
text2 = 'ประเทศไทยตอนบนมีฝนฟ้าคะนองและลมกระโชกแรงบางแห่ง ขอให้ระวังอันตรายจากฝนฟ้าคะนองและลมกระโชกแรง ภาคใต้ยังมีฝนต่อเนื่อง'
sentences2 = nltk.sent_tokenize(text2)
for sentence in sentences2:
print(sentence)
print()
2 การตัดคำ
เราสามารถใช้ nltk ตัดคำในภาษาอังกฤษได้เช่นเดียวกับการตัดประโยค เพราะภาษาอังฤษมีการเว้นวรรคระหว่างคำ ขณะที่การตัดคำภาษาไทยนั้นค่อนข้างซับซ้อนกว่า เพราะการเขียนประโยคภาษาไทยจะไม่มีการเว้นวรรคระหว่างคำเหมือนในภาษาอังกฤษ แต่ปัจจุบันก็มีการพัฒนา Package/Library โดยฝีมือคนไทยสำหรับการตัดคำภาษาไทย ที่มีความสามารถค่อนข้างดี เช่น Deepcut และ PyThaiNLP เป็นต้น
การตัดคำภาษาอังกฤษโดยใช้ nltk.word_tokenize
for sentence in sentences:
words = nltk.word_tokenize(sentence)
print(words)
การตัดคำภาษาไทยโดยใช้ nltk.word_tokenize
for sentence in sentences2[0]:
words = nltk.word_tokenize(sentence)
print(words)
การตัดคำภาษาไทยโดยใช้ deepcut และ pythainlp
# ตัดคำด้วย deepcut
# pip install deepcut
import deepcut
tokens = deepcut.tokenize(sentences2[0])
tokens
# ตัดคำด้วย pythainlp
# pip install pythainlp
from pythainlp.tokenize import word_tokenize
text='ผมรักคุณนะครับโอเคบ่พวกเราเป็นคนไทยรักภาษาไทยภาษาบ้านเกิด'
a=word_tokenize(text)
a
3 การเปลี่ยนรูปคำ
ด้วยไวยากรณ์ทางภาษา เช่นในภาษาอังกฤษ ทำให้แต่ละประโยคอาจมีรูปของคำที่แตกต่างกัน เช่น problems, problem, seen และ see ฯลฯ ซึ่งในบางครั้งความหลากหลายของรูปคำเช่นนี้อาจส่งผลต่อประสิทธิภาพในการทำนายของ Model ได้ ดังนั้นเราอาจต้องแปลงคำเหล่านี้เป็นรูปคำพื้นฐาน โดยการทำ Stemming และ Lemmatization
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.corpus import wordnet
nltk.download('wordnet')
3.1 Stemming (การตัดส่วนขยาย)
เช่น ตัด s, es, ing หรือ ed
3.2 Lemmatization (การเปลี่ยนรูปคำให้อยู่ในรูปแบบดั้งเดิม)
am, are, is, was ‐> be
saw, seen ‐> see
def compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word, pos):
print("Stemmer:", stemmer.stem(word))
print("Lemmatizer:", lemmatizer.lemmatize(word, pos))
print("\n")
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "problems", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "seen", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "drove", pos = wordnet.VERB)
compare_stemmer_and_lemmatizer(stemmer, lemmatizer, word = "are", pos = wordnet.VERB)
4 การกำจัดคำที่ไม่สำคัญ (Stopwords)
Stop Word เป็นคำที่เรานำออกไปจากข้อความแล้วยังทำให้สามารถเข้าใจความหมายของมันได้ แต่ถ้าคำเหล่านี้ยังอยู่ก็อาจจะทำให้เกิด Noise เมื่อมีการใช้เทคนิคอย่างเช่น Machine Learning
from nltk.corpus import stopwords
nltk.download('stopwords')
print(stopwords.words("english"))
stop_words = set(stopwords.words("english"))
sentence = "Our mission is to ensure that artificial general intelligence benefits all of humanity."
words = nltk.word_tokenize(sentence)
without_stop_words = []
for word in words:
if word not in stop_words:
without_stop_words.append(word)
print(without_stop_words)
5 Regular Expression
เป็นการเขียนลำดับของตัวอักษรเพื่อสร้าง Pattern ที่ Match กับข้อความ (Text) สำหรับการลบอักขระ ตัวเลข หรือการค้นหาข้อความที่ตรงกับ Pattern ที่กำหนด ฯลฯ ดังเช่นตัวอย่างต่อไปนี้
5.1 ลบอักขระอื่นที่ไม่ใช่ตัวอักษรภาษาอังกฤษและตัวเลข
import re
sentence = "Give 100%!"
pattern = r"[^\w]"
print(re.sub(pattern, " ", sentence))
5.2 ลบตัวเลข
input_str = "Box A contains 30 red and 5 white balls, while Box B contains 4 red and 2 blue balls."
result = re.sub(r"\d", "", input_str)
print(result)
5.3 การค้นหา
txt = "ประเทศไทย ตอนบนมีฝนฟ้าคะนอง"
x = re.search("^ประเทศไทย.*ฝนฟ้าคะนอง$", txt)
# ^ Matches the start of the string
# . Any character
# + one or more
# * Zero or more occurrences
# $ Ends with
if (x):
print("YES! We have a match!")
else:
print("No match")
6 ลบ Whitespaces
input_str = " \t Hello NLP in AI course\t "
input_str = input_str.strip()
input_str
7 Bag of Word
เทคนิคทาง Machine Learnig ต่างๆ จะไม่สามารถประมวลผลข้อมูลได้จากข้อความ (Text) โดยตรง ดังนั้นเราจึงจำเป็นต้องเปลี่ยนรูปของ Text ให้เป็นกลุ่มของตัวเลข หรือ Vector โดย Bag of Word เป็นเทคนิคสำหรับการสร้าง Vector ของคำหรือประโยคที่นิยมให้กันในยุคแรกๆ ของงานทางด้าน NLP
7.1 Load Data
Load Data จาก http://gitlab.cpsudevops.com/nuttachot/intro-to-npl.git แล้วอ่านไฟล์
with open("test.txt", "r") as file:
documents = file.read().splitlines()
print(documents)
import pandas as pd
df = pd.DataFrame(documents, columns = ['sentence'])
df
7.2 การสร้าง Bag of Word และแสดงข้อความด้วย Vector
from sklearn.feature_extraction.text import CountVectorizer
count_vectorizer = CountVectorizer()
bag_of_words = count_vectorizer.fit_transform(documents)
feature_names = count_vectorizer.get_feature_names()
feature_names
print(df)
bag_of_words.toarray()
#แปลงเป็น Pandas Frame
wc = pd.DataFrame(bag_of_words.toarray(), columns = feature_names)
wc
8 Vector แบบ TF-IDF
อย่างไรก็ตาม Vector แบบ Bag of Word นั้นยังขาด Information ของคำสำหรับการสร้าง Model ที่มีความแม่นยำ วิธีการหนึ่งที่จะช่วยสร้าง Vector คือ เทคนิคแบบ TF-IDF
โดย TF-IDF จะใช้วิธีการทางสถิติในการประมาณความสำคัญของคำในเอกสารที่เป็นส่วนหนึ่งของ Text Dataset หรือ Corpus
สมการ TF-IDF
ตัวอย่างการคำนวน TF-IDF ของ “cat” ในประโยค “The cat sat on my face”
tf = 1/6
N = 2
df = 1
TF-IDF = 1/6*log(2/1) = 0.050172
TF-IDF Vector
9 Part of Speech Tagging (POS)
เป็นวิธีการกำกับหน้าที่ของคำในประโยค เช่น Noun, Adjective และ Verb เป็นต้น โดยจะขอแสดงตัวอย่างการกำกับหน้าที่ของคำในประโยคโดยใช้ textblob Libraries ด้วยคำสั่งต่อไปนี้
# ภาษาอังกฤษ Work!
# !pip install textblob
from textblob import TextBlob
input_str="I like this movie, it's funny"
result = TextBlob(input_str)
print(result.tags)
#ภาษาไทยยังไม่ Work!
input_str="ฉัน ชอบ กิน ข้าว"
result = TextBlob(input_str)
print(result.tags)
POS tag:
CC coordinating conjunction
CD cardinal digit
DT determiner (คำนำหน้านาม)
EX existential there (like: "there is" ... think of it like "there exists")
FW foreign word
IN preposition/subordinating conjunction
JJ adjective 'big' (คำคุณศัพท์)
JJR adjective, comparative 'bigger'
JJS adjective, superlative 'biggest'
LS list marker 1)
MD modal could, will
NN noun, singular 'desk' (คำนาม)
NNS noun plural 'desks'
NNP proper noun, singular 'Harrison'
NNPS proper noun, plural 'Americans'
PDT predeterminer 'all the kids'
POS possessive ending parent\'s
PRP personal pronoun I, he, she (คำสรรพนาม)
PRP$ possessive pronoun my, his, hers
RB adverb very, silently,
RBR adverb, comparative better
RBS adverb, superlative best
RP particle give up
TO to go 'to' the store.
UH interjection errrrrrrrm
VB verb, base form take
VBD verb, past tense took
VBG verb, gerund/present participle taking
VBN verb, past participle taken
VBP verb, sing. present, non-3d take (คำกิริยา)
VBZ verb, 3rd person sing. present takes (คำกิริยา)
WDT wh-determiner which
WP wh-pronoun who, what
WP$ possessive wh-pronoun whose
WRB wh-abverb where, when
10 การจัดกลุ่ม (Shallow Parsing)
เป็นการจัดกลุ่มของคำตามไวยกรณ์ที่กำหนดเอง โดยก่อนจะจัดกลุ่ม เราจะต้องทำ Part of Speech Tagging กับประโยคหรือวลี แล้วนำเข้าเป็น Input เพื่อจัดกลุ่มตามไวยกรณ์ที่กำหนดไว้ต่อไป
from textblob import TextBlob
input_str="I like this movie, it's funny."
result = TextBlob(input_str)
print(result.tags)
การทำ Speech Tagging
reg_exp = "NP: {<DT>?<JJ>*<NN>}"
rp = nltk.RegexpParser(reg_exp)
res = rp.parse(result.tags)
print(res)
res.draw()
การทำ Shallow Parsing
? มีหรือไม่มีก็ได้
* มีอย่างน้อย 1
แสดง Shallow Parsing แบบ Tree
11 Named Entity Recognition
Named Entity คือ คำที่มีความหมายเฉพาะ เช่น ชื่อคน ชื่อหน่วยงาน เวลา หรือ ตัวเลข ฯลฯ โดยการทำ Named Entity Recognition จะช่วยในการตอบคำถามของมนุษย์ เช่น ศรีสุวรรณ ไปพักค้างคืนที่ไหน จากวรรคหนึ่งของกลอนเรื่องพระอภัยมณี ดังต่อไปนี้ “ศรีสุวรรณนั้นมาค้างอยู่ปรางศ์มาส“
from nltk import word_tokenize, pos_tag, ne_chunk
nltk.download('maxent_ne_chunker')
nltk.download('words')
input_str = "Bob works for Silpakorn University so he went to Japan for a conference."
print(ne_chunk(pos_tag(word_tokenize(input_str))))
ตัวอย่าง NE tags
geo = Geographical Entity
org/ORGANIZATION = Organization
per/PERSON = Person
gpe = Geopolitical Entity
tim = Time indicator
art = Artifact
eve = Event
nat = Natural Phenomenon
12 Context Free Grammar
Context Free Grammar (CFG) เป็นเครื่องมือในการนิยามไวยากรณ์สำหรับภาษาต่างๆ
my_grammar = nltk.CFG.fromstring("""
S -> N V O
N -> "ฉัน" | "สุนัข" | "แมว"
V -> "กิน" | "เล่น" | "เรียน"
O -> "ข้าว" | "เกม" | "หนังสือ"
""")
N : Noun (นาม)
V : Verb (กริยา)
O : OBJECT (กรรม)
sent = "ฉัน เล่น เกม".split()
rd_parser = nltk.RecursiveDescentParser(my_grammar)
for tree in rd_parser.parse(sent):
print(tree)
การสร้างประโยคอย่างง่ายจาก Context Free Gramma
from nltk.parse.generate import generate
for sentence in generate(my_grammar, n=20):
print(' '.join(sentence))
ลองทำดูครับ
“เป็นการเขียนลำดับของตัวอักษรเพื่อสร้าง Pattern ที่ Match กับข้อความ (Text) ใช้สำหรับการลบอักขระ ตัวเลข การค้นหา เป็นต้น”
จากข้อความด้านบน ลองเขียน Program โดยใช้ Regular Expression เพื่อให้ได้ผลลัพธ์ ดังนี้
Guideline
- แยกภาษาอังกฤษออกจากภาษาไทย
- แปลงข้อความภาษาอังกฤษ เป็นตัวพิมพ์เล็กทั้งหมด
- ตัดคำ และนับคำภาษาอังกฤษ
- ตัดคำ และนับคำภาษาไทย
เฉลย!
Bonus -> TF-IDF from Scratch
Are blue balls a real thing? | Womansplainer
The first thing people who have never experienced blue balls usually ask is: \”Are blue balls actually, like, a thing?\” To put it simply, the answer is yes. In fact, blue balls even have a fancy medical name: epidydimal hypertension. Not all men get blue balls, however, and, as much as they suck for those unfortunate souls who do, they’re not a serious medical condition.
นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูความรู้เพิ่มเติมที่นี่
Learn Letters With Max the Glow Train – TOYS (Letters and Toys)
Learn Letters For Toddlers!
Join this amazing adventure with Max the Glow Train, letters and toys.
Max is a fun loving toy train who teaches children the English alphabet. On his way to the zoo Max meets the letters and takes them with him.
Your child will see toy vehicles like an airplane, a bulldozer, a crane, an excavator, a garbage truck, a helicopter, a scooter, a police car, a road roller, a street sweeper, a tow truck, a van, a wagon, and a yacht.
Your child will learn the names of fruits like kiwi, lemon, mango, nectarine and orange.
Your child will see how a fire truck will rescue the shapes – circle, oval, triangle, square, rectangle and octagon.
Your child will see toy animals – a dog, a bear, a cow, a duck, an elephant, a giraffe, a hedgehog, a mouse, a pig, and a zebra.
Learn Letters with Max the Glow Train Poster, 36X24 is now available at www.coilbook.com!
THANK YOU FOR WATCHING!
Follow us on Facebook https://www.facebook.com/coilbook
Follow us on Google+ https://plus.google.com/+coilbook
© 2015, TOYS, Max the Glow Train, Letters, Episode 1.
© 2015, coilbook™ | Learning is easy when it’s fun!™, iCoilbook™ | coilbook international, coildot™
Soundtracks are licensed by coilbook
“Stock media provided by [Alanay]/ Pond5.com“
“Stock media provided by [Helennn]/ Pond5.com“
“Stock media provided by [brianallenholmes]/ Pond5.com“
“Stock media provided by [leetheedge]/ Pond5.com“
“Stock media provided by [fematronik]/ Pond5.com“
“Stock media provided by [Gennadyl]/ Pond5.com“
“Stock media provided by [PlayAgain]/ Pond5.com“
“Stock media provided by [DavidFlavinmusic]/ Pond5.com“
“Stock media provided by [AdiGoldstein]/ Pond5.com“
“Stock media provided by [ChristianA]/ Pond5.com“
“Stock media provided by [ScoreStudio]/ Pond5.com“
“Stock media provided by [Epicfx]/ Pond5.com“
Friday Night Funkin’ – The Blueballs Incident FULL WEEK – FNF MODS [HARD]
Mod Link: https://gamebanana.com/mods/293462
Submitter: MaliciousBunny
Artist, Coder, Charter: MaliciousBunny
Musician, Charter: EZHALT
Special Thanks: IdiotWithAGun, Banbuds, Soulegal (DropTheBeat), Diavololi
Teach Babies Colors, Numbers, and Vehicles with Tayo the Little Bus Toy Video for Kids!
In this preschool, educational video for babies and kids we’ve got the super fun learning toy bus, Tayo the Little Bus! This fun toy bus has tons of great educational features to keep your toddler learning and engaged during this educational video. There are four pop up pals along the top row; each one activated by a different color button with a unique shape and method of pressing. There are ten numbered windows along one side of the bus that pop up to reveal a different type of vehicle. The other side has colorful mini bus sliders and a rotating dial that features animals, foods, colors, shapes, letters and more. It also has fun spinning gears that teach kids simple physics. And best of all this educational toy for kids plays music and sounds! The bus speaks Korean and will help your child understand some of the basics of a second language. Exposing your child to a second language has been shown to greatly improve language skills as children develop. Tayo the Little Bus Pop up surprise pals is one of the best educational toys that we’ve seen and we had so much fun playing with it and are very glad to be able to show you and your child in this educational video what a great toy it is.
You can buy some of the great toys featured in our videos here:
https://www.genevievesplayhouse.com/shoptoys
Subscribe to Genevieve’s Playhouse Here:
https://www.youtube.com/GenevievesPlayhouse?sub_confirmation=1
Play these other learning videos for kids!
Giant 1 Hour Long Paw Patrol Learning Videos: https://youtu.be/1tX2cyvMzX4
Educational Toy Car Videos for Kids: https://youtu.be/XN8k8ZkJwiQ
PJ Masks Toy Car Race: https://youtu.be/k0Twsb22Ld4
Genevieve Teaches Numbers with Cookie Monster: https://youtu.be/_X78XfQkLk0
Learn Words with Pororo the Little Penguin Toy House: https://youtu.be/GzIQqBEJg
Laguna Blue and Pinkie Pie are Hungry: https://youtu.be/oCbegRDxgak
Learn Colors w/ Paw Patrol Underwater Rescue Mission: https://youtu.be/RhrKJvAKsGQ
Peppa Pig gets a NEW House! https://youtu.be/OoMONAVtcDw
Learn How to Count to 10 with Stackable Toy Cars!
Best Preschool Learning Toy Video for Kids with Cute Kid Genevieve Teach Kids How to Count 1 to 10! In today’s educational preschool learning video for kids we’re going to learn our numbers one to ten and how to count! These awesome stacking cars come in ten different colors and each one slides down the parking deck until we have all ten cars. These are some of the best educational toys for kids for teaching counting, adding, subtracting, and colors. They also help teach fine motor skills as well. Cute kid Genevieve joins the fun too and she helps teach our class by using these fun educational preschool toys!
Subscribe Here:
https://www.youtube.com/GenevievesPlayhouse?sub_confirmation=1
Join us on Facebook for bonus footage and pics!
https://www.facebook.com/genevievesplayhouse/
Follow us on Instagram: https://www.instagram.com/genevievesplayhouse/
Here are some of our other fun kid \u0026 toddler learning videos by Genevieve’s Playhouse:
Best Toy Paw Patrol Learning Video Compilation for Kids: https://goo.gl/O1odl9
Tons of Fun Preschool Educational Videos for Toddlers: https://goo.gl/4XQjh2
Learn Colors and Numbers with Locking Toy Dollhouse: https://goo.gl/HfTmo5
Cute Toddler Genevieve Teaching Numbers, Colors and More: https://goo.gl/D4yPZ6
Our 2 year old daughter, Genevieve, teaching the ABC’s: https://goo.gl/cJygXN
Learn Food Names for Kids with Lagoona Blue, Pinkie Pie and Pororo the Little Penguin: https://goo.gl/SyafAF
Sound Effects by http://www.audiomicro.com/soundeffects
นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูวิธีอื่นๆLEARN FOREIGN LANGUAGE
ขอบคุณที่รับชมกระทู้ครับ blue balls คือ