【问题标题】:Opposite of unnest_tokens in RR中unnest_tokens的对面
【发布时间】:2018-03-05 20:04:32
【问题描述】:

我有一个数据框,已在 R 中转换为整洁的文本格式,以消除停用词。我现在想将该数据框“整理”回其原始格式。

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

如果我想通过 purrr 中的 map 函数将文本经过整理后的形式恢复为原始形式。

首先,让我们从原始文本转换为经过整理的格式。

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 中的 nest,然后使用 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 步:然后再次标记化

【问题讨论】:

    标签: r tidytext


    【解决方案1】:

    如果我正确理解您想要做什么,您可以通过调用 mutate() 将您的二元组(或三元组,只需更改为 n = 3)转换为一个单元。

    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 年 3 月 20 日创建。

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多