Nettoyage de texte

Feature Engineering pour le NLP en Python

Rounak Banik

Data Scientist

Techniques de nettoyage de texte

  • Espaces superflus et séquences d'échappement
  • Ponctuation
  • Caractères spéciaux (chiffres, emojis, etc.)
  • Mots vides
Feature Engineering pour le NLP en Python

isalpha()

"Dog".isalpha()
True
"3dogs".isalpha()
False
"12347".isalpha()
False
"!".isalpha()
False
"❤".isalpha()
False
Feature Engineering pour le NLP en Python

Attention

  • Abréviations : U.S.A, U.K, etc.
  • Noms propres : word2vec et xto10x.
  • Écrire votre propre fonction (avec regex) pour les cas plus subtils.
Feature Engineering pour le NLP en Python

Suppression des caractères non alphabétiques

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 pour le NLP en Python

Suppression des caractères non alphabétiques

...
...
# 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 pour le NLP en Python

Mots vides

  • Mots extrêmement fréquents
  • Ex. : articles, verbes « be », pronoms, etc.
Feature Engineering pour le NLP en Python

Supprimer les mots vides avec 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 pour le NLP en Python

Supprimer les mots vides avec 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 pour le NLP en Python

Autres techniques de prétraitement

  • Suppression des balises HTML/XML
  • Remplacement des caractères accentués (comme é)
  • Correction des fautes d'orthographe
Feature Engineering pour le NLP en Python

Attention

N'appliquez que les techniques de prétraitement pertinentes pour votre cas d'usage.

Feature Engineering pour le NLP en Python

Passons à la pratique !

Feature Engineering pour le NLP en Python

Preparing Video For Download...