标签: tm

如何压扁列表列表?

tm包扩展c,使得如果给定一组PlainTextDocument的IT自动创建Corpus.不幸的是,似乎每个都PlainTextDocument必须单独指定.

例如,如果我有:

foolist <- list(a, b, c); # where a,b,c are PlainTextDocument objects
Run Code Online (Sandbox Code Playgroud)

我这样做是为了得到一个Corpus:

foocorpus <- c(foolist[[1]], foolist[[2]], foolist[[3]]);
Run Code Online (Sandbox Code Playgroud)

我有一个列表的列表'PlainTextDocument,看起来像这样:

> str(sectioned)
List of 154
 $ :List of 6
  ..$ :Classes 'PlainTextDocument', 'TextDocument', 'character'  atomic [1:1] Developing assessment models   Developing models
  .. .. ..- attr(*, "Author")= chr "John Smith"
  .. .. ..- attr(*, "DateTimeStamp")= POSIXlt[1:1], format: "2013-04-30 12:03:49"
  .. .. ..- attr(*, "Description")= chr(0) 
  .. .. …
Run Code Online (Sandbox Code Playgroud)

r list tm

64
推荐指数
3
解决办法
5万
查看次数

Corpus参数上的DocumentTermMatrix错误

我有以下代码:

# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)

news_corpus <- Corpus(VectorSource(news_raw$text)) # a column of strings.

corpus_clean <- tm_map(news_corpus, tolower)
corpus_clean <- tm_map(corpus_clean, removeNumbers)
corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
corpus_clean <- tm_map(corpus_clean, removePunctuation)
corpus_clean <- tm_map(corpus_clean, stripWhitespace)
corpus_clean <- tm_map(corpus_clean, trim)

news_dtm <- DocumentTermMatrix(corpus_clean) # errors here
Run Code Online (Sandbox Code Playgroud)

当我运行该DocumentTermMatrix()方法时,它给了我这个错误:

错误:inherits(doc,"TextDocument")不为TRUE

为什么我会收到此错误?我的行不是文本文件吗?

这是检查时的输出corpus_clean:

[[153]]
[1] obama holds technical school model us

[[154]]
[1] oil boom produces jobs bonanza archaeologists

[[155]] …
Run Code Online (Sandbox Code Playgroud)

r corpus tm

56
推荐指数
3
解决办法
5万
查看次数

使用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-Project没有适用于'meta'的方法应用于类"character"的对象

我正在尝试运行此代码(Ubuntu 12.04,R 3.1.1)

# Load requisite packages
library(tm)
library(ggplot2)
library(lsa)

# Place Enron email snippets into a single vector.
text <- c(
  "To Mr. Ken Lay, I’m writing to urge you to donate the millions of dollars you made from selling Enron stock before the company declared bankruptcy.",
  "while you netted well over a $100 million, many of Enron's employees were financially devastated when the company declared bankruptcy and their retirement plans were wiped out",
  "you sold $101 million worth …
Run Code Online (Sandbox Code Playgroud)

r text-mining tm

32
推荐指数
2
解决办法
4万
查看次数

安装TM软件包时,依赖'slam'不可用

library(tm)在今天加载tm显示时,我能够毫无问题地使用in r :

library(tm)
Run Code Online (Sandbox Code Playgroud)

加载所需的包:NLP

loadNamespace(i,c(lib.loc,.libPaths()),versionCheck = vI [[i]])出错:

没有名为'slam'的包裹

错误:'tm'的包或命名空间加载失败

我认为这是一个依赖问题,并寻求重新安装包

install.packages("tm")
Run Code Online (Sandbox Code Playgroud)

install.packages中的警告:依赖'slam'不可用

然后尝试找到slam:

install.packages("slam")
Run Code Online (Sandbox Code Playgroud)

install.packages中的警告:

包''slam'不可用(对于R版本3.2.4)

我也尝试了一些列的问题,在这里chooseCRANmirror(),仍然没有运气.这个软件包有什么问题,或者只是我的r不允许在今天之后安装'slam'吗?

会话信息

> sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] NLP_0.1-9

loaded via a namespace (and not attached):
[1] parallel_3.2.4 tools_3.2.4 
Run Code Online (Sandbox Code Playgroud)

r tm slam

25
推荐指数
4
解决办法
3万
查看次数

使用多个核时,tm_map转换函数的行为不一致

这篇文章的另一个潜在标题可能是"当r中的并行处理时,核心数量,循环块大小和对象大小之间的比例是否重要?"

我有一个语料库我正在运行一些使用tm包的转换.由于语料库很大,我使用多路并行包进行并行处理.

有时转换完成任务,但有时它们不会.例如,tm::removeNumbers().语料库中的第一个文档的内容值为"n417".因此,如果预处理成功,那么此doc将转换为"n".

下面的样本语料库用于复制.这是代码块:

library(tidyverse)
library(qdap)
library(stringr)
library(tm)
library(textstem)
library(stringi)
library(foreach)
library(doParallel)
library(SnowballC)

  corpus <- (see below)
  n <- 100 # this is the size of each chunk in the loop

  # split the corpus into pieces for looping to get around memory issues with transformation
  nr <- length(corpus)
  pieces <- split(corpus, rep(1:ceiling(nr/n), each=n, length.out=nr))
  lenp <- length(pieces)

  rm(corpus) # save memory

  # save pieces to rds files since not enough RAM
  tmpfile <- tempfile() 
  for (i in …
Run Code Online (Sandbox Code Playgroud)

parallel-processing r text-mining tm doparallel

24
推荐指数
1
解决办法
607
查看次数

主题模型:具有对数似然或困惑的交叉验证

我正在使用主题建模来集群文档.我需要提出最佳的主题数字.因此,我决定使用主题10,20,... 60进行十倍交叉验证.

我已将我的语料库分成十批,并留出一批用于保留集.我使用9个批次(总共180个文档)运行潜在的dirichlet分配(LDA),主题为10到60.现在,我必须计算保持集的困惑或记录可能性.

我从CV的一个讨论会上找到了这段代码.我真的不明白下面的几行代码.我有dtm矩阵使用holdout set(20个文档).但我不知道如何计算这个坚持集的困惑或记录可能性.


问题:

  1. 任何人都可以向我解释seq(2,100,by = 1)在这里意味着什么?那么,AssociatedPress [21:30]是什么意思?这里有什么功能(k)?

    best.model <- lapply(seq(2, 100, by=1), function(k){ LDA(AssociatedPress[21:30,], k) })
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果我想计算称为dtm的保持集的困惑或记录可能性,是否有更好的代码?我知道有perplexity()logLik()函数但是因为我是新手我无法弄清楚如何使用我的保持矩阵(称为dtm)来实现它.

  3. 如何使用包含200个文档的语料库进行十倍交叉验证?是否存在我可以调用的现有代码?我找到caret了这个目的,但也无法弄明白.

r topic-modeling cross-validation tm

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

具有topicmodel的LDA,如何查看不同文档属于哪些主题?

我正在使用topicmodels包中的LDA,我已经在大约30.000个文档上运行它,获得了30个主题,并且获得了主题的前10个单词,它们看起来非常好.但是我想看看哪些文件最有可能属于哪个主题,我该怎么办呢?

myCorpus <- Corpus(VectorSource(userbios$bio))
docs <- userbios$twitter_id
myCorpus <- tm_map(myCorpus, tolower)
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpus <- tm_map(myCorpus, removeNumbers)
removeURL <- function(x) gsub("http[[:alnum:]]*", "", x)
myCorpus <- tm_map(myCorpus, removeURL)
myStopwords <- c("twitter", "tweets", "tweet", "tweeting", "account")

# remove stopwords from corpus
myCorpus <- tm_map(myCorpus, removeWords, stopwords('english'))
myCorpus <- tm_map(myCorpus, removeWords, myStopwords)


# stem words
# require(rJava) # needed for stemming function 
# library(Snowball) # also needed for stemming function 
# a <- tm_map(myCorpus, stemDocument, language = "english")

myDtm <- DocumentTermMatrix(myCorpus, …
Run Code Online (Sandbox Code Playgroud)

r lda topic-modeling tm

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

R中的removeSparseTerms如何工作?

我在R中使用removeSparseTerms方法,它需要输入一个阈值.我还读到,值越高,返回矩阵中保留的项数就越多.

这种方法如何工作以及它背后的逻辑是什么?我理解稀疏性的概念,但这个阈值是否表明一个术语应该出现多少文件,或者其他比例等等?

r lda tm

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

如何确定哪个旧版本的R包与我的R版本兼容

我正在尝试安装"tm"软件包,但后来我收到一条错误消息,说我的R版本"tm"不可用

package ‘tm’ is not available (for R version 3.0.2)
Run Code Online (Sandbox Code Playgroud)

但后来我看到有人建议我从中下载存档版本

http://cran.r-project.org/src/contrib/Archive/tm/?C=M;O=A
Run Code Online (Sandbox Code Playgroud)

然后尝试从源代码安装.

我的问题是如何确定列表中的哪个文件与我的R版本兼容?

r package tm

18
推荐指数
3
解决办法
2772
查看次数