分词与清洗

R 文本分析入门

Maham Faisal Khan

Senior Data Science Content Developer

使用 tidytext

R 文本分析入门

文本分词

一些自然语言处理(NLP)术语:

  • 词袋:文档中的词彼此独立
  • 每段独立文本是一个文档
  • 每个唯一的词是一个术语
  • 术语的每次出现是一个标记
  • 构建词袋称为分词
R 文本分析入门

使用 unnest_tokens()

tidy_review <- review_data %>% 
  unnest_tokens(word, review)

tidy_review
# A tibble: 229,481 x 4
   date    product                    stars word   
   <chr>   <chr>                      <dbl> <chr>  
 1 2/28/15 iRobot Roomba 650 for Pets     5 you    
 2 2/28/15 iRobot Roomba 650 for Pets     5 would  
 3 2/28/15 iRobot Roomba 650 for Pets     5 not    
# … with 229,478 more rows
R 文本分析入门

统计词频

tidy_review %>% 
  count(word) %>% 
  arrange(desc(n))
# A tibble: 10,310 x 2
   word      n
   <chr> <int>
 1 the   11785
 2 it     7905
 3 and    6794
# … with 10,307 more rows
R 文本分析入门

使用 anti_join()

  • 我们想从整理后的数据框中移除停用词
  • 将通过连接实现此操作

R 文本分析入门

使用 anti_join()

tidy_review2 <- review_data %>% 
  unnest_tokens(word, review) %>% 
  anti_join(stop_words)

tidy_review2
# A tibble: 78,868 x 4
   date     product                    stars word       
   <chr>    <chr>                      <dbl> <chr>      
 1 1/12/15  iRobot Roomba 650 for Pets     4 walk       
 2 1/12/15  iRobot Roomba 650 for Pets     4 rest       
# … with 78,866 more rows
R 文本分析入门

再次统计词频

tidy_review2 %>% 
  count(word) %>% 
  arrange(desc(n))
# A tibble: 9,672 x 2
   word         n
   <chr>    <int>
 1 roomba    2286
 2 clean     1204
 3 vacuum     989
# … with 9,669 more rows
R 文本分析入门

¡Vamos a practicar!

R 文本分析入门

Preparing Video For Download...