Help Regarding R Code

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-#
Step by Step Sentiment analysis on Twitter data using R with Airtel Tweets: Part – III – My exploration in data analytics

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

  • Gollapinni Karthik Sharma
    Gollapinni Karthik Sharma
    Any Code Geeks!
    Kindly help!

You are reading an archived discussion.

Related Posts

This - ...and all I wanted to tell you is that the next Terminator Movie is coming soon and our favourite Arnie "will be back" to kill all the machines...
One of the biggest fears of a petrol head or a gear head is that when the world finally runs out of petrol, his or her favourite supercar will go...
A team of biomedical engineers from Washington University in St. Louis has invented a two-dimensional camera by using Compressed Ultrafast Photography (CUP) technology developed at School of Engineering & Applied...
Quote: Medical errors are the third leading cause of death in the United States, killing more people than everything but cancer and heart disease. The reasons behind those errors include...
Quote: Ever heard of biological circuits? Odds are you will, according to a recent report in EE Times. Researchers have been engaged in pioneering work in which they "wire" naturally...