Créer des modèles n-grammes

Feature Engineering pour le NLP en Python

Rounak Banik

Data Scientist

Limites du BoW

review label
'The movie was good and not boring' positive
'The movie was not good and boring' negative

 

  • Représentation BoW strictement identique !
  • Le contexte des mots est perdu.
  • Le sentiment dépend de la position de « not ».
Feature Engineering pour le NLP en Python

n-grammes

  • Suite contiguë de n éléments (ou mots) dans un document.
  • n = 1 → bag-of-words
    'for you a thousand times over'
    
  • n = 2, n-grammes :
    [
    'for you',
    'you a',
    'a thousand',
    'thousand times',
    'times over'
    ]
    
Feature Engineering pour le NLP en Python

n-grammes

'for you a thousand times over'
  • n = 3, n-grammes :
    [
    'for you a',
    'you a thousand',
    'a thousand times',
    'thousand times over'
    ]
    
  • Capture davantage de contexte.
Feature Engineering pour le NLP en Python

Applications

  • Complétion de phrase
  • Correction orthographique
  • Correction en traduction automatique
Feature Engineering pour le NLP en Python

Créer des modèles n-grammes avec scikit-learn

Génère uniquement des bigrammes.

bigrams = CountVectorizer(ngram_range=(2,2))

Génère des unigrams, bigrammes et trigrammes.

ngrams = CountVectorizer(ngram_range=(1,3))
Feature Engineering pour le NLP en Python

Limites

  • Malédiction de la dimensionnalité
  • Les n-grammes d'ordre élevé sont rares
  • Gardez n petit
Feature Engineering pour le NLP en Python

Passons à la pratique !

Feature Engineering pour le NLP en Python

Preparing Video For Download...