-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsentiment_extraction.R
More file actions
85 lines (69 loc) · 3.75 KB
/
Copy pathsentiment_extraction.R
File metadata and controls
85 lines (69 loc) · 3.75 KB
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
77
78
79
80
81
82
83
84
# https://towardsdatascience.com/understanding-and-writing-your-first-text-mining-script-with-r-c74a7efbe30f
# https://medium.com/swlh/exploring-sentiment-analysis-a6b53b026131
# https://cran.r-project.org/web/packages/syuzhet/vignettes/syuzhet-vignette.html
setwd("C:/Users/m/Documents/Python/PythonScripts/intro2ml_2021_final_project/Data")
# LIBRARIES ---------------------------------------------------------------
library(dplyr)
library(syuzhet)
library(tm)
library(wordcloud)
library(SnowballC)
# IMPORT RAW DATA ---------------------------------------------------------
dataset <- utils::read.csv("dataset_2k.csv", header = TRUE, sep = ",")
# TEXT MINING -------------------------------------------------------
# myStopwords <- c(generics::setdiff(tm::stopwords('english'), c("r", "big")),"use")
# dictCorpus <- dataset$tweet %>%
# tm::VectorSource() %>%
# tm::Corpus() %>%
# tm::tm_map(tm::content_transformer(tolower)) %>% # to lower case
# tm::tm_map(tm::content_transformer(gsub), #remove emojis
# pattern = "\\W", replace = " ") %>%
# tm::tm_map(tm::content_transformer( # remove URLs
# function(x) gsub("http[^[:space:]]*", "", x))) %>%
# tm::tm_map(tm::content_transformer( # remove anything other Eng letters/spaces
# function(x) gsub("[^[:alpha:][:space:]]*", "", x))) %>%
# tm::tm_map(tm::removeWords, tm::stopwords("english")) %>% # remove stopwords
# #tm::tm_map(tm::removeWords, myStopwords) %>% # remove custom stop words
# tm::tm_map(tm::stripWhitespace) %>% # remove extra spaces
# tm::tm_map(tm::removeNumbers) %>% # remove numbers
# tm::tm_map(tm::removePunctuation) # remove punctuation
#
# myCorpus <- dictCorpus %>% # stemming
# tm::tm_map(tm::stemDocument) %>%
# tm::tm_map(tm::stemCompletion, dictionary = dictCorpus) %>%
# utils::stack() %>% tibble::as_tibble() %>% dplyr::select(-values)
#dataset <- dataset %>% dplyr::mutate(tweet = as.character(myCorpus$ind))
dataset <- dataset %>% dplyr::mutate(tweet = as.character(tweet))
# SENTIMENT EXTRACTION -----------------------------------------------------------
output <- data.frame(anger = numeric(),
anticipation = numeric(),
disgust = numeric(),
fear = numeric(),
joy = numeric(),
sadness = numeric(),
surprise = numeric(),
trust = numeric(),
negative = numeric(),
positive = numeric())
for (row in 1:nrow(dataset)) {
df <- dataset[row, "tweet"] %>%
syuzhet::get_sentences() %>% # sentiment by sentence
#syuzhet::get_tokens(pattern = "\\W") %>% # sentiment by word
syuzhet::get_nrc_sentiment(lang = "english", lowercase = TRUE) %>%
colSums() %>%
t() %>%
data.frame()
output <- rbind(output, df)
}
# OUTPUT ------------------------------------------------------------------
utils::write.csv(output, file = "sentiment.csv", row.names = FALSE)
# plot a word cloud
wordcloud::wordcloud(dictCorpus,
scale = c(5, 0.5), # Set min and max scale
max.words = 100, # Set top n words
random.order = FALSE, # Words in decreasing freq
rot.per = 0.35, # % of vertical words
use.r.layout = FALSE, # Use C++ collision detection
colors = brewer.pal(8, "Dark2"))
#lsf.str("package:dplyr") #to list all functions in package
#environmentName(environment(select)) #to get package name from function