開始使用免費開始

直接上手!視覺化極性

情感分析能協助你擷取作者對主題的情緒傾向。這個練習會先帶你試試看接下來要學的內容!

我們已建立 text_df,代表一段對話,包含 persontext 欄位。

使用 qdappolarity() 函式替 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
___(___)
編輯並執行程式碼