Pembersihan teks

Rekayasa Fitur untuk NLP di Python

Rounak Banik

Data Scientist

Teknik pembersihan teks

  • Spasi tak perlu dan escape sequence
  • Tanda baca
  • Karakter khusus (angka, emoji, dll.)
  • Stopword
Rekayasa Fitur untuk NLP di Python

isalpha()

"Dog".isalpha()
True
"3dogs".isalpha()
False
"12347".isalpha()
False
"!".isalpha()
False
"❤".isalpha()
False
Rekayasa Fitur untuk NLP di Python

Catatan penting

  • Singkatan: U.S.A, U.K, dll.
  • Nomina umum: word2vec dan xto10x.
  • Tulis fungsi kustom (pakai regex) untuk kasus yang lebih rumit.
Rekayasa Fitur untuk NLP di Python

Menghapus karakter non-alfabet

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

Menghapus karakter non-alfabet

...
...
# 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'
Rekayasa Fitur untuk NLP di Python

Stopword

  • Kata yang sangat sering muncul
  • Mis. artikel, verba be, pronomina, dll.
Rekayasa Fitur untuk NLP di Python

Menghapus stopword dengan 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. ❤ """
Rekayasa Fitur untuk NLP di Python

Menghapus stopword dengan 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'
Rekayasa Fitur untuk NLP di Python

Teknik prapemrosesan teks lain

  • Menghapus tag HTML/XML
  • Mengganti karakter beraksen (seperti é)
  • Memperbaiki ejaan
Rekayasa Fitur untuk NLP di Python

Catatan penting

Gunakan hanya teknik prapemrosesan yang relevan untuk aplikasi Anda.

Rekayasa Fitur untuk NLP di Python

Ayo berlatih!

Rekayasa Fitur untuk NLP di Python

Preparing Video For Download...