为R中的每行文本数据计算ngrams

为R中的每行文本数据计算ngrams,r,text,text-parsing,n-gram,tm,R,Text,Text Parsing,N Gram,Tm,我有以下格式的数据列: 文本 Hello world Hello How are you today I love stackoverflow blah blah blahdy 我想通过使用tau包的textcnt()函数来计算数据集中每行的3克。然而,当我尝试它时,它给了我一个数值向量,其中包含整个列的Ngram。如何将此函数分别应用于数据中的每个观察值?以下是使用 这就是你想要的吗 library("RWeka") library("tm") TrigramToken

我有以下格式的数据列:

文本

Hello world  
Hello  
How are you today  
I love stackoverflow  
blah blah blahdy  

我想通过使用
tau
包的
textcnt()
函数来计算数据集中每行的3克。然而,当我尝试它时,它给了我一个数值向量,其中包含整个列的Ngram。如何将此函数分别应用于数据中的每个观察值?

以下是使用


这就是你想要的吗

library("RWeka")
library("tm")

TrigramTokenizer <- function(x) NGramTokenizer(x, 
                                Weka_control(min = 3, max = 3))
# Using Tyler's method of making the 'Text' object here
tdm <- TermDocumentMatrix(Corpus(VectorSource(Text)), 
                          control = list(tokenize = TrigramTokenizer))

inspect(tdm)

A term-document matrix (4 terms, 5 documents)

Non-/sparse entries: 4/16
Sparsity           : 80%
Maximal term length: 20 
Weighting          : term frequency (tf)

                      Docs
Terms                  1 2 3 4 5
  are you today        0 0 1 0 0
  blah blah blahdy     0 0 0 0 1
  how are you          0 0 1 0 0
  i love stackoverflow 0 0 0 1 0
库(“RWeka”)
图书馆(“tm”)
TrigramTokenizer以下是如何使用该软件包:


txt我猜OP想要使用
tau
,但其他人没有使用该软件包。以下是您在tau的操作方式:

data = "Hello world\nHello\nHow are you today\nI love stackoverflow\n  
blah blah blahdy"

bigram_tau <- textcnt(data, n = 2L, method = "string", recursive = TRUE)

我强烈建议使用
tau
,因为它在处理大数据时表现非常好。我用它创建了1GB的大屏幕,它既快速又流畅。

@Tylerlinker谢谢你,Tyler。然而,萨普利没有起作用。我这样使用它:>trigram\u title最好是展示你所做的事情,而不是提及它。也许这篇文章可以帮助你。。。谢谢泰勒!我将探索您的qdap包。我想现在我将使用Ben的RWeka/tm解决方案,因为它以一种我可以轻松计算相似性的方式呈现数据。谢谢Ben。这使我能够轻松地计算Strings之间的令牌相似性。我在尝试“tdm”时遇到以下错误。听起来,Java安装有问题,可能路径设置不正确。
library("RWeka")
library("tm")

TrigramTokenizer <- function(x) NGramTokenizer(x, 
                                Weka_control(min = 3, max = 3))
# Using Tyler's method of making the 'Text' object here
tdm <- TermDocumentMatrix(Corpus(VectorSource(Text)), 
                          control = list(tokenize = TrigramTokenizer))

inspect(tdm)

A term-document matrix (4 terms, 5 documents)

Non-/sparse entries: 4/16
Sparsity           : 80%
Maximal term length: 20 
Weighting          : term frequency (tf)

                      Docs
Terms                  1 2 3 4 5
  are you today        0 0 1 0 0
  blah blah blahdy     0 0 0 0 1
  how are you          0 0 1 0 0
  i love stackoverflow 0 0 0 1 0
txt <- c("Hello world", "Hello", "How are you today", "I love stackoverflow", "blah blah blahdy")

require(quanteda)
dfm(txt, ngrams = 3, concatenator = " ", verbose = FALSE)
## Document-feature matrix of: 5 documents, 4 features.
## 5 x 4 sparse Matrix of class "dfmSparse"
##   features
## docs    how are you are you today i love stackoverflow blah blah blahdy
##  text1           0             0                    0                0
##  text2           0             0                    0                0
##  text3           1             1                    0                0
##  text4           0             0                    1                0
##  text5           0             0                    0                1
data = "Hello world\nHello\nHow are you today\nI love stackoverflow\n  
blah blah blahdy"

bigram_tau <- textcnt(data, n = 2L, method = "string", recursive = TRUE)
data.frame(counts = unclass(bigram_tau), size = nchar(names(bigram_tau)))
format(r)