Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. library(dplyr)
  2. library(tidyr)
  3. library(purrr)
  4. library(readr)
  5. library(gutenbergr)
  6. library(tidytext)
  7. library(ggplot2)
  8. #a---------
  9. komen1 <- read_file(file.choose()) #pilih komen1
  10. komen2 <- read_file(file.choose()) #pilih komen2
  11. komen3 <- read_file(file.choose()) #pilih komen3
  12. komen4 <- read_file(file.choose()) #pilih komen4
  13.  
  14. #b------
  15. komen1_df <- tibble(line=1:1,text = komen1)
  16. komen1_text <- komen1_df %>% unnest_tokens(word, text)
  17.  
  18. komen2_df <- tibble(line=1:1,text = komen2)
  19. komen2_text <- komen2_df %>% unnest_tokens(word, text)
  20.  
  21. komen3_df <- tibble(line=1:1,text = komen3)
  22. komen3_text <- komen3_df %>% unnest_tokens(word, text)
  23.  
  24. komen4_df <- tibble(line=1:1,text = komen4)
  25. komen4_text <- komen4_df %>% unnest_tokens(word, text)
  26. #c--------
  27. data(stop_words)
  28.  
  29. komen1_clean <- komen1_text %>%
  30. anti_join(stop_words,by="word")
  31.  
  32. komen1_clean %>%
  33. count(word, sort = TRUE) %>%
  34. filter(n > 1) %>%
  35. mutate(word = reorder(word, n)) %>%
  36. ggplot(aes(word, n)) +
  37. geom_col() +
  38. xlab(NULL) +
  39. coord_flip()
  40.  
  41. komen2_clean <- komen2_text %>%
  42. anti_join(stop_words,by="word")
  43. komen2_clean %>%
  44. count(word, sort = TRUE) %>%
  45. filter(n > 1) %>%
  46. mutate(word = reorder(word, n)) %>%
  47. ggplot(aes(word, n)) +
  48. geom_col() +
  49. xlab(NULL) +
  50. coord_flip()
  51.  
  52. komen3_clean <- komen3_text %>%
  53. anti_join(stop_words,by="word")
  54. komen3_clean %>%
  55. count(word, sort = TRUE) %>%
  56. filter(n > 1) %>%
  57. mutate(word = reorder(word, n)) %>%
  58. ggplot(aes(word, n)) +
  59. geom_col() +
  60. xlab(NULL) +
  61. coord_flip()
  62.  
  63. komen4_clean <- komen4_text %>%
  64. anti_join(stop_words,by="word")
  65. komen4_clean %>%
  66. count(word, sort = TRUE) %>%
  67. filter(n > 1) %>%
  68. mutate(word = reorder(word, n)) %>%
  69. ggplot(aes(word, n)) +
  70. geom_col() +
  71. xlab(NULL) +
  72. coord_flip()
  73.  
  74.  
  75. #d-----
  76. #Komen1
  77. komen1_sentiment <- komen1_clean %>%
  78. inner_join(get_sentiments("bing"),by="word") %>%
  79. count(word, index = line, sentiment) %>%
  80. spread(sentiment, n, fill = 0) %>%
  81. mutate(sentiment = positive - negative)
  82.  
  83. komen1_sentiment
  84.  
  85. komen1_counts <- komen1_clean %>%
  86. inner_join(get_sentiments("bing")) %>%
  87. count(word, sentiment, sort = TRUE) %>%
  88. ungroup()
  89.  
  90. komen1_counts
  91.  
  92. komen1_counts %>%
  93. group_by(sentiment) %>%
  94. top_n(3) %>%
  95. ungroup() %>%
  96. mutate(word = reorder(word, n)) %>%
  97. ggplot(aes(word, n, fill = sentiment)) +
  98. geom_col(show.legend = FALSE) +
  99. facet_wrap(~sentiment, scales = "free_y") +
  100. labs(y = "Contribution to sentiment",
  101. x = NULL) +
  102. coord_flip()
  103.  
  104. #Komen2
  105. komen2_sentiment <- komen2_clean %>%
  106. inner_join(get_sentiments("bing"),by="word") %>%
  107. count(word, index = line, sentiment) %>%
  108. spread(sentiment, n, fill = 0) %>%
  109. mutate(sentiment = positive - negative)
  110.  
  111. komen2_sentiment
  112.  
  113. komen2_counts <- komen2_clean %>%
  114. inner_join(get_sentiments("bing")) %>%
  115. count(word, sentiment, sort = TRUE) %>%
  116. ungroup()
  117.  
  118. komen2_counts
  119.  
  120. komen2_counts %>%
  121. group_by(sentiment) %>%
  122. top_n(5) %>%
  123. ungroup() %>%
  124. mutate(word = reorder(word, n)) %>%
  125. ggplot(aes(word, n, fill = sentiment)) +
  126. geom_col(show.legend = FALSE) +
  127. facet_wrap(~sentiment, scales = "free_y") +
  128. labs(y = "Contribution to sentiment",
  129. x = NULL) +
  130. coord_flip()
  131.  
  132. #Komen3
  133. komen3_sentiment <- komen3_clean %>%
  134. inner_join(get_sentiments("bing"),by="word") %>%
  135. count(word, index = line, sentiment) %>%
  136. spread(sentiment, n, fill = 0) %>%
  137. mutate(sentiment = positive - negative)
  138.  
  139. komen3_sentiment
  140.  
  141. komen3_counts <- komen3_clean %>%
  142. inner_join(get_sentiments("bing")) %>%
  143. count(word, sentiment, sort = TRUE) %>%
  144. ungroup()
  145.  
  146. komen3_counts
  147.  
  148. komen3_counts %>%
  149. group_by(sentiment) %>%
  150. top_n(4) %>%
  151. ungroup() %>%
  152. mutate(word = reorder(word, n)) %>%
  153. ggplot(aes(word, n, fill = sentiment)) +
  154. geom_col(show.legend = FALSE) +
  155. facet_wrap(~sentiment, scales = "free_y") +
  156. labs(y = "Contribution to sentiment",
  157. x = NULL) +
  158. coord_flip()
  159.  
  160. #Komen4
  161. komen4_sentiment <- komen4_clean %>%
  162. inner_join(get_sentiments("bing"),by="word") %>%
  163. count(word, index = line, sentiment) %>%
  164. spread(sentiment, n, fill = 0) %>%
  165. mutate(sentiment = positive - negative)
  166.  
  167. komen4_sentiment
  168.  
  169. komen4_counts <- komen4_clean %>%
  170. inner_join(get_sentiments("bing")) %>%
  171. count(word, sentiment, sort = TRUE) %>%
  172. ungroup()
  173.  
  174. komen4_counts
  175.  
  176. komen4_counts %>%
  177. group_by(sentiment) %>%
  178. top_n(5) %>%
  179. ungroup() %>%
  180. mutate(word = reorder(word, n)) %>%
  181. ggplot(aes(word, n, fill = sentiment)) +
  182. geom_col(show.legend = FALSE) +
  183. facet_wrap(~sentiment, scales = "free_y") +
  184. labs(y = "Contribution to sentiment",
  185. x = NULL) +
  186. coord_flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement