-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNegative Sentiment Network.R
76 lines (53 loc) · 1.99 KB
/
Negative Sentiment Network.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
library(textdata)
library(openxlsx)
library(widyr)
library(readxl)
library(tidytext)
library(dplyr)
library(tidyr)
library(ggplot2)
library(stringr)
library(tm)
library(igraph)
library(ggraph)
## pakages
## data importing
data <- read_excel("C:/Users/KraCo0P/OneDrive/Desktop/trash/new/data/partial ready data/Reviews.xlsx")
stp_words <- get_stopwords()
data_new <- data %>%
unnest_tokens(output = word,input = Review) %>%
anti_join(stop_words, by = "word") %>%
filter(str_detect(word, "[:alpha:]"))%>%
distinct()
## sentiment grading
afinn <- get_sentiments("afinn")
word_sentiment <- data_new %>%
inner_join(afinn, by ='word')
# Define function to generate word graph
generate_word_graph <- function(data_new,
minimum_users_n = 50,
minimum_correlation = 0.2,
graph_title = "Positive Sentiment Word Network") {
user_who_mentioned_word <- data_new %>%
count(word, name = "users_n") %>%
filter(users_n >= minimum_users_n)
word_correlations <- data_new %>%
semi_join(user_who_mentioned_word, by = "word") %>%
pairwise_cor(item = word, feature = user_id) %>%
filter(correlation >= minimum_correlation)
graph_from_data_frame(d = word_correlations,
vertices = user_who_mentioned_word %>%
semi_join(word_correlations, by = c('word' = 'item1'))) %>%
ggraph(layout = 'fr') +
geom_edge_link(aes(alpha = correlation)) +
geom_node_point() +
geom_node_text(aes(color = users_n, label = name), repel = TRUE) +
ggtitle(graph_title)+
theme(plot.title = element_text(hjust = 0.5))
}
data_new.NEGATIVE <- word_sentiment %>%
filter(value < 1 )
data_new.NEGATIVE %>%
generate_word_graph(minimum_users_n = 5,
minimum_correlation = 0.14,
graph_title = "Negative Sentiment Word Network")