标签: term-document-matrix

使用tm_map(...,tolower)将文本转换为小写时出错

我试过用了tm_map.它给出了以下错误.我怎么能绕过这个?

 require(tm)
 byword<-tm_map(byword, tolower)

Error in UseMethod("tm_map", x) : 
  no applicable method for 'tm_map' applied to an object of class "character"
Run Code Online (Sandbox Code Playgroud)

r lowercase tm term-document-matrix

48
推荐指数
2
解决办法
6万
查看次数

使用R的单词频率列表

我一直在使用tm包来运行一些文本分析.我的问题是创建一个包含单词及其相关频率的列表

library(tm)
library(RWeka)

txt <- read.csv("HW.csv",header=T) 
df <- do.call("rbind", lapply(txt, as.data.frame))
names(df) <- "text"

myCorpus <- Corpus(VectorSource(df$text))
myStopwords <- c(stopwords('english'),"originally", "posted")
myCorpus <- tm_map(myCorpus, removeWords, myStopwords)

#building the TDM

btm <- function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3))
myTdm <- TermDocumentMatrix(myCorpus, control = list(tokenize = btm))
Run Code Online (Sandbox Code Playgroud)

我通常使用以下代码生成频率范围内的单词列表

frq1 <- findFreqTerms(myTdm, lowfreq=50)
Run Code Online (Sandbox Code Playgroud)

有没有办法自动化这个,以便我们得到一个包含所有单词及其频率的数据帧?

我面临的另一个问题是将术语文档矩阵转换为数据框.当我处理大量数据时,我遇到了内存错误.有一个简单的解决方案吗?

r text-mining term-document-matrix word-frequency

18
推荐指数
5
解决办法
4万
查看次数

使用NLTK的高效术语文档矩阵

我正在尝试使用NLTK和pandas创建一个术语文档矩阵.我写了以下函数:

def fnDTM_Corpus(xCorpus):
    import pandas as pd
    '''to create a Term Document Matrix from a NLTK Corpus'''
    fd_list = []
    for x in range(0, len(xCorpus.fileids())):
        fd_list.append(nltk.FreqDist(xCorpus.words(xCorpus.fileids()[x])))
    DTM = pd.DataFrame(fd_list, index = xCorpus.fileids())
    DTM.fillna(0,inplace = True)
    return DTM.T
Run Code Online (Sandbox Code Playgroud)

运行它

import nltk
from nltk.corpus import PlaintextCorpusReader
corpus_root = 'C:/Data/'

newcorpus = PlaintextCorpusReader(corpus_root, '.*')

x = fnDTM_Corpus(newcorpus)
Run Code Online (Sandbox Code Playgroud)

它适用于语料库中的少量小文件, 但当我尝试使用4,000个文件(每个大约2 kb)运行它时,它会给我一个MemoryError.

我错过了什么吗?

我使用的是32位python.(我在Windows 7,64位操作系统,Core Quad CPU,8 GB RAM).我真的需要使用64位这种大小的语料库吗?

python nltk pandas term-document-matrix

15
推荐指数
2
解决办法
3万
查看次数

如何告诉Solr返回每个文档的命中搜索条件?

我对Solr中的查询有疑问.当我使用多个搜索术语执行查询时,所有搜索术语都通过OR逻辑链接(例如q=content:(foo OR bar OR foobar)),而Solr则返回所有符合这些术语的文档列表.但Solr 没有回复的是哪个文件被哪些术语击中.所以在上面的例子中,我想知道的是我的结果列表中的哪些文档包含术语foo等.根据这些信息,我将能够创建一个术语 - 文档矩阵.

所以我的问题是:我怎么能告诉Solr给我那些丢失的信息呢?我确定它在某个地方,否则搜索作为一个整体是行不通的.但是我错过了什么?谢谢你的帮助.

PS:作为一种解决方法,我正在为所有搜索词执行单个Solr查询.但是你可以想象它在性能问题上是一个灾难,因为搜索条件的数量可以超过50 :(

solr term-document-matrix

14
推荐指数
2
解决办法
3285
查看次数

创建具有4M行的语料库和DTM的更有效方法

我的文件有超过4M的行,我需要一种更有效的方法将我的数据转换为语料库和文档术语矩阵,以便我可以将它传递给贝叶斯分类器.

请考虑以下代码:

library(tm)

GetCorpus <-function(textVector)
{
  doc.corpus <- Corpus(VectorSource(textVector))
  doc.corpus <- tm_map(doc.corpus, tolower)
  doc.corpus <- tm_map(doc.corpus, removeNumbers)
  doc.corpus <- tm_map(doc.corpus, removePunctuation)
  doc.corpus <- tm_map(doc.corpus, removeWords, stopwords("english"))
  doc.corpus <- tm_map(doc.corpus, stemDocument, "english")
  doc.corpus <- tm_map(doc.corpus, stripWhitespace)
  doc.corpus <- tm_map(doc.corpus, PlainTextDocument)
  return(doc.corpus)
}

data <- data.frame(
  c("Let the big dogs hunt","No holds barred","My child is an honor student"), stringsAsFactors = F)

corp <- GetCorpus(data[,1])

inspect(corp)

dtm <- DocumentTermMatrix(corp)

inspect(dtm)
Run Code Online (Sandbox Code Playgroud)

输出:

> inspect(corp)
<<VCorpus (documents: 3, metadata (corpus/indexed): 0/0)>>

[[1]]
<<PlainTextDocument (metadata: 7)>> …
Run Code Online (Sandbox Code Playgroud)

r corpus term-document-matrix qdap data.table

13
推荐指数
2
解决办法
1万
查看次数

R中的TermDocumentMatrix错误

我一直在研究R中{tm}包的许多在线示例,试图创建一个TermDocumentMatrix.创建和清理语料库非常简单,但是当我尝试创建矩阵时,我一直遇到错误.错误是:

UseMethod("meta",x)中的错误:"meta"没有适用于类"character"对象的适用方法此外:警告消息:在mclapply(unname(content(x)),termFreq,control):all计划的核心在用户代码中遇到错误

例如,这里是Jon Starkweather的文本挖掘示例中的代码.为这么长的代码提前道歉,但这确实产生了一个可重复的例子.请注意,错误在{tdm}函数结束时出现.

#Read in data
policy.HTML.page <- readLines("http://policy.unt.edu/policy/3-5")

#Obtain text and remove mark-up
policy.HTML.page[186:202]
id.1 <- 3 + which(policy.HTML.page == "                    TOTAL UNIVERSITY        </div>")
id.2 <- id.1 + 5
text.data <- policy.HTML.page[id.1:id.2]
td.1 <- gsub(pattern = "<p>", replacement = "", x = text.data, 
     ignore.case = TRUE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

td.2 <- gsub(pattern = "</p>", replacement = "", x = td.1, ignore.case = TRUE,
     perl = FALSE, fixed = FALSE, useBytes …
Run Code Online (Sandbox Code Playgroud)

r corpus text-mining tm term-document-matrix

12
推荐指数
2
解决办法
1万
查看次数

R tm包创建了几乎常用术语的矩阵

我在R中termDocumentMatrix创建了一个tm包.

我正在尝试创建一个具有50个最常出现的术语的矩阵/数据帧.

当我尝试转换为矩阵时,我收到此错误:

> ap.m <- as.matrix(mydata.dtm)
Error: cannot allocate vector of size 2.0 Gb
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用Matrix包转换为稀疏矩阵:

> A <- as(mydata.dtm, "sparseMatrix") 
Error in as(from, "CsparseMatrix") : 
  no method or default for coercing "TermDocumentMatrix" to "CsparseMatrix"
> B <- Matrix(mydata.dtm, sparse = TRUE)
Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法访问tdm的不同部分:

> freqy1 <- data.frame(term1 = findFreqTerms(mydata.dtm, lowfreq=165))
> mydata.dtm[mydata.dtm$ Terms %in% freqy1$term1,]
Error in seq_len(nr) : argument must be coercible to non-negative integer
Run Code Online (Sandbox Code Playgroud)

这是其他一些信息: …

r text-mining tm term-document-matrix

7
推荐指数
1
解决办法
5285
查看次数

TermDocumentMatrix有时会抛出错误

我正在根据各种不同运动队的推文创建一个词云.此代码成功执行约10次:

handle <- 'arsenal'
txt <- searchTwitter(handle,n=1000,lang='en')
t <- sapply(txt,function(x) x$getText())
t <- gsub('http.*\\s*|RT|Retweet','',t)
t <- gsub(handle,'',t)
t_c <- Corpus(VectorSource(t))
tdm = TermDocumentMatrix(t_c,control = list(removePunctuation = TRUE,stopwords = stopwords("english"),removeNumbers = TRUE, content_transformer(tolower)))
m = as.matrix(tdm)
word_freqs = sort(rowSums(m), decreasing=TRUE) 
dm = data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"),rot.per=0.5)
Run Code Online (Sandbox Code Playgroud)

其他9次中有10次,它会抛出以下错误:

Error in simple_triplet_matrix(i = i, j = j, v = as.numeric(v), nrow = length(allTerms),  : 
  'i, j, v' different lengths
In addition: Warning messages:
1: In mclapply(unname(content(x)), termFreq, control) :
  all …
Run Code Online (Sandbox Code Playgroud)

r word-cloud term-document-matrix

7
推荐指数
1
解决办法
4242
查看次数

如何有效地计算文档流中的文档之间的相似性

我收集Text文档(在Node.js中),其中一个文档i表示为单词列表.考虑到新文档是作为一种文档流出现的,计算这些文档之间相似性的有效方法是什么?

我目前在每个文档中的单词的标准化频率上使用cos相似性.由于可扩展性问题,我不使用TF-IDF(术语频率,反向文档频率),因为我收到的文档越来越多.

原来

我的第一个版本是当前可用的文件开始,计算一个大项-文档矩阵A,再计算S = A^T x A,这样S(i, j)是后(通过归一化后norm(doc(i))norm(doc(j)))文档之间的余弦相似性ij词频率分别为doc(i)doc(j).

对于新文件

收到新文件后我该怎么办doc(k)?好吧,我必须计算这个文档与之前所有文档的相似性,这不需要构建一个完整的矩阵.我可以把doc(k) dot doc(j)所有以前的内在产品j,结果S(k, j),这是伟大的.

麻烦

  1. S在Node.js中计算真的很长.实际上太长了!所以我决定创建一个C++模块,它可以更快地完成整个过程.它确实如此!但我不能等待它,我应该能够使用中间结果.而我所说的"不等待它"就是两者

    一个.等待计算完成,但也
    b.等待矩阵A建立(这是一个很大的矩阵).

  2. 计算new S(k, j)可以利用文档比所有给定单词(我用来构建整个矩阵A)的单词少得多的事实.因此,在Node.js中执行它看起来更快,避免了大量额外资源来访问数据.

但有没有更好的方法呢?

注意:我开始计算的原因S是我可以轻松地A在Node.js中构建我可以访问所有数据的地方,然后在C++中进行矩阵乘法并将其返回到Node.js中,这会加速整个事情的发展. .但是现在计算S变得不切实际,它看起来毫无用处.

注2:是的,我不必计算整体S,我可以计算右上角的元素(或左下角的元素),但这不是问题.时间计算问题不是那个顺序.

nlp stream node.js cosine-similarity term-document-matrix

6
推荐指数
1
解决办法
1166
查看次数

使用tm和RWeka创建N-Grams - 与VCorpus合作但不与Corpus合作

在使用'tm'和'RWeka'软件包创建biGrams的许多指南之后,我感到很沮丧的是,在tdm中只返回了1克.通过大量的反复试验,我发现使用' VCorpus '但不使用' Corpus ' 可以实现正常的功能.顺便说一句,我很确定这是在1个月前与'Corpus'合作但现在不是.

R(3.3.3),RTools(3.4),RStudio(1.0.136)和所有软件包(tm 0.7-1,RWeka 0.4-31)已更新至最新版本.

如果对于语料库不起作用以及其他人是否有同样的问题,我将不胜感激.

#A Reproducible example
#
#Weka bi-gram test
#

library(tm)
library(RWeka)

someCleanText <- c("Congress shall make no law respecting an establishment of",
                    "religion, or prohibiting the free exercise thereof or",
                    "abridging the freedom of speech or of the press or the",
                    "right of the people peaceably to assemble and to petition",
                    "the Government for a redress of grievances")

aCorpus <- Corpus(VectorSource(someCleanText))   #With this, only 1-Grams …
Run Code Online (Sandbox Code Playgroud)

r n-gram tm term-document-matrix rweka

6
推荐指数
1
解决办法
3036
查看次数