Sentiment Analysis AFINN Lexicon

im a newbie and im trying to get to work with Rstudio for my final project. i am using the AFINN sentiment dictionary, i found this code online but I keep getting an error and i am not sure where i am going wrong.

here is the code


#tweets <- data.frame(read.csv("tweets.csv"))

#tweets <- read_csv("F:/SKRIPSI/EKSTRAKSI FITUR/AFINN/05a_stemming_3000c.csv", 
#                   col_types = cols(label = col_character(), 
#                                    text = col_character()))
#df1 <- tweets$text

tweets <- data.frame(read.csv("F:/SKRIPSI/EKSTRAKSI FITUR/AFINN/05a_stemming_3000c.csv"))

dtm.control <- list(
  tolower = TRUE, 
  removePunctuation = TRUE, 
  removeNumbers = TRUE,
#  stopwords = stopwords("english"),
#  stemming = FALSE,
  wordLengths = c(2,"inf")

corpboston <- Corpus(VectorSource(tweets$text)) <- DocumentTermMatrix(corpboston, control = dtm.control)

afinn_list <- read.delim(file = "afinn-indo.txt", header = FALSE, stringsAsFactors = FALSE)  
names(afinn_list) <- c("word", "score")  
afinn_list$word <- tolower(afinn_list$word)

sentiment <- function(afinn_list, tweets) {
  tweet.sent <- mat.or.vec(nrow(tweets), 2)
  colnames(tweet.sent) <- c('text', 'label')
  word.frequency <- vector('list', nrow(tweets))
  for(f in 1:nrow(tweets))
    tweet.sent[f, 1] <- tweets$text[f]
    tweet.sent[f, 1] <- tweets$label[f]
    corpboston <- Corpus(VectorSource(tweets$text[f])) <- DocumentTermMatrix(corpboston, control = dtm.control)
    TermFreq <- data.frame(findFreqTerms(,$v)
    word.frequency[[f]] <- data.frame(findFreqTerms(,$v)
    sentwords <- intersect(findFreqTerms(, afinn_list[,1])
    sentwords.index <- match(sentwords, afinn_list[,1])
    if(length(sentwords.index) > 0)
      sentwords.score <- affin_list[sentwords.index,2]
      sentwords.freq <- TermFreq[match(sentwords,TermFreq[,1]),2]
      tweet.sent[f,3] <- sum(sentwords.score*sentwords.freq)
TweetSent <- data.frame(sentiment(affin_list, tweets))

the error is

Error in as.vector(y) : object 'affin_list' not found
Called from: as.vector(y)
Error during wrapup: unimplemented type (29) in 'eval'

Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Error during wrapup: INTEGER() can only be applied to a 'integer', not a 'unknown type #29'
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

i hope everyone can help, thankyou in advance!

seems like you've been flexible on how many f's are in afinn_list/affinn_list

ah that was my fault, it worked now. thank you! :slight_smile: :grin:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.