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

setwd("F:/SKRIPSI/EKSTRAKSI FITUR/AFINN")


#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
#mat.or.vec(nrow(tweets),2)

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

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

corpboston <- Corpus(VectorSource(tweets$text))
dtm.boston <- 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]))
    dtm.boston <- DocumentTermMatrix(corpboston, control = dtm.control)
    TermFreq <- data.frame(findFreqTerms(dtm.boston), dtm.boston$v)
    word.frequency[[f]] <- data.frame(findFreqTerms(dtm.boston), dtm.boston$v)
    sentwords <- intersect(findFreqTerms(dtm.boston), 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)
    }
  }  
  return(tweet.sent)}
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.