tuc*_*son 5 r term-document-matrix
我有两组数据:
一组标签(单词喜欢php,html等)
一套文本
我现在希望构建一个Term-Document-Matrix,表示tags 元素中text元素的出现次数.
我已经查看了R库tm和TermDocumentMatrix函数,但是我没有看到将标记指定为输入的可能性.
有没有办法做到这一点?
我对任何工具(R,Python,其他)都持开放态度,尽管使用R会很棒.
我们将数据设置为:
TagSet <- data.frame(c("c","java","php","javascript","android"))
colnames(TagSet)[1] <- "tag"
TextSet <- data.frame(c("How to check if a java file is a javascript script java blah","blah blah php"))
colnames(TextSet)[1] <- "text"
Run Code Online (Sandbox Code Playgroud)
现在我想根据TagSet获得TextSet的TermDocumentMatrix.
我试过这个:
myCorpus <- Corpus(VectorSource(TextSet$text))
tdm <- TermDocumentMatrix(myCorpus, control = list(removePunctuation = TRUE, stopwords=TRUE))
>inspect(tdm)
A term-document matrix (7 terms, 2 documents)
Non-/sparse entries: 8/6
Sparsity : 43%
Maximal term length: 10
Weighting : term frequency (tf)
Docs
Terms 1 2
blah 1 2
check 1 0
file 1 0
java 2 0
javascript 1 0
php 0 1
script 1 0
Run Code Online (Sandbox Code Playgroud)
但是这是根据文本的单词检查文本,而我想检查已经定义的标签的存在.
小智 10
tdm.onlytags <- tdm[rownames(tdm)%in%TagSet$tag,]
Run Code Online (Sandbox Code Playgroud)
只选择您指定的单词,然后继续进行分析.