Help Regarding R Code

Gollapinni Karthik Sharma
@gollapinni-karthik-sharma-EgBEXm โ€ข Oct 26, 2024

Hi,

I am trying to find out the sentiment score for a .csv file which is already existing in my system. I surfed in the internet and all I could find was people doing real time processing of twitter tweets but they have to save the data in .txt or .csv to process.

#-Link-Snipped-#
#-Link-Snipped-#
<a href="https://sivaanalytics.wordpress.com/2013/11/10/step-by-step-sentiment-analysis-on-twitter-data-using-r-with-airtel-tweets-part-iii/" target="_blank" rel="nofollow noopener noreferrer">Step by Step Sentiment analysis on Twitter data using R with Airtel Tweets: Part โ€“ III โ€“ My exploration in data analytics</a>

These are the websites which have the code for that.

Although I modified the code as I do not need the first part to excute.

So here is my code:

{
+
+     ##Packages required for sentiment analysis
+     library(plyr)
+     library(stringr)
+     library(sentiment)
+     library(wordcloud)
+     library(ggplot2)
+     library(RColorBrewer)
+     require(plyr)
+     require(stringr)
+
+     fedex_tweet=read.csv('C:\\Users\\gollapinni.karthik\\Downloads\\Project\\Best\\fedexAll.csv")
+
+
+ ##Upload the Lexicon of Hu and Liu saved on your desktop
+ pos_words = scan("C:\\Users\\gollapinni.karthik\\Downloads\\Project\\positive-words.txt', what='character', comment.char=';")
+ neg_words = scan("C:\\Users\\gollapinni.karthik\\Downloads\\Project\\positive-words.txt', what='character', comment.char=';")
+
+
+ ## Build the score sentiment function that will return the sentiment score
+ score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
Error: unexpected symbol in:
"## Build the score sentiment function that will return the sentiment score
score.sentiment = function(sentences, pos.words, neg.words, .progress='none"
> {
+
+   # we want a simple array ("a") of scores back, so we use
+   # "l" + "a" + "ply" = "laply":
+
+   scores = laply(sentences, function(sentence, pos.words, neg.words) {
+
+     # clean up sentences with R's regex-driven global substitute, gsub():
+                          
+                              sentence = gsub('[[punct:]]', '', sentence)
+                      
+                          sentence = gsub('[[:cntrl:]]', '', sentence)
+                      
+                          sentence = gsub('\\d+', '', sentence)
+                      
+                          # and convert to lower case:
+                      
+                          sentence = tolower(sentence)
+                      
+                          # split into words. str_split is in the stringr package
+                      
+                          word.list = str_split(sentence, '\\s+')
+                      
+                          # sometimes a list() is one level of hierarchy too much
+                      
+                          words = unlist(word.list)
+                      
+                          # compare our words to the dictionaries of positive & negative terms
+                      
+                          pos.matches = match(words, pos.words)
+                          neg.matches = match(words, neg.words)
+                      
+                          # match() returns the position of the matched term or NA
+                          # we just want a TRUE/FALSE:
+                      
+                          pos.matches = !is.na(pos.matches)
+                      
+                          neg.matches = !is.na(neg.matches)
+                      
+                          # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
+                      
+                          score = sum(pos.matches) - sum(neg.matches)
+                      
+                          return(score)
+                      
+ }, pos.words, neg.words, .progress=.progress )
+ scores.df = data.frame(score=scores, text=sentences)
+ return(scores.df)
+ }
Error: could not find function "laply"
>
> ## Creating a Vector to store sentiment scores
> a = rep(NA, 10)
>
> ## Calculate the sentiment score for each brand and store the score sum in array
> fedexAll.scores = score.sentiment(fedexAll.df$text, pos.words,neg.words, .progress='text')
Error in score.sentiment(fedexAll.df$text, pos.words, neg.words, .progress = "text") :
  could not find function "laply"
> a[1] = sum(fedexAll.scores$score)
Error: object 'fedexAll.scores' not found
>
>
> ##Plot the histogram for a few brand.
> par(mfrow=c(1,1))
> hist(fedexAll.scores$score, main="Fedex All Sentiments")
Error in hist(fedexAll.scores$score, main = "Fedex All Sentiments") :
  object 'fedexAll.scores' not found
>
> }
Error: unexpected '}' in "}"
>

This includes the error also. So can anybody help me to resolve this errors, it will be really thank full.

Or any suggestions to do sentiment analysis of a file somehow..is also fine.

Thanks,

Karthik

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform