การทำความสะอาดข้อความ

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

เทคนิคการทำความสะอาดข้อความ

  • ช่องว่างและ escape sequence ที่ไม่จำเป็น
  • เครื่องหมายวรรคตอน
  • อักขระพิเศษ (ตัวเลข, อีโมจิ ฯลฯ)
  • Stopwords
Feature Engineering for NLP in Python

isalpha()

"Dog".isalpha()
True
"3dogs".isalpha()
False
"12347".isalpha()
False
"!".isalpha()
False
"❤".isalpha()
False
Feature Engineering for NLP in Python

ข้อควรระวัง

  • คำย่อ: U.S.A, U.K ฯลฯ
  • คำนามเฉพาะ: word2vec และ xto10x
  • เขียนฟังก์ชันเองโดยใช้ regex สำหรับกรณีที่ซับซ้อนกว่า
Feature Engineering for NLP in Python

การลบอักขระที่ไม่ใช่ตัวอักษร

string = """
OMG!!!! This is like    the best thing ever \t\n. 
Wow, such an amazing song! I'm hooked. Top 5 definitely. ❤
"""

import spacy # Generate list of tokens nlp = spacy.load('en_core_web_sm') doc = nlp(string) lemmas = [token.lemma_ for token in doc]
Feature Engineering for NLP in Python

การลบอักขระที่ไม่ใช่ตัวอักษร

...
...
# Remove tokens that are not alphabetic
a_lemmas = [lemma for lemma in lemmas 
            if lemma.isalpha() or lemma == '-PRON-']

# Print string after text cleaning print(' '.join(a_lemmas))
'omg this be like the good thing ever wow such an amazing song -PRON- be hooked top definitely'
Feature Engineering for NLP in Python

Stopwords

  • คำที่ปรากฏบ่อยมาก
  • เช่น คำนำหน้านาม, กริยา be, สรรพนาม ฯลฯ
Feature Engineering for NLP in Python

การลบ stopwords ด้วย spaCy

# Get list of stopwords
stopwords = spacy.lang.en.stop_words.STOP_WORDS

string = """ OMG!!!! This is like the best thing ever \t\n. Wow, such an amazing song! I'm hooked. Top 5 definitely. ❤ """
Feature Engineering for NLP in Python

การลบ stopwords ด้วย spaCy

...
...
# Remove stopwords and non-alphabetic tokens
a_lemmas = [lemma for lemma in lemmas 
            if lemma.isalpha() and lemma not in stopwords]
# Print string after text cleaning
print(' '.join(a_lemmas))
'omg like good thing wow amazing song hooked definitely'
Feature Engineering for NLP in Python

เทคนิคการประมวลผลข้อความอื่น

  • การลบแท็ก HTML/XML
  • การแทนที่อักขระเสริม (เช่น é)
  • การแก้ไขการสะกดผิด
Feature Engineering for NLP in Python

ข้อควรระวัง

ใช้เฉพาะเทคนิคการประมวลผลข้อความที่เหมาะสมกับงานของคุณเท่านั้น

Feature Engineering for NLP in Python

มาฝึกกันเถอะ!

Feature Engineering for NLP in Python

Preparing Video For Download...