我希望找到一个简单的包装,但没有成功.我最终得到了RWeka,下面的代码和输出,但不幸的是,这种方法会丢弃所有2或1个字符的unigrams.
这可以修复,还是人们知道另一条路?谢谢!
TrigramTokenizer <- function(x) NGramTokenizer(x,Weka_control(min = 1,max = 3))Text = c( "Ab Hello world","Hello ab","ab" )tt = Corpus(VectorSource(Text))tdm <- TermdocumentMatrix( tt,control = List(tokenize = TrigramTokenizer))inspect(tdm)# <<TermdocumentMatrix (terms: 6,documents: 3)>># Non-/sparse entrIEs: 7/11# Sparsity : 61%# Maximal term length: 14# weighting : term frequency (tf)# Docs# Terms 1 2 3# ab hello 1 0 0# ab hello world 1 0 0# hello 1 1 0# hello ab 0 1 0# hello world 1 0 0# world 1 0 0
这是一个来自下面的ngram()版本,为最优性而编辑(我认为).基本上,当include.all = TRUE时,我尝试重用令牌字符串以摆脱双循环.
ngram <- function(tokens,n = 2,concatenator = "_",include.all = FALSE) { M = length(tokens) stopifnot( n > 0 ) # if include.all=FALSE return null if nothing to report due to short doc if ( ( M == 0 ) || ( !include.all && M < n ) ) { return( c() ) } # bail if just want original tokens or if we only have one token if ( (n == 1) || (M == 1) ) { return( tokens ) } # set max size of ngram at max length of tokens end <- min( M-1,n-1 ) all_ngrams <- c() toks = tokens for (wIDth in 1:end) { if ( include.all ) { all_ngrams <- c( all_ngrams,toks ) } toks = paste( toks[1:(M-wIDth)],tokens[(1+wIDth):M],sep=concatenator ) } all_ngrams <- c( all_ngrams,toks ) all_ngrams}ngram( c("A","B","C","D"),n=3,include.all=TRUE ) ngram( c("A",include.all=FALSE ) ngram( c("A",n=10,include.all=FALSE ) ngram( c("A",include.all=TRUE ) # edge casesngram( c(),include.all=TRUE ) ngram( "A",n=0,include.all=FALSE ) ngram( "A",n=1,"B"),"C"),include.all=TRUE )解决方法 你很幸运,有一个包: quanteda.
# or: devtools::install_github("kbenoit/quanteda")require(quanteda)Text <- c("Ab Hello world","ab")dfm(Text,ngrams = 1:3,verbose = FALSE)## document-feature matrix of: 3 documents,7 features.## 3 x 7 sparse Matrix of class "dfmSparse"## features## docs ab ab_hello ab_hello_world hello hello_ab hello_world world## text1 1 1 1 1 0 1 1## text2 1 0 0 1 1 0 0## text3 1 0 0 0 0 0 0
这创建了一个文档特征矩阵,其中“特征”是低级的unigrams,bigrams和trigrams.如果您喜欢单词之间的空格,只需将参数concatenator =“”添加到dfm()调用即可.
问题解决了,不需要Weka.
对于好奇,这里是创建n-gram的主力函数,其中标记是一个字符向量(来自单独的标记化器):
ngram <- function(tokens,include.all = FALSE) { # start with lower ngrams,or just the specifIEd size if include.all = FALSE start <- ifelse(include.all,1,ifelse(length(tokens) < n,n)) # set max size of ngram at max length of tokens end <- ifelse(length(tokens) < n,length(tokens),n) all_ngrams <- c() # outer loop for all ngrams down to 1 for (wIDth in start:end) { new_ngrams <- tokens[1:(length(tokens) - wIDth + 1)] # inner loop for ngrams of wIDth > 1 if (wIDth > 1) { for (i in 1:(wIDth - 1)) new_ngrams <- paste(new_ngrams,tokens[(i + 1):(length(tokens) - wIDth + 1 + i)],sep = concatenator) } # paste onto prevIoUs results and continue all_ngrams <- c(all_ngrams,new_ngrams) } all_ngrams}总结
以上是内存溢出为你收集整理的在R中通过三卦产生所有单词unigrams全部内容,希望文章能够帮你解决在R中通过三卦产生所有单词unigrams所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)