Skip to content

Instantly share code, notes, and snippets.

@trinker
Created August 3, 2017 15:39
Show Gist options
  • Select an option

  • Save trinker/d9a13874ca78b5a3a8e1a1a82096e28d to your computer and use it in GitHub Desktop.

Select an option

Save trinker/d9a13874ca78b5a3a8e1a1a82096e28d to your computer and use it in GitHub Desktop.
nrc emotions analysis
## installing and loading dependencies
if (!require("pacman")) install.packages("pacman")
pacman::p_load_current_gh(
file.path('trinker', c(
"gofastr",
"textshape",
"lexicon",
"textclean",
"termco"
))
)
pacman::p_load(magrittr)
## the data to tag
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"
)
## make the nrc tokens
token_list <- lexicon::nrc_emotions %>%
textshape::column_to_rownames() %>%
t() %>%
textshape::as_list()
## the procedure
counts <- mytext %>%
textshape::split_sentence() %>%
textshape::tidy_list('element', 'text') %$%
termco::token_count(text, TRUE, token_list)
counts
## Coverage: 40%
## # A tibble: 5 x 10
## id n.tokens anger anticipation disgust fear joy sadness surprise trust
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 5 0 0 0 0 0 0 0 0
## 2 2 6 2 0 2 2 0 2 0 0
## 3 3 6 0 0 0 0 1 0 0 1
## 4 4 6 0 0 0 0 0 0 0 0
## 5 5 4 0 0 0 0 0 0 0 0
counts %>%
dplyr::select(anger:trust) %>%
textshape::as_list()
mytext %>%
textshape::split_sentence() %>%
textshape::tidy_list('element', 'text') %$%
termco::token_count(text, element, token_list)
## Coverage: 66.67%
## # A tibble: 3 x 10
## element n.tokens anger anticipation disgust fear joy sadness surprise trust
## <fctr> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 1 11 2(18.18%) 0 2(18.18%) 2(18.18%) 0 2(18.18%) 0 0
## 2 2 6 0 0 0 0 1(16.67%) 0 0 1(16.67%)
## 3 3 10 0 0 0 0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment