Preparare il testo per il modeling

Introduzione all'Elaborazione del Linguaggio Naturale in R

Kasey Jones

Research Data Scientist

Apprendimento supervisionato in R: classificazione

Introduzione all'Elaborazione del Linguaggio Naturale in R

Modelli di classificazione

  • approccio di apprendimento supervisionato
  • classifica le osservazioni in categorie
    • vittoria/sconfitta
    • pericoloso, amichevole o indifferente
  • può usare diverse tecniche:
    • regressione logistica
    • alberi decisionali/random forest/xgboost
    • reti neurali
    • ecc.
Introduzione all'Elaborazione del Linguaggio Naturale in R

Passi base del modeling

  1. Pulisci/prepara i dati
  2. Crea training e test set
  3. Allena un modello sul training set
  4. Riporta l'accuratezza sul test set
Introduzione all'Elaborazione del Linguaggio Naturale in R

Riconoscimento dei personaggi

Napoloeon Napoleone

Boxer Boxer

1 https://comicvine.gamespot.com/napoleon/4005-141035/ 2 https://hero.fandom.com/wiki/Boxer_(Animal_Farm)
Introduzione all'Elaborazione del Linguaggio Naturale in R

Frasi sugli animali

# Make sentences
sentences <- animal_farm %>%
  unnest_tokens(output = "sentence", token = "sentences", input = text_column)
# Label sentences by animal
sentences$boxer <- grepl('boxer', sentences$sentence)
sentences$napoleon <- grepl('napoleon', sentences$sentence)
# Replace the animal name
sentences$sentence <- gsub("boxer", "animal X", sentences$sentence)
sentences$sentence <- gsub("napoleon", "animal X", sentences$sentence)
animal_sentences <- sentences[sentences$boxer + sentences$napoleon == 1, ]
Introduzione all'Elaborazione del Linguaggio Naturale in R

Frasi (continua)

animal_sentences$Name <-
    as.factor(ifelse(animal_sentences$boxer, "boxer", "napoleon"))
# 75 of each
animal_sentences <- 
  rbind(animal_sentences[animal_sentences$Name == "boxer", ][c(1:75), ],
        animal_sentences[animal_sentences$Name == "napoleon", ][c(1:75), ])
animal_sentences$sentence_id <- c(1:dim(animal_sentences)[1])
Introduzione all'Elaborazione del Linguaggio Naturale in R

Prepara i dati

library(tm); library(tidytext)
library(dplyr); library(SnowballC)
animal_tokens <- animal_sentences %>%
  unnest_tokens(output = "word", token = "words", input = sentence) %>%
  anti_join(stop_words) %>%
  mutate(word = wordStem(word))
Introduzione all'Elaborazione del Linguaggio Naturale in R

Preparazione (continua)

animal_matrix <- animal_tokens %>%
  count(sentence_id, word) %>%
  cast_dtm(document = sentence_id, term = word,
           value = n, weighting = tm::weightTfIdf)
animal_matrix
<<DocumentTermMatrix (documents: 150, terms: 694)>>
Non-/sparse entries: 1235/102865
Sparsity           : 99%
Maximal term length: 17
Weighting          : term frequency - inverse document frequency
Introduzione all'Elaborazione del Linguaggio Naturale in R

Rimuovere termini rari

  • Non vuote (1.235) + vuote (102.865)
  • Dimensioni matrice 150 × 694
  • Sparsità: 102.865 / 104.100 (99%)

Soluzione: removeSparseTerms()

Introduzione all'Elaborazione del Linguaggio Naturale in R

Quanto rara è troppo rara?

removeSparseTerms(animal_matrix, sparse = .90)
<<DocumentTermMatrix (documents: 150, terms: 4)>>
Non-/sparse entries: 207/393
Sparsity           : 66%
removeSparseTerms(animal_matrix, sparse = .99)
removeSparseTerms(animal_matrix, sparse = .99)
<<DocumentTermMatrix (documents: 150, terms: 172)>>
Non-/sparse entries: 713/25087
Sparsity           : 97%
Introduzione all'Elaborazione del Linguaggio Naturale in R

Ayo berlatih!

Introduzione all'Elaborazione del Linguaggio Naturale in R

Preparing Video For Download...