Loading 02_code/R/Script 2 - Sentiment analysis.R 0 → 100644 +60 −0 Original line number Diff line number Diff line rm(list = ls()) # Installation of packages install.packages("stringr") # Loading of packages library(stringr) #loading necessary dataset setwd("D:/Hohenheim/SEM 3/ADS/introads_ass2_team18/01_data") reviews.clean <- read.csv("reviews.clean.csv") # Only keeping necessary columns for analysis rev.sentiment <- data.frame(reviews = reviews.clean$review) # Manual sentiment analysis----------------------------------------------------- # Loading of necessary words lists taken from Kaggel by Hu and Bing Liu setwd("D:/Hohenheim/SEM 3/ADS/introads_ass2_team18/00_docs") positive.words <- readLines("positive-words.txt") negative.words <- readLines("negative-words.txt") # Function to perform sentiment analysis on a single review; in a code, sent=sentiment sent.analysis <- function(review) { # Tokenize the review into words words <- unlist(strsplit(review, "\\s+")) # Initialize sentiment score sent.score <- 0 # Loop through each word in the review for (word in words) { # Check if the word is in the positive or negative word lists if (word %in% positive.words) { sent.score <- sent.score + 1 } else if (word %in% negative.words) { sent.score <- sent.score - 1 } } # Check for negation (not) before an opinion word if ("not" %in% words) { sent.score <- sent.score * -1 } # Ascribe positive (1), negative (-1), or neutral (0) sentiment sentiment <- ifelse(sent.score > 0, 1, ifelse(sent.score < 0, -1, 0)) return(sentiment) } # Apply sentiment analysis to all reviews in the dataframe rev.sentiment$sentiment.score <- sapply(rev.sentiment$reviews, sent.analysis) # Labeling the sentiment score with sentiment rev.sentiment$sentiment.label <- ifelse(rev.sentiment$sentiment.score == 1, "Positive", ifelse(rev.sentiment$sentiment.score == -1, "Negative", "Neutral")) Loading
02_code/R/Script 2 - Sentiment analysis.R 0 → 100644 +60 −0 Original line number Diff line number Diff line rm(list = ls()) # Installation of packages install.packages("stringr") # Loading of packages library(stringr) #loading necessary dataset setwd("D:/Hohenheim/SEM 3/ADS/introads_ass2_team18/01_data") reviews.clean <- read.csv("reviews.clean.csv") # Only keeping necessary columns for analysis rev.sentiment <- data.frame(reviews = reviews.clean$review) # Manual sentiment analysis----------------------------------------------------- # Loading of necessary words lists taken from Kaggel by Hu and Bing Liu setwd("D:/Hohenheim/SEM 3/ADS/introads_ass2_team18/00_docs") positive.words <- readLines("positive-words.txt") negative.words <- readLines("negative-words.txt") # Function to perform sentiment analysis on a single review; in a code, sent=sentiment sent.analysis <- function(review) { # Tokenize the review into words words <- unlist(strsplit(review, "\\s+")) # Initialize sentiment score sent.score <- 0 # Loop through each word in the review for (word in words) { # Check if the word is in the positive or negative word lists if (word %in% positive.words) { sent.score <- sent.score + 1 } else if (word %in% negative.words) { sent.score <- sent.score - 1 } } # Check for negation (not) before an opinion word if ("not" %in% words) { sent.score <- sent.score * -1 } # Ascribe positive (1), negative (-1), or neutral (0) sentiment sentiment <- ifelse(sent.score > 0, 1, ifelse(sent.score < 0, -1, 0)) return(sentiment) } # Apply sentiment analysis to all reviews in the dataframe rev.sentiment$sentiment.score <- sapply(rev.sentiment$reviews, sent.analysis) # Labeling the sentiment score with sentiment rev.sentiment$sentiment.label <- ifelse(rev.sentiment$sentiment.score == 1, "Positive", ifelse(rev.sentiment$sentiment.score == -1, "Negative", "Neutral"))