R 计算术语文档矩阵,同时查找字符串中的单词
这个问题与我先前的问题有关 将它作为一个单独的发布,因为它可能会帮助其他用户轻松找到它 问题是关于R 计算术语文档矩阵,同时查找字符串中的单词,r,text-mining,tm,term-document-matrix,R,Text Mining,Tm,Term Document Matrix,这个问题与我先前的问题有关 将它作为一个单独的发布,因为它可能会帮助其他用户轻松找到它 问题是关于tm软件包当前计算术语文档矩阵的方式。我想对这种方式稍加调整,如下所述 目前,任何术语文档矩阵都是通过在文档中查找一个单独的单词(而不是字符串)来创建的。例如,假设有两个文档 document 1: "this is a milky way galaxy" document 2: "this is a milkyway galaxy" 根据当前算法的工作方式(tmpackage),“银河”将出
tm
软件包当前计算术语文档矩阵的方式。我想对这种方式稍加调整,如下所述
目前,任何术语文档矩阵都是通过在文档中查找一个单独的单词(而不是字符串)来创建的。例如,假设有两个文档
document 1: "this is a milky way galaxy"
document 2: "this is a milkyway galaxy"
根据当前算法的工作方式(tm
package),“银河”将出现在第一个文档中,但不会出现在第二个文档中,因为算法会将术语银河
作为一个单独的词来查找。但是,如果该算法查找了术语milky
一个类似字符串的函数grepl
,它也会在第二个文档中找到术语“milky”
grepl('milky', 'this is a milkyway galaxy')
TRUE
有人能帮我创建一个符合我要求的术语文档矩阵吗(即在两个文档中都能找到术语milky
。请注意,我不想要一个特定于某个单词的解决方案或milky
,我想要一个通用解决方案,我将在更大范围内应用它来处理所有此类情况)?即使解决方案不使用tm
包,也可以。我只需要最终得到一份符合我要求的学期文档矩阵最终,我希望能够获得一个术语文档矩阵,以便在所讨论的文档的所有字符串中查找其中的每个术语作为字符串(而不仅仅是单词)(grepl
like功能,同时计算术语文档矩阵)。
我用来获取术语文档矩阵的当前代码是
doc1 <- "this is a document about milkyway"
doc2 <- "milky way is huge"
library(tm)
tmp.text<-data.frame(rbind(doc1,doc2))
tmp.corpus<-Corpus(DataframeSource(tmp.text))
tmpDTM<-TermDocumentMatrix(tmp.corpus, control= list(tolower = T, removeNumbers = T, removePunctuation = TRUE,stopwords = TRUE,wordLengths = c(2, Inf)))
tmp.df<-as.data.frame(as.matrix(tmpDTM))
tmp.df
1 2
document 1 0
huge 0 1
milky 0 1
milkyway 1 0
way 0 1
doc1我不确定tm是否使基于正则表达式选择或分组特征变得容易(或可能)。但是,在构建文档特征矩阵时,文本包通过一个根据字典对术语进行分组的叙词表
参数做到了这一点
(quanteda使用通用术语“功能”,因为在这里,您的类别是包含短语“乳白色”的术语,而不是原始的“术语”。)
valuetype
参数可以是“glob”格式(默认)、正则表达式(“regex”
),也可以是固定格式(“fixed”
)。下面我展示了带有glob和正则表达式的版本
require(quanteda)
myDictGlob <- dictionary(list(containsMilky = c("milky*")))
myDictRegex <- dictionary(list(containsMilky = c("^milky")))
(plainDfm <- dfm(c(doc1, doc2)))
## Creating a dfm from a character vector ...
## ... lowercasing
## ... tokenizing
## ... indexing documents: 2 documents
## ... indexing features: 9 feature types
## ... created a 2 x 9 sparse dfm
## ... complete.
## Elapsed time: 0.008 seconds.
## Document-feature matrix of: 2 documents, 9 features.
## 2 x 9 sparse Matrix of class "dfmSparse"
## features
## docs this is a document about milkyway milky way huge
## text1 1 1 1 1 1 1 0 0 0
## text2 0 1 0 0 0 0 1 1 1
dfm(c(doc1, doc2), thesaurus = myDictGlob, valuetype = "glob", verbose = FALSE)
## Document-feature matrix of: 2 documents, 8 features.
## 2 x 8 sparse Matrix of class "dfmSparse"
## this is a document about way huge CONTAINSMILKY
## text1 1 1 1 1 1 0 0 1
## text2 0 1 0 0 0 1 1 1
dfm(c(doc1, doc2), thesaurus = myDictRegex, valuetype = "regex")
## Document-feature matrix of: 2 documents, 8 features.
## 2 x 8 sparse Matrix of class "dfmSparse"
## this is a document about way huge CONTAINSMILKY
## text1 1 1 1 1 1 0 0 1
## text2 0 1 0 0 0 1 1 1
require(quanteda)
myDictGlob我不确定tm是否使基于正则表达式选择或分组特征变得容易(或可能)。但是,在构建文档特征矩阵时,文本包通过一个根据字典对术语进行分组的叙词表
参数做到了这一点
(quanteda使用通用术语“功能”,因为在这里,您的类别是包含短语“乳白色”的术语,而不是原始的“术语”。)
valuetype
参数可以是“glob”格式(默认)、正则表达式(“regex”
),也可以是固定格式(“fixed”
)。下面我展示了带有glob和正则表达式的版本
require(quanteda)
myDictGlob <- dictionary(list(containsMilky = c("milky*")))
myDictRegex <- dictionary(list(containsMilky = c("^milky")))
(plainDfm <- dfm(c(doc1, doc2)))
## Creating a dfm from a character vector ...
## ... lowercasing
## ... tokenizing
## ... indexing documents: 2 documents
## ... indexing features: 9 feature types
## ... created a 2 x 9 sparse dfm
## ... complete.
## Elapsed time: 0.008 seconds.
## Document-feature matrix of: 2 documents, 9 features.
## 2 x 9 sparse Matrix of class "dfmSparse"
## features
## docs this is a document about milkyway milky way huge
## text1 1 1 1 1 1 1 0 0 0
## text2 0 1 0 0 0 0 1 1 1
dfm(c(doc1, doc2), thesaurus = myDictGlob, valuetype = "glob", verbose = FALSE)
## Document-feature matrix of: 2 documents, 8 features.
## 2 x 8 sparse Matrix of class "dfmSparse"
## this is a document about way huge CONTAINSMILKY
## text1 1 1 1 1 1 0 0 1
## text2 0 1 0 0 0 1 1 1
dfm(c(doc1, doc2), thesaurus = myDictRegex, valuetype = "regex")
## Document-feature matrix of: 2 documents, 8 features.
## 2 x 8 sparse Matrix of class "dfmSparse"
## this is a document about way huge CONTAINSMILKY
## text1 1 1 1 1 1 0 0 1
## text2 0 1 0 0 0 1 1 1
require(quanteda)
myDictGlob在乳白色前后使用\b
@pcantalupo在哪里使用此/b
?正如我所解释的,这个问题不仅仅局限于“牛奶”“牛奶”只是一个例子。最终,我希望能够创建一个术语文档矩阵,该矩阵的计算方式应确保每个术语也应在文档字符串中查找。grepl(“\\bmilky\\b”,“这是一个milkyway galaxy”)
将返回FALSE@pcantalupo我不想无礼。但你似乎不明白这个问题。请再读一遍。很抱歉……我稍后会回到这里使用\b
在milky
@pcantalupo前后使用/b
?正如我所解释的,这个问题不仅仅局限于“牛奶”“牛奶”只是一个例子。最终,我希望能够创建一个术语文档矩阵,该矩阵的计算方式应确保每个术语也应在文档字符串中查找。grepl(“\\bmilky\\b”,“这是一个milkyway galaxy”)
将返回FALSE@pcantalupo我不想无礼。但你似乎不明白这个问题。请再读一遍。很抱歉……我稍后再谈这个问题