Limpeza de texto

Feature Engineering para NLP em Python

Rounak Banik

Data Scientist

Técnicas de limpeza de texto

  • Espaços extras e sequências de escape
  • Pontuação
  • Caracteres especiais (números, emojis etc.)
  • Stopwords
Feature Engineering para NLP em Python

isalpha()

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

Um alerta

  • Abreviações: U.S.A, U.K etc.
  • Nomes próprios: word2vec e xto10x.
  • Para casos mais sutis, escreva sua própria função (com regex).
Feature Engineering para NLP em Python

Removendo caracteres não alfabéticos

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 para NLP em Python

Removendo caracteres não alfabéticos

...
...
# 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 para NLP em Python

Stopwords

  • Palavras muito frequentes
  • Ex.: artigos, verbos auxiliares, pronomes etc.
Feature Engineering para NLP em Python

Removendo stopwords com 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 para NLP em Python

Removendo stopwords com 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 para NLP em Python

Outras técnicas de pré-processamento de texto

  • Remover tags HTML/XML
  • Substituir caracteres acentuados (como é)
  • Corrigir erros ortográficos
Feature Engineering para NLP em Python

Um alerta

Use apenas as técnicas de pré-processamento de texto relevantes para sua aplicação.

Feature Engineering para NLP em Python

Vamos praticar!

Feature Engineering para NLP em Python

Preparing Video For Download...