tidy_net_sentiment_nrc.Rd
Order result of calc_net_sentiment_nrc
by the
count of one or more NRC sentiments, and convert to "tidy" format.
tidy_net_sentiment_nrc( net_sentiment_wide_nrc, sorting_sentiments = "anger", num_of_docs = 60 )
net_sentiment_wide_nrc | A data frame/tibble derived from
|
---|---|
sorting_sentiments | A string of vector of strings with the NRC
sentiments (Mohammad & Turney, 2013) by which to sort the returned table.
(To see all NRC sentiments run |
num_of_docs | The number of document IDs for which to return results. See details. |
A data frame in long format with four columns: the text; the line number; the NRC sentiment; and the count of the NRC sentiment.
The returned data frame will have, for each document (or class- see
filter_class
in calc_net_sentiment_nrc
), as many rows
as the NRC sentiments with non-zero counts (remember- this function
returns the result in long format). If all sentiments have non-zero
counts in a given document, then there will be 10 rows (as many as all
the NRC sentiments) in the returned data frame for this document.
Argument num_of_docs
returns the first num_of_docs
documents
(or classes, see filter_class
in calc_net_sentiment_nrc
),
with the top one being the one that has the highest count(s) of the NRC
sentiment(s) specified in sorting_sentiments
.
Mohammad S.M. & Turney P.D. (2013). Crowdsourcing a Word–Emotion Association Lexicon. Computational Intelligence, 29(3):436-465.
library(experienceAnalysis) books <- janeaustenr::austen_books() # Jane Austen books emma <- paste(books[books$book == "Emma", ], collapse = " ") # String with whole book pp <- paste(books[books$book == "Pride & Prejudice", ], collapse = " ") # String with whole book # Make data frame with books Emma and Pride & Prejudice x <- data.frame( text = c(pp, emma), book = c("Pride & Prejudice", "Emma") ) # Net sentiment in each book net_sentiment_wide_nrc <- calc_net_sentiment_nrc(x, target_col_name = "book", text_col_name = "text", filter_class = NULL) net_sentiment_wide_nrc#> # A tibble: 2 x 13 #> text linenumber anger anticipation disgust fear joy negative positive #> <chr> <int> <int> <int> <int> <int> <int> <int> <int> #> 1 "c(\"PRID~ 1 1295 3596 973 1767 3336 3641 7443 #> 2 "c(\"EMMA~ 2 1548 4821 1323 2343 4432 4473 9471 #> # ... with 4 more variables: sadness <int>, surprise <int>, trust <int>, #> # book <chr># Tidy net_sentiment_wide_nrc and place the most "angry" book at the top tidy_net_sentiment_nrc(net_sentiment_wide_nrc, sorting_sentiments = "anger", num_of_docs = 2) %>% dplyr::select(-text)#> # A tibble: 20 x 4 #> linenumber book name value #> <fct> <chr> <fct> <int> #> 1 2 Emma anger 1548 #> 2 2 Emma anticipation 4821 #> 3 2 Emma disgust 1323 #> 4 2 Emma fear 2343 #> 5 2 Emma joy 4432 #> 6 2 Emma negative 4473 #> 7 2 Emma positive 9471 #> 8 2 Emma sadness 2253 #> 9 2 Emma surprise 2220 #> 10 2 Emma trust 5613 #> 11 1 Pride & Prejudice anger 1295 #> 12 1 Pride & Prejudice anticipation 3596 #> 13 1 Pride & Prejudice disgust 973 #> 14 1 Pride & Prejudice fear 1767 #> 15 1 Pride & Prejudice joy 3336 #> 16 1 Pride & Prejudice negative 3641 #> 17 1 Pride & Prejudice positive 7443 #> 18 1 Pride & Prejudice sadness 1755 #> 19 1 Pride & Prejudice surprise 1665 #> 20 1 Pride & Prejudice trust 4180# Can sort by multiple sentiments too: as above, but for most "angry" and then for most "negative" tidy_net_sentiment_nrc(net_sentiment_wide_nrc, sorting_sentiments = c("anger", "negative"), num_of_docs = 2) %>% dplyr::select(-text)#> # A tibble: 20 x 4 #> linenumber book name value #> <fct> <chr> <fct> <int> #> 1 2 Emma anger 1548 #> 2 2 Emma anticipation 4821 #> 3 2 Emma disgust 1323 #> 4 2 Emma fear 2343 #> 5 2 Emma joy 4432 #> 6 2 Emma negative 4473 #> 7 2 Emma positive 9471 #> 8 2 Emma sadness 2253 #> 9 2 Emma surprise 2220 #> 10 2 Emma trust 5613 #> 11 1 Pride & Prejudice anger 1295 #> 12 1 Pride & Prejudice anticipation 3596 #> 13 1 Pride & Prejudice disgust 973 #> 14 1 Pride & Prejudice fear 1767 #> 15 1 Pride & Prejudice joy 3336 #> 16 1 Pride & Prejudice negative 3641 #> 17 1 Pride & Prejudice positive 7443 #> 18 1 Pride & Prejudice sadness 1755 #> 19 1 Pride & Prejudice surprise 1665 #> 20 1 Pride & Prejudice trust 4180