텍스트 정제 기본기

R로 배우는 자연어 처리 입문

Kasey Jones

Research Data Scientist

러시아 트윗 데이터셋

러시아 트롤 트윗 300만 건

러시아 트롤 트윗 데이터셋은 fivethirtyeight가 제공하며, 2016년 미국 대선 기간에 생성된 봇 트윗 300만 건으로 구성됩니다.

  • 처음 20,000개 트윗을 탐색합니다
  • 트윗 본문, 팔로워/팔로잉, 게시일, 계정 유형 등 포함
  • 토픽 모델링, 분류, 개체명 인식 등에 적합
1 https://github.com/fivethirtyeight/russian-troll-tweets
R로 배우는 자연어 처리 입문

자주 나타나는 단어

library(tidytext); library(dplyr)
russian_tweets %>%
  unnest_tokens(word, content) %>%
  count(word, sort = TRUE)
# A tibble: 44,318 x 2
   word      n
   <chr> <int>
 1 t.co  18121
 2 https 16003
 3 the    7226
 4 to     5279
 ...
R로 배우는 자연어 처리 입문

불용어 제거

tidy_tweets <- russian_tweets %>%
  unnest_tokens(word, content) %>%
  anti_join(stop_words)
tidy_tweets %>%
  count(word, sort = TRUE)
 1 t.co             18121
 2 https            16003
 3 http              2135
 4 blacklivesmatter  1292
 5 trump             1004
 ...
# A tibble: 1,149 x 2
   word        lexicon
   <chr>       <chr>  
 1 a           SMART  
 2 a's         SMART  
 3 able        SMART  
 4 about       SMART  
 5 above       SMART
R로 배우는 자연어 처리 입문

사용자 정의 불용어

custom <- add_row(stop_words, word = "https", lexicon = "custom")
custom <- add_row(custom, word = "http", lexicon = "custom")
custom <- add_row(custom, word = "t.co", lexicon = "custom")
russian_tweets %>%
  unnest_tokens(word, content) %>%
  anti_join(custom) %>%
  count(word, sort = TRUE)
R로 배우는 자연어 처리 입문

최종 결과

# A tibble: 43,663 x 2
   word                 n
   <chr>            <int>
 1 blacklivesmatter  1292
 2 trump             1004
 3 black              781
 4 enlist             764
 5 police             745
 6 people             723
 7 cops               693
R로 배우는 자연어 처리 입문

어간 추출

  • enlisted ---> enlist
  • enlisting ---> enlist
library(SnowballC)
tidy_tweets <- russian_tweets %>%
  unnest_tokens(word, content) %>%
  anti_join(custom)
# 어간 추출
stemmed_tweets <- tidy_tweets %>%
  mutate(word = wordStem(word))
R로 배우는 자연어 처리 입문

어간 추출 결과

# A tibble: 38,907 x 2
   word               n
   <chr>          <int>
 1 blacklivesmatt  1301
 2 cop             1016
 3 trump           1013
 4 black            848
 5 enlist           809
 6 polic            763
 7 peopl            730
R로 배우는 자연어 처리 입문

예시 시간

R로 배우는 자연어 처리 입문

Preparing Video For Download...