Tokenisasi dan Lemmatisasi

Rekayasa Fitur untuk NLP di Python

Rounak Banik

Data Scientist

Sumber teks

  • Artikel berita
  • Tweet
  • Komentar
Rekayasa Fitur untuk NLP di Python

Membuat teks ramah mesin

  • Dogs, dog
  • reduction, REDUCING, Reduce
  • don't, do not
  • won't, will not
Rekayasa Fitur untuk NLP di Python

Teknik prapemrosesan teks

  • Ubah huruf ke lowercase
  • Hapus spasi di awal/akhir
  • Hapus tanda baca
  • Hapus stopword
  • Perluas kontraksi
  • Hapus karakter khusus (angka, emoji, dll.)
Rekayasa Fitur untuk NLP di Python

Tokenisasi

"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", "."]
Rekayasa Fitur untuk NLP di Python

Tokenisasi dengan 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','.']
Rekayasa Fitur untuk NLP di Python

Lemmatisasi

  • Ubah kata ke bentuk dasar
    • reducing, reduces, reduced, reductionreduce
    • am, are, isbe
    • n'tnot
    • 'vehave
Rekayasa Fitur untuk NLP di Python

Lemmatisasi dengan 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', '.']
Rekayasa Fitur untuk NLP di Python

Ayo berlatih!

Rekayasa Fitur untuk NLP di Python

Preparing Video For Download...