r - 如何使用整洁的文本使用二元组和三元组

标签 r token tidytext

我正在尝试使用 tidytext 同时使用二元组和三元组。我可以使用什么代码让 token 查找 2 和 3 个单词。

这是仅使用二元组的代码:

library(tidytext)
library(janeaustenr)

austen_bigrams <- austen_books() %>%
  unnest_tokens(bigram, text, token = "ngrams", n = 2)

austen_bigrams

最佳答案

如果您查看 ?unnest_tokens,它会告诉您 ... 用于传递给标记生成器的参数。对于 ngrams,这就是 tokenizers::tokenize_ngrams,如果你查看它的帮助页面,它有一个 n_min 参数,所以你可以这样做

library(magrittr)
library(tidytext)
library(janeaustenr)

austen_bigrams <- austen_books() %>% 
    head(1000) %>%    # otherwise this will get very large
    unnest_tokens(bigram, text, token = "ngrams", n = 3, n_min = 2)

austen_bigrams
#> # A tibble: 19,801 x 2
#>                   book                bigram
#>                 <fctr>                 <chr>
#>  1 Sense & Sensibility             sense and
#>  2 Sense & Sensibility sense and sensibility
#>  3 Sense & Sensibility       and sensibility
#>  4 Sense & Sensibility    and sensibility by
#>  5 Sense & Sensibility        sensibility by
#>  6 Sense & Sensibility   sensibility by jane
#>  7 Sense & Sensibility               by jane
#>  8 Sense & Sensibility        by jane austen
#>  9 Sense & Sensibility           jane austen
#> 10 Sense & Sensibility      jane austen 1811
#> # ... with 19,791 more rows

关于r - 如何使用整洁的文本使用二元组和三元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663519/

相关文章:

c++ - 一个特定的测试用例不会以某种方式通过测试

c - strtok() 问题 : If tokens are delimited by delimiters, 为什么最后一个标记位于分隔符和空值 '\0' 之间?

java - 无需高级扫描仪即可获取 Java 扫描仪输入

r - 从R中的字符串中提取混合日期

r - 与unnest_tokens相反

在 R (Windows) 中从 C 创建一个 dll 动态库

read.csv ("http://ichart.finance.yahoo.com/table.csv?s=SPY") 不工作

r - r 中组的加权平均值 - 一起使用聚合函数和加权平均值函数

r - 在 R 中的两个分类点之间添加一条虚线

r - 语料库中最常提到的国家;从摘要中提取国家名称 R