Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(dplyr)
- library(tidyr)
- library(purrr)
- library(readr)
- library(gutenbergr)
- library(tidytext)
- library(ggplot2)
- #a---------
- komen1 <- read_file(file.choose()) #pilih komen1
- komen2 <- read_file(file.choose()) #pilih komen2
- komen3 <- read_file(file.choose()) #pilih komen3
- komen4 <- read_file(file.choose()) #pilih komen4
- #b------
- komen1_df <- tibble(line=1:1,text = komen1)
- komen1_text <- komen1_df %>% unnest_tokens(word, text)
- komen2_df <- tibble(line=1:1,text = komen2)
- komen2_text <- komen2_df %>% unnest_tokens(word, text)
- komen3_df <- tibble(line=1:1,text = komen3)
- komen3_text <- komen3_df %>% unnest_tokens(word, text)
- komen4_df <- tibble(line=1:1,text = komen4)
- komen4_text <- komen4_df %>% unnest_tokens(word, text)
- #c--------
- data(stop_words)
- komen1_clean <- komen1_text %>%
- anti_join(stop_words,by="word")
- komen1_clean %>%
- count(word, sort = TRUE) %>%
- filter(n > 1) %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n)) +
- geom_col() +
- xlab(NULL) +
- coord_flip()
- komen2_clean <- komen2_text %>%
- anti_join(stop_words,by="word")
- komen2_clean %>%
- count(word, sort = TRUE) %>%
- filter(n > 1) %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n)) +
- geom_col() +
- xlab(NULL) +
- coord_flip()
- komen3_clean <- komen3_text %>%
- anti_join(stop_words,by="word")
- komen3_clean %>%
- count(word, sort = TRUE) %>%
- filter(n > 1) %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n)) +
- geom_col() +
- xlab(NULL) +
- coord_flip()
- komen4_clean <- komen4_text %>%
- anti_join(stop_words,by="word")
- komen4_clean %>%
- count(word, sort = TRUE) %>%
- filter(n > 1) %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n)) +
- geom_col() +
- xlab(NULL) +
- coord_flip()
- #d-----
- #Komen1
- komen1_sentiment <- komen1_clean %>%
- inner_join(get_sentiments("bing"),by="word") %>%
- count(word, index = line, sentiment) %>%
- spread(sentiment, n, fill = 0) %>%
- mutate(sentiment = positive - negative)
- komen1_sentiment
- komen1_counts <- komen1_clean %>%
- inner_join(get_sentiments("bing")) %>%
- count(word, sentiment, sort = TRUE) %>%
- ungroup()
- komen1_counts
- komen1_counts %>%
- group_by(sentiment) %>%
- top_n(3) %>%
- ungroup() %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n, fill = sentiment)) +
- geom_col(show.legend = FALSE) +
- facet_wrap(~sentiment, scales = "free_y") +
- labs(y = "Contribution to sentiment",
- x = NULL) +
- coord_flip()
- #Komen2
- komen2_sentiment <- komen2_clean %>%
- inner_join(get_sentiments("bing"),by="word") %>%
- count(word, index = line, sentiment) %>%
- spread(sentiment, n, fill = 0) %>%
- mutate(sentiment = positive - negative)
- komen2_sentiment
- komen2_counts <- komen2_clean %>%
- inner_join(get_sentiments("bing")) %>%
- count(word, sentiment, sort = TRUE) %>%
- ungroup()
- komen2_counts
- komen2_counts %>%
- group_by(sentiment) %>%
- top_n(5) %>%
- ungroup() %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n, fill = sentiment)) +
- geom_col(show.legend = FALSE) +
- facet_wrap(~sentiment, scales = "free_y") +
- labs(y = "Contribution to sentiment",
- x = NULL) +
- coord_flip()
- #Komen3
- komen3_sentiment <- komen3_clean %>%
- inner_join(get_sentiments("bing"),by="word") %>%
- count(word, index = line, sentiment) %>%
- spread(sentiment, n, fill = 0) %>%
- mutate(sentiment = positive - negative)
- komen3_sentiment
- komen3_counts <- komen3_clean %>%
- inner_join(get_sentiments("bing")) %>%
- count(word, sentiment, sort = TRUE) %>%
- ungroup()
- komen3_counts
- komen3_counts %>%
- group_by(sentiment) %>%
- top_n(4) %>%
- ungroup() %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n, fill = sentiment)) +
- geom_col(show.legend = FALSE) +
- facet_wrap(~sentiment, scales = "free_y") +
- labs(y = "Contribution to sentiment",
- x = NULL) +
- coord_flip()
- #Komen4
- komen4_sentiment <- komen4_clean %>%
- inner_join(get_sentiments("bing"),by="word") %>%
- count(word, index = line, sentiment) %>%
- spread(sentiment, n, fill = 0) %>%
- mutate(sentiment = positive - negative)
- komen4_sentiment
- komen4_counts <- komen4_clean %>%
- inner_join(get_sentiments("bing")) %>%
- count(word, sentiment, sort = TRUE) %>%
- ungroup()
- komen4_counts
- komen4_counts %>%
- group_by(sentiment) %>%
- top_n(5) %>%
- ungroup() %>%
- mutate(word = reorder(word, n)) %>%
- ggplot(aes(word, n, fill = sentiment)) +
- geom_col(show.legend = FALSE) +
- facet_wrap(~sentiment, scales = "free_y") +
- labs(y = "Contribution to sentiment",
- x = NULL) +
- coord_flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement