直接上手!視覺化極性
情感分析能協助你擷取作者對主題的情緒傾向。這個練習會先帶你試試看接下來要學的內容!
我們已建立 text_df,代表一段對話,包含 person 與 text 欄位。
使用 qdap 的 polarity() 函式替 text_df 打分。polarity() 可接受單一字元物件,或帶有分組變數的資料框,來計算正向或負向的分數。
本例中你會使用 magrittr 套件的 dollar 管線運算子 %$%。這個符號會把資料框轉送到 polarity(),而你在函式中以不加引號的欄位名稱指定文字欄,或同時指定文字欄與分組變數。
text_data_frame %$% polarity(text_column_name)
若要用 dollar 運算子建立物件:
polarity_object <- text_data_frame %$%
polarity(text_column_name, grouping_column_name)
更具體地說,若要對一段文字的情感做出量化判斷,你需要替它給分。簡單的方法是為句子、段落,或稱為語料庫的文件集合賦予正或負的數值。只用正負值評分稱為「極性」。用來擷取極性分數的實用函式是對極性物件套用 counts()。想快速視覺化,則對 polarity() 的輸出呼叫 plot()。
本練習屬於課程
R 情感分析
練習說明
- 先檢視
text_df這個對話資料框。 - 使用
%$%將text_df傳入polarity(),並以不加引號的欄位名稱text作為參數。這會印出所有文字的極性分數。 - 以
%$%將text_df轉送到polarity(),傳入text,再加上分組欄位person,建立新物件datacamp_conversation。這會計算每位人物的極性。由於都包在同一組括號內,結果也會一併列印。 - 對
datacamp_conversation套用counts(),列印找到的具體情感詞彙。 - 對
datacamp_conversation呼叫plot()。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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
___(___)