Metin temizleme

Python ile NLP için Özellik Mühendisliği

Rounak Banik

Data Scientist

Metin temizleme teknikleri

  • Gereksiz boşluklar ve kaçış dizileri
  • Noktalama işaretleri
  • Özel karakterler (sayılar, emojiler vb.)
  • Stopwords
Python ile NLP için Özellik Mühendisliği

isalpha()

"Dog".isalpha()
True
"3dogs".isalpha()
False
"12347".isalpha()
False
"!".isalpha()
False
"❤".isalpha()
False
Python ile NLP için Özellik Mühendisliği

Dikkat

  • Kısaltmalar: U.S.A, U.K vb.
  • Özel adlar: word2vec ve xto10x.
  • Daha ince durumlar için (regex ile) özel bir işlev yazın.
Python ile NLP için Özellik Mühendisliği

Alfabetik olmayan karakterleri kaldırma

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]
Python ile NLP için Özellik Mühendisliği

Alfabetik olmayan karakterleri kaldırma

...
...
# 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'
Python ile NLP için Özellik Mühendisliği

Stopwords

  • Çok sık geçen kelimeler
  • Örn. artikeller, olmak fiilleri, zamirler vb.
Python ile NLP için Özellik Mühendisliği

spaCy ile stopwords kaldırma

# 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. ❤ """
Python ile NLP için Özellik Mühendisliği

spaCy ile stopwords kaldırma

...
...
# 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'
Python ile NLP için Özellik Mühendisliği

Diğer metin ön işleme teknikleri

  • HTML/XML etiketlerini kaldırma
  • Aksanlı karakterleri değiştirme (ör. é)
  • Yazım hatalarını düzeltme
Python ile NLP için Özellik Mühendisliği

Dikkat

Uygulamanız için yalnızca gerekli metin ön işleme tekniklerini kullanın.

Python ile NLP için Özellik Mühendisliği

Haydi pratik yapalım!

Python ile NLP için Özellik Mühendisliği

Preparing Video For Download...