在过去的几天里,我一直在打破这个.我搜索了所有的SO档案,并尝试了建议的解决方案,但似乎无法让这个工作.我在诸如2000 06,1995 -99等文件夹中有一组txt文档,并且想要运行一些基本的文本挖掘操作,例如创建文档术语矩阵和术语文档矩阵以及基于单词的共同位置进行一些操作.我的脚本适用于较小的语料库,但是,当我使用更大的语料库进行尝试时,它会让我失望.我已经粘贴了一个这样的文件夹操作的代码.
library(tm) # Framework for text mining.
library(SnowballC) # Provides wordStem() for stemming.
library(RColorBrewer) # Generate palette of colours for plots.
library(ggplot2) # Plot word frequencies.
library(magrittr)
library(Rgraphviz)
library(directlabels)
setwd("/ConvertedText")
txt <- file.path("2000 -06")
docs<-VCorpus(DirSource(txt, encoding = "UTF-8"),readerControl = list(language = "UTF-8"))
docs <- tm_map(docs, content_transformer(tolower), mc.cores=1)
docs <- tm_map(docs, removeNumbers, mc.cores=1)
docs <- tm_map(docs, removePunctuation, mc.cores=1)
docs <- tm_map(docs, stripWhitespace, mc.cores=1)
docs <- tm_map(docs, removeWords, stopwords("SMART"), mc.cores=1)
docs <- tm_map(docs, removeWords, stopwords("en"), mc.cores=1)
#corpus creation complete
setwd("/ConvertedText/output") …Run Code Online (Sandbox Code Playgroud) 我有两组数据:
一组标签(单词喜欢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 : …Run Code Online (Sandbox Code Playgroud) 我创建了一个TermDocumentMatrix来自tm于R.它看起来像这个库:
> inspect(freq.terms)
A document-term matrix (19 documents, 214 terms)
Non-/sparse entries: 256/3810
Sparsity : 94%
Maximal term length: 19
Weighting : term frequency (tf)
Terms
Docs abundant acid active adhesion aeropyrum alternative
1 0 0 1 0 0 0
2 0 0 0 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
6 0 1 0 0 0 0
7 …Run Code Online (Sandbox Code Playgroud) 我对R很新,并且不能完全围绕DocumentTermMatrixs.我有一个使用TM包创建的DocumentTermMatrix,它有术语频率和其中的术语,但我无法弄清楚如何访问它们.
理想情况下,我想:
Term #
"the" 200
"is" 400
"a" 200
Run Code Online (Sandbox Code Playgroud)
目前我的代码是:
library(tm)
common.words <- c("amp","@RT","I","http","https", stopwords("english"), "you")
x <- Corpus(VectorSource(results))
x <- tm_map(x, stripWhitespace)
x <- tm_map(x, removeNumbers)
x <- tm_map(x, removePunctuation)
x <- tm_map(x, stripWhitespace)
dtm <- DocumentTermMatrix(x)
for(i in 1:length(common.words)) {
dtm <- dtm[,!colnames(dtm)%in%c(common.words[i])]
}
Run Code Online (Sandbox Code Playgroud)
这是str(dtm)的输出
List of 6
$ i : int [1:9769] 1 1 1 1 1 1 1 1 2 2 ...
$ j : int [1:9769] 1596 1684 1858 2112 2175 2490 2714 2814 …Run Code Online (Sandbox Code Playgroud) 我知道术语 - 文档矩阵是一个数学矩阵,它描述了文档集合中出现的术语的频率.在文档术语矩阵中,行对应于集合中的文档,列对应于术语.
我正在使用sklearn的CountVectorizer从字符串(文本文件)中提取功能以简化我的任务.以下代码根据sklearn_documentation返回term-document矩阵
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np
vectorizer = CountVectorizer(min_df=1)
print(vectorizer)
content = ["how to format my hard disk", "hard disk format problems"]
X = vectorizer.fit_transform(content) #X is Term-document matrix
print(X)
Run Code Online (Sandbox Code Playgroud)
输出如下
我没有得到如何计算这个矩阵.请讨论代码中显示的示例.我已经阅读了维基百科的另一个例子,但无法理解.
我有以下情况,我想解决Python(最好使用numpy和scipy):
我如何实现这一目标Python?我知道我可以scipy.sparse.coo_matrix用来表示文档作为稀疏向量并使用点积找到余弦相似度,但是如何将整个语料库转换为大而稀疏的术语文档矩阵(这样我也可以将它的行提取为scipy.sparse.coo_matrix行向量)?
谢谢.
我想分析一个大的(n = 500,000)文档语料库.我使用quanteda的期望 会更快比tm_map()从tm.我想一步一步而不是使用自动化方式dfm().我有理由这样做:在一种情况下,我不想在删除停用词之前进行标记化,因为这会导致许多无用的双字母组合,在另一种情况下,我必须使用特定于语言的过程预处理文本.
我想要实现这个序列:
1)删除标点符号和数字
2)删除停用词(即在标记化之前避免无用的标记)
3)使用unigrams和bigrams标记化
4)创建dfm
我的尝试:
> library(quanteda)
> packageVersion("quanteda")
[1] ‘0.9.8’
> text <- ie2010Corpus$documents$texts
> text.corpus <- quanteda:::corpus(text, docnames=rownames(ie2010Corpus$documents))
> class(text.corpus)
[1] "corpus" "list"
> stopw <- c("a","the", "all", "some")
> TextNoStop <- removeFeatures(text.corpus, features = stopw)
# Error in UseMethod("selectFeatures") :
# no applicable method for 'selectFeatures' applied to an object of class "c('corpus', 'list')"
# This is how I would theoretically continue:
> …Run Code Online (Sandbox Code Playgroud) 在RI中,用于[tm package][1]从文档集构建术语 - 文档矩阵.
我的目标是从术语文档矩阵中的所有双字母组合中提取单词关联,并为每个前三个或一些返回.因此,我正在寻找一个包含矩阵中所有row.names的变量,以便该函数findAssocs()可以完成他的工作.
到目前为止这是我的代码:
library(tm)
library(RWeka)
txtData <- read.csv("file.csv", header = T, sep = ",")
txtCorpus <- Corpus(VectorSource(txtData$text))
...further preprocessing
#Tokenizer for n-grams and passed on to the term-document matrix constructor
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
txtTdmBi <- TermDocumentMatrix(txtCorpus, control = list(tokenize = BigramTokenizer))
#term argument holds two words since the BigramTokenizer extracted all pairs from txtCorpus
findAssocs(txtTdmBi, "cat shop", 0.5)
cat cabi cat scratch ...
0.96 …Run Code Online (Sandbox Code Playgroud) 我有一个文档TermMatrix,如下所示:
artikel naam product personeel loon verlof
doc 1 1 1 2 1 0 0
doc 2 1 1 1 0 0 0
doc 3 0 0 1 1 2 1
doc 4 0 0 0 1 1 1
Run Code Online (Sandbox Code Playgroud)
在包中tm,可以计算两个文档之间的汉明距离。但现在我想对汉明距离小于 3 的所有文档进行聚类。所以这里我希望聚类 1 是文档 1 和 2,聚类 2 是文档 3 和 4。有可能这样做吗?
r cluster-analysis matrix hamming-distance term-document-matrix