Tokenization และ Lemmatization

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

แหล่งที่มาของข้อความ

  • บทความข่าว
  • ทวีต
  • ความคิดเห็น
Feature Engineering for NLP in Python

ทำให้ข้อความเป็นมิตรกับเครื่อง

  • Dogs, dog
  • reduction, REDUCING, Reduce
  • don't, do not
  • won't, will not
Feature Engineering for NLP in Python

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

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

Tokenization

"I have a dog. His name is Hachi."

Token:

["I", "have", "a", "dog", ".", "His", "name", "is", "Hachi", "."]
"Don't do this."

Token:

["Do", "n't", "do", "this", "."]
Feature Engineering for NLP in Python

Tokenization ด้วย spaCy

import spacy

# Load the en_core_web_sm model nlp = spacy.load('en_core_web_sm')
# Initiliaze string string = "Hello! I don't know what I'm doing here."
# Create a Doc object doc = nlp(string)
# Generate list of tokens tokens = [token.text for token in doc] print(tokens)
['Hello','!','I','do',"n't",'know','what','I',"'m",'doing','here','.']
Feature Engineering for NLP in Python

Lemmatization

  • แปลงคำให้เป็นรูปฐาน
    • reducing, reduces, reduced, reductionreduce
    • am, are, isbe
    • n'tnot
    • 'vehave
Feature Engineering for NLP in Python

Lemmatization ด้วย spaCy

import spacy

# Load the en_core_web_sm model
nlp = spacy.load('en_core_web_sm')
# Initiliaze string
string = "Hello! I don't know what I'm doing here."
# Create a Doc object
doc = nlp(string)

# Generate list of lemmas lemmas = [token.lemma_ for token in doc] print(lemmas)
['hello','!','-PRON-','do','not','know','what','-PRON','be','do','here', '.']
Feature Engineering for NLP in Python

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

Feature Engineering for NLP in Python

Preparing Video For Download...