Preparing text for modeling

Introduction to Natural Language Processing in R

Kasey Jones

Research Data Scientist

Supervised learning in R: classification

Introduction to Natural Language Processing in R

Classification modeling

  • supervised learning approach
  • classifies observations into categories
    • win/loss
    • dangerous, friendly, or indifferent
  • can use a number of different techniques:
    • logistic regression
    • decision trees/random forest/xgboost
    • neural networks
    • etc.
Introduction to Natural Language Processing in R

Modeling basics steps

  1. Clean/prepare data
  2. Create training and testing datasets
  3. Train a model on the training dataset
  4. Report accuracy on the testing dataset
Introduction to Natural Language Processing in R

Character recognition

Napoloeon Napoleon

Boxer Boxer

1 https://comicvine.gamespot.com/napoleon/4005-141035/ 2 https://hero.fandom.com/wiki/Boxer_(Animal_Farm)
Introduction to Natural Language Processing in R

Animal sentences

# 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, ]
Introduction to Natural Language Processing in R

Sentences continued

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])
Introduction to Natural Language Processing in R

Prepare the data

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))
Introduction to Natural Language Processing in R

Preparation continued

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
Introduction to Natural Language Processing in R

Remove sparse terms

  • Non-empty (1,235) + empty (102,865)
  • Matrix dimensions 150 * 694
  • Sparsity: 102,865 / 104,100 (99%)

Solution: removeSparseTerms()

Introduction to Natural Language Processing in R

How sparse is too sparse?

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%
Introduction to Natural Language Processing in R

Let's practice!

Introduction to Natural Language Processing in R

Preparing Video For Download...