Difference between revisions of "R: sentiments analysis"

From OnnoWiki
Jump to navigation Jump to search
Line 29: Line 29:
 
     inner_join(nrcjoy) %>%
 
     inner_join(nrcjoy) %>%
 
     count(word, sort = TRUE)
 
     count(word, sort = TRUE)
 +
 +
 +
library(tidyr)
 +
janeaustensentiment <- tidy_books %>%
 +
    inner_join(get_sentiments("bing")) %>%
 +
    count(book, index = linenumber %/% 80, sentiment) %>%
 +
    spread(sentiment, n, fill = 0) %>%
 +
    mutate(sentiment = positive - negative)
 +
 +
library(ggplot2)
 +
ggplot(janeaustensentiment, aes(index, sentiment, fill = book)) +
 +
    geom_col(show.legend = FALSE) +
 +
    facet_wrap(~book, ncol = 2, scales = "free_x")
 +
 +
  
  

Revision as of 17:01, 8 November 2018


library(tidytext)
sentiments


get_sentiments("afinn")
get_sentiments("bing")
get_sentiments("nrc")


library(janeaustenr)
library(dplyr)
library(stringr)
tidy_books <- austen_books() %>%
    group_by(book) %>%
    mutate(linenumber = row_number(),
           chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
           ignore_case = TRUE)))) %>%
           ungroup() %>%
    unnest_tokens(word, text)


nrcjoy <- get_sentiments("nrc") %>%
    filter(sentiment == "joy")
tidy_books %>%
    filter(book == "Emma") %>%
    inner_join(nrcjoy) %>%
    count(word, sort = TRUE)


library(tidyr)
janeaustensentiment <- tidy_books %>%
    inner_join(get_sentiments("bing")) %>%
    count(book, index = linenumber %/% 80, sentiment) %>%
    spread(sentiment, n, fill = 0) %>%
    mutate(sentiment = positive - negative)
library(ggplot2)
ggplot(janeaustensentiment, aes(index, sentiment, fill = book)) +
    geom_col(show.legend = FALSE) +
    facet_wrap(~book, ncol = 2, scales = "free_x")




Pranala Menarik