Mark
Mark

Reputation: 1769

Package `sentimentr`: how remove emoticons and stopwords before `sentiment_by`

Here is a basic sentiment example. The text data is splitted into sentences via the get_sentences function. With sentiment_by we approximate the sentiment (polarity) of text for an entire element of a list (mytext in this example).

E.g. for the example:

library(sentimentr)

mytext <- c(
  'do you like it?  But I hate really bad dogs',
  'I am the best friend.',
  'Do you really like it?  I\'m not a fan'
)

mytext <- get_sentences(mytext)
sentiment_by(mytext)

I obtained the following result:

   element_id word_count       sd ave_sentiment
1:          1         10 1.497465    -0.8088680
2:          2          5       NA     0.5813777
3:          3          9 0.284605     0.2196345

Before applying sentiment function, I would like to remove stop words, number, emoticons from mytext. I figured I could use, e.g:

library("tm")
tm_map(mytext, removeNumbers)
tm_map(mytext, removeWords, stopwords())

but I obtained:

Error in UseMethod("tm_map", x) : 
 no applicable method for 'tm_map' applied to an object of class "c('get_sentences', 
'get_sentences_character', 'list')"

Upvotes: 0

Views: 350

Answers (0)

Related Questions