Quanteda Dictionaries

Using Quanteda's sentiment functions.

By Monty Reynolds

January 25, 2021

Quanteda Dictionaries

Next, we will install the quanteda textual analysis set of packages.

These include:

  • quanteda: contains all of the core natural language processing and textual data management functions
  • quanteda.textmodels: contains all of the text models and supporting functions, namely the textmodel_*() functions. This was split from the main package with the v2 release
  • quanteda.textstats: statistics for textual data, namely the textstat_*() functions, split with the v3 release
  • quanteda.textplots: plots for textual data, namely the textplot_*() functions, split with the v3 release

For this, there are several packages we will install:

devtools::install_github("hadley/devtools")

devtools::install_github("quanteda/quanteda") 

devtools::install_github("kbenoit/quanteda.dictionaries") 

devtools::install_github("quanteda/quanteda.corpora")

  1. Install
devtools::install_github("kbenoit/quanteda.dictionaries")

  1. Load packages
library(quanteda.dictionaries)
library(quanteda)
library(quanteda.textmodels)
library(quanteda.textstats)
library(quanteda.textplots)
library(readtext)
library(devtools)
library(quanteda.corpora)
library(newsmap)
library(seededlda)
library(quanteda.sentiment)
library(quanteda.dictionaries)

  1. Apply corpus and sentiment dictionary and view data
output_lsd <- liwcalike(quanteda.corpora::data_corpus_movies, 
                        dictionary = data_dictionary_NRC)
                        
output_lsd
if (!require("pacman")) install.packages("pacman")
pacman::p_load_current_gh("trinker/lexicon", "trinker/sentimentr")


if (!require("pacman")) install.packages("pacman")
pacman::p_load(sentimentr, dplyr, magrittr)

mytext <- c(
    'do you like it?  But I hate really bad dogs',
    'I am the best friend.',
    'Do you really like it?  I\'m not a fan'
)

mytext <- get_sentences(mytext)
sentiment(mytext)
mytext <- c(
    'do you like it?  But I hate really bad dogs',
    'I am the best friend.',
    'Do you really like it?  I\'m not a fan'
)

mytext <- get_sentences(mytext)
sentiment(mytext)

Group by Elements

We can also group by elements:

mytext <- c(
    'do you like it?  But I hate really bad dogs',
    'I am the best friend.',
    'Do you really like it?  I\'m not a fan'
)
mytext <- get_sentences(mytext)
sentiment_by(mytext)