-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentiment words analysis.Rmd
67 lines (55 loc) · 1.72 KB
/
Sentiment words analysis.Rmd
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
---
title: "Sentiment words analysis"
output: word_document
---
```{r}
if (!require(tidytext)) {install.packages("tidytext")}
if (!require(ggplot2)) {install.packages("ggplot2")}
if (!require(dplyr)) {install.packages("dplyr")}
```
read zomato and swiggy files
```{r read files}
zomato <- readLines('data/zomato.txt')
swiggy <- readLines("data/swiggy.txt")
```
function to return bing sentiment words
```{r}
get_positive_negative_word_counts <- function(corpus) {
df = data_frame(text = zomato) #create dataframe from corpus
bing_sentiments = get_sentiments("bing")
word_counts <- df %>%
unnest_tokens(word, text) %>%
inner_join(bing_sentiments) %>%
count(word, sentiment, sort = TRUE) %>%
ungroup()
return (word_counts)
}
```
bing sentiments add positive and negative scores
Based on those score contribution
```{r}
swiggy_word_counts <- get_positive_negative_word_counts(swiggy)
swiggy_word_counts %>%
filter(n > 20) %>%
mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = sentiment)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
xlab("Words") +
ylab("Count of words") +
ggtitle("Words contributing towards Swiggy sentiment")
```
```{r}
zomato_word_counts <- get_positive_negative_word_counts(zomato)
zomato_word_counts %>%
filter(n > 20) %>%
mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = sentiment)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
xlab("Words") +
ylab("Count of words") +
ggtitle("Words contributing towards Zomato sentiment")
```