# Text Analytics Part VII – Sentiment Analysis using R

In my last 6 posts on Text Analytics, I have discussed analysis of reviews and ratings given by customers seperately. Let us now compare the reviews written and ratings given by each customer. How many reviews matches with the ratings given.

But before starting with the analysis work, lets have a look on the meaning of these words:-

#### Sentiment analysis and polarity:

Sentiment analysis aims to determine the attitude of a speaker or a writer with respect to some topic or the overall contextual polarity of a document.

A basic task in sentiment analysis is classifying the polarity of a given text at the document, sentence, or feature/aspect level — whether the expressed opinion in a document, a sentence or an entity feature/aspect is positive, negative, or neutral. Advanced, “beyond polarity” sentiment classification looks, for instance, at emotional states such as “angry,” “sad,” and “happy.”

On the basis of polarity value (group stan.mean.polarity value field which will give this value), we have tried segregating the customer’s review into three categories:

1. who are highly Impressed with this phone
2. who are Satisfied with this phone
3. who are not at all satisfied

But first, we need to find the threshold value of polarity:

Considering 0.1324276 as threshold, got below table:

This table seems to be matching with the counts that we have got from ratings. Here 54% customers seem to be impressed by the use of MOTO-G. Most of the people have given ratings according to the views that they have expressed.

#### R Code to do this:

```#sentiments
#package required:- qdap
data2=data1
satisfaction1=as.data.frame(satisfaction)
for(i in 1:100)
{
sent=sent_detect(reviews[i])
pol=polarity(sent)
data2\$polarity[i]=pol\$group\$stan.mean.polarity
satisfaction1\$polarity_val[i]=pol\$group\$stan.mean.polarity
if(is.na(satisfaction1\$polarity_val[i]))
{satisfaction1\$polarity_val[i]=pol\$group\$ave.polarity
data2\$polarity[i]=pol\$group\$ave.polarity}
}

new_rate=cbind(finalratings1,satisfaction1)
aggregate(polarity_val~finalratings1,data=new_rate,FUN=mean)
new_rate\$status=ifelse(new_rate\$polarity_val>0.1324276,"Impressed!",ifelse(new_rate\$polarity_val<=-0.4982026,"Dissatisfied","Satisfied"))
count_status1=as.data.frame(table(new_rate\$status))

View(count_satis)
View(count_status1)
```

This is a chunk of code for creating contingency table of ratings with 3 categories of satisfaction, find full codes in R here.

With this post, I ends up this “Text Analysis” series. I hope this would have helped you in learning.

Don’t forget to comment so that I can improve my post.
Good Bye!
Take Care!