Creare modelli n-gram

Feature Engineering per NLP in Python

Rounak Banik

Data Scientist

Limiti di BoW

review label
'The movie was good and not boring' positiva
'The movie was not good and boring' negativa

 

  • Rappresentazione BoW identica!
  • Si perde il contesto delle parole.
  • Il sentiment dipende dalla posizione di 'not'.
Feature Engineering per NLP in Python

n-gram

  • Sequenza contigua di n elementi (o parole) in un documento.
  • n = 1 → bag-of-words
    'for you a thousand times over'
    
  • n = 2, n-gram:
    [
    'for you',
    'you a',
    'a thousand',
    'thousand times',
    'times over'
    ]
    
Feature Engineering per NLP in Python

n-gram

'for you a thousand times over'
  • n = 3, n-gram:
    [
    'for you a',
    'you a thousand',
    'a thousand times',
    'thousand times over'
    ]
    
  • Cattura più contesto.
Feature Engineering per NLP in Python

Applicazioni

  • Completamento frasi
  • Correzione ortografica
  • Correzione traduzioni automatiche
Feature Engineering per NLP in Python

Creare modelli n-gram con scikit-learn

Genera solo bigrammi.

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

Genera unigrammi, bigrammi e trigrammi.

ngrams = CountVectorizer(ngram_range=(1,3))
Feature Engineering per NLP in Python

Limiti

  • Maledizione della dimensionalità
  • n-gram di ordine alto sono rari
  • Tieni n piccolo
Feature Engineering per NLP in Python

Esercitiamoci!

Feature Engineering per NLP in Python

Preparing Video For Download...