Jump right in! Visualize polarity
Sentiment analysis helps you extract an author's feelings towards a subject. This exercise will give you a taste of what's to come!
We created text_df representing a conversation with person and text columns.
Use qdap's polarity() function to score text_df. polarity() will accept a single character object or data frame with a grouping variable to calculate a positive or negative score.
In this example you will use the magrittr package's dollar pipe operator %$%. The dollar sign forwards the data frame into polarity() and you declare a text column name or the text column and a grouping variable without quotes.
text_data_frame %$% polarity(text_column_name)
To create an object with the dollar sign operator:
polarity_object <- text_data_frame %$%
polarity(text_column_name, grouping_column_name)
More specifically, to make a quantitative judgement about the sentiment of some text, you need to give it a score. A simple method is a positive or negative value related to a sentence, passage or a collection of documents called a corpus. Scoring with positive or negative values only is called "polarity." A useful function for extracting polarity scores is counts() applied to the polarity object. For a quick visual call plot() on the polarity() outcome.
Este exercício faz parte do curso
Sentiment Analysis in R
Instruções do exercício
- Examine the
text_dfconversation data frame. - Using
%$%passtext_dftopolarity()along with the column nametextwithout quotes. This will print the polarity for all text. - Create a new object
datacamp_conversationby forwardingtext_dfwith%$%topolarity(). Pass intextfollowed by the groupingpersoncolumn. This will calculate polarity according to each individual person. Since it is all within parentheses the result will be printed too. - Apply
counts()todatacamp_conversationto print the specific emotional words that were found. plot()thedatacamp_conversation.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Examine the text data
text_df
# Calc overall polarity score
text_df %$% polarity(___)
# Calc polarity score by person
(datacamp_conversation <- text_df %$% ___(___, ___))
# Counts table from datacamp_conversation
___(___)
# Plot the conversation polarity
___(___)