0

I have a column made up of 741 reviews. I would like to get other two columns with a value which indicate the number of positive and negative words for each review.

can someone of you help me?

I tried to use this function but it gives me a score by subtracting negative and positive words for each review. I would like to get 2 columns containing just the number of positive and negative words.

score.sentiment = function(sentence, pos.words, neg.words, .progress=’none’)

    {
     require(plyr)
     require(stringr)


    scores = laply(sentences, function(sentence, pos.words, neg.words) {


     sentence = gsub(‘[[:punct:]]’, ‘’, sentence)
     sentence = gsub(‘[[:cntrl:]]’, ‘’, sentence)
     sentence = gsub(‘\\d+’, ‘’, sentence)

     sentence = tolower(sentence)


     word.list = str_split(sentence, ‘\\s+’)
      words = unlist(word.list)


     pos.matches = match(words, pos.words)
     neg.matches = match(words, neg.words)


     pos.matches = !is.na(pos.matches)
     neg.matches = !is.na(neg.matches)


     score = sum(pos.matches) — sum(neg.matches)

     return(score)
     }, pos.words, neg.words, .progress=.progress )

     scores.df = data.frame(score=scores, text=sentence)
     return(scores.df)
    }
  • 2
    You can check the tidy approach [here](http://tidytextmining.com/sentiment.html) – akrun Dec 11 '17 at 16:29
  • 1
    Lacking that, please make this question [reproducible](https://stackoverflow.com/questions/5963269/); also, making it a question with data and code shows some effort on your part, otherwise it might be seen as "code this for me", not what SO is meant to provide. – r2evans Dec 11 '17 at 16:31
  • I tried to use the function "match" with a list of positive and negative words but it did not work. Sorry but I am a beginner in r – Alessandro Dec 11 '17 at 16:41
  • Show the code you've tried, and show a sample of your data. Refer to other questions for examples of this. Help us to help you – spinodal Dec 11 '17 at 20:24
  • Attached you may find the code I have used but it gives a different result. – Alessandro Dec 12 '17 at 21:13

0 Answers0