始める無料で始める

センチメントスコア

『Animal Farm』では、ナポレオン、スノーボール、スクィーラーという3頭のブタが物語の出来事を主導します。彼らは反乱の思想を広め、農場の持ち主であるジョーンズ氏から農場を奪い取るよう、ほかの動物たちを鼓舞します。

それぞれのブタに言及している文を使って、どのキャラクターに最もネガティブな感情が結び付いているかを判定してください。sentences ティブルには『Animal Farm』の文がティブルとして含まれています。

この演習はコースの一部です

Rで学ぶ自然言語処理入門

コースを見る

演習の手順

  • grepl() 関数を使って、ブタの名前に言及している文だけにフィルタします。
  • inner_join() を使って、afinn レキシコンからセンチメントスコアを結合します。
  • score 列を合計して結果を要約します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Print the overall sentiment associated with each pig's sentences
for(name in c("napoleon", "snowball", "squealer")) {
  # Filter to the sentences mentioning the pig
  pig_sentences <- sentences[___(___, sentences$sentence), ]
  # Tokenize the text
  napoleon_tokens <- pig_sentences %>%
    unnest_tokens(output = "word", token = "words", input = sentence) %>%
    anti_join(stop_words)
  # Use afinn to find the overall sentiment score
  result <- napoleon_tokens %>% 
    inner_join(___("___")) %>%
    summarise(sentiment = ___(___))
  # Print the result
  print(paste0(name, ": ", result$sentiment))
}
コードを編集して実行