시작하기무료로 시작하기

감성 점수

소설 Animal Farm에서 주요 사건을 이끄는 세 마리의 돼지, Napoleon, Snowball, Squealer가 등장해요. 이들은 책 전반에서 반란 사상을 퍼뜨리고 다른 동물들이 농장 소유주인 Mr. Jones에게서 농장을 탈취하도록 부추기죠.

각 돼지를 언급한 문장들을 활용해, 어떤 등장인물이 가장 부정적인 감성과 함께 연관되어 있는지 확인하세요. 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))
}
코드 편집 및 실행