与R中的unnest_tokens相反

时间:2018-03-05 20:04:32

标签: r tidytext

我有一个数据框,我已在R中转换为整洁的文本格式,以摆脱停用词。我现在想要不整洁'该数据框恢复为原始格式。

unexst_tokens的反向/反向命令是什么? 我在本论坛上提到的另一个类似问题中检查了答案,我可以执行以下操作:

如果我希望在使用purrr中的地图函数对其整理后的形式进行一些处理后将文本恢复为原始形式。

首先,让我们从原始文本转到整理格式。

library(tidyverse)
library(tidytext)


tidy_austen <- janeaustenr::austen_books() %>%
  group_by(book) %>%
  mutate(linenumber = row_number()) %>%
  ungroup() %>%
  unnest_tokens(word, text)

tidy_austen
#> # A tibble: 725,055 x 3
#>                   book linenumber        word
#>                 <fctr>      <int>       <chr>
#>  1 Sense & Sensibility          1       sense
#>  2 Sense & Sensibility          1         and
#>  3 Sense & Sensibility          1 sensibility
#>  4 Sense & Sensibility          3          by
#>  5 Sense & Sensibility          3        jane
#>  6 Sense & Sensibility          3      austen
#>  7 Sense & Sensibility          5        1811
#>  8 Sense & Sensibility         10     chapter
#>  9 Sense & Sensibility         10           1
#> 10 Sense & Sensibility         13         the
#> # ... with 725,045 more rows

现在文字很整洁!但我们可以将它弄清楚,回到某种类似于其原始形式的东西。我通常使用来自tidyr的嵌套来处理这个问题,然后使用purrr中的一些map函数。

nested_austen <- tidy_austen %>%
  nest(word) %>%
  mutate(text = map(data, unlist), 
         text = map_chr(text, paste, collapse = " ")) 

nested_austen
#> # A tibble: 62,272 x 4
#>                   book linenumber              data
#>                 <fctr>      <int>            <list>
#>  1 Sense & Sensibility          1  <tibble [3 x 1]>
#>  2 Sense & Sensibility          3  <tibble [3 x 1]>
#>  3 Sense & Sensibility          5  <tibble [1 x 1]>
#>  4 Sense & Sensibility         10  <tibble [2 x 1]>
#>  5 Sense & Sensibility         13 <tibble [12 x 1]>
#>  6 Sense & Sensibility         14 <tibble [13 x 1]>
#>  7 Sense & Sensibility         15 <tibble [11 x 1]>
#>  8 Sense & Sensibility         16 <tibble [12 x 1]>
#>  9 Sense & Sensibility         17 <tibble [11 x 1]>
#> 10 Sense & Sensibility         18 <tibble [15 x 1]>
#> # ... with 62,262 more rows, and 1 more variables: text <chr>

如果我刻录成n克,其中n可以是2或3,请有人帮我改变上面的代码。

我想做的是:

第1步:将文本拆分为三元组

步骤2:查看三元组并查看哪些是有意义的(我需要手动检查它,我将只替换对我有意义的那些)

步骤:3将原始文本中的这些三元组替换为_

连接的单个单词

第4步:重复上面的双字母

步骤5:然后再次标记

1 个答案:

答案 0 :(得分:0)

如果我正确理解你想做什么,你可以通过调用n = 3将你的双字母(或三元组,只需更改为mutate())变成一个单元。

library(tidyverse)
library(tidytext)


tidy_austen <- janeaustenr::austen_books() %>%
  group_by(book) %>%
  mutate(linenumber = row_number()) %>%
  ungroup() %>%
  unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
  mutate(bigram = str_replace_all(bigram, " ", "_"))

tidy_austen
#> # A tibble: 662,783 x 3
#>    book                linenumber bigram         
#>    <fct>                    <int> <chr>          
#>  1 Sense & Sensibility          1 sense_and      
#>  2 Sense & Sensibility          1 and_sensibility
#>  3 Sense & Sensibility          3 by_jane        
#>  4 Sense & Sensibility          3 jane_austen    
#>  5 Sense & Sensibility         10 chapter_1      
#>  6 Sense & Sensibility         13 the_family     
#>  7 Sense & Sensibility         13 family_of      
#>  8 Sense & Sensibility         13 of_dashwood    
#>  9 Sense & Sensibility         13 dashwood_had   
#> 10 Sense & Sensibility         13 had_long       
#> # ... with 662,773 more rows

然后,您可以在same way as in my other answer中重新嵌套文本。

nested_austen <- tidy_austen %>%
  nest(bigram) %>%
  mutate(text = map(data, unlist), 
         text = map_chr(text, paste, collapse = " ")) 

nested_austen %>%
  select(text)
#> # A tibble: 61,180 x 1
#>    text                                                                   
#>    <chr>                                                                  
#>  1 sense_and and_sensibility                                              
#>  2 by_jane jane_austen                                                    
#>  3 chapter_1                                                              
#>  4 the_family family_of of_dashwood dashwood_had had_long long_been been_…
#>  5 was_large large_and and_their their_residence residence_was was_at at_…
#>  6 their_property property_where where_for for_many many_generations gene…
#>  7 respectable_a a_manner manner_as as_to to_engage engage_the the_genera…
#>  8 surrounding_acquaintance acquaintance_the the_late late_owner owner_of…
#>  9 man_who who_lived lived_to to_a a_very very_advanced advanced_age age_…
#> 10 life_had had_a a_constant constant_companion companion_and and_houseke…
#> # ... with 61,170 more rows

reprex package(v0.2.0)创建于2018-03-20。