我正在尝试从包含少于5个单词的数据框中删除行.例如
mydf <- as.data.frame(read.xlsx("C:\\data.xlsx", 1, header=TRUE)
head(mydf)
NO ARTICLE
1 34 The New York Times reports a lot of words here.
2 12 Greenwire reports a lot of words.
3 31 Only three words.
4 2 The Financial Times reports a lot of words.
5 9 Greenwire short.
6 13 The New York Times reports a lot of words again.
Run Code Online (Sandbox Code Playgroud)
我想删除5个或更少单词的行.我怎样才能做到这一点?
我正在尝试编写代码来构建一个表格,该表格显示语料库中所有单词之间的所有相关性。
我知道我可以findAssocs在tm包中使用来查找单个单词的所有单词相关性,即findAssocs(dtm, "quick", 0.5)- 会给我所有与 0.5 以上的单词“quick”相关的单词,但我不想手动为每个单词执行此操作我所拥有的文字中的词。
#Loading a .csv file into R
file_loc <- "C:/temp/TESTER.csv"
x <- read.csv(file_loc, header=FALSE)
require (tm)
corp <- Corpus(DataframeSource(x))
dtm <- DocumentTermMatrix(corp)
#Clean up the text
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(stripWhitespace))
dtm <- DocumentTermMatrix(corp)
Run Code Online (Sandbox Code Playgroud)
从这里我可以找到单个单词的单词相关性:
findAssocs(dtm, "quick", 0.4)
Run Code Online (Sandbox Code Playgroud)
但我想找到所有这样的相关性:
quick easy the and
quick 1.00 0.54 0.72 0.92
easy 0.54 1.00 0.98 0.54
the 0.72 0.98 1.00 …Run Code Online (Sandbox Code Playgroud) 我在mac中下载了PDFtoText,并编写了以下代码将pdf文件转换为文本:
pdf_to_load =("~/my_directory/my.pdf")
system(paste('pdftotext', pdf_to_load))
Run Code Online (Sandbox Code Playgroud)
代码运行良好,但我无法在源目录中看到my.txt,也没有将其保存在文件夹中的任何位置.哪里出错了?
我的一位导师能够在他的计算机上运行相同的代码,他能够看到转换后的.txt文件.
请指导.
我是 R 新手,正在尝试使用以空格分隔的文本文件进行 wordcloud 。我已经安装tm并tmap打包了。我收到以下错误:
错误:找不到函数“Corpus”
错误:找不到函数“tm_map”
有人可以帮我吗?
请参阅下面的 MWE,自定义的标记生成器不起作用,为什么?tm包版本是0.71
library(tm)
ts <- c("This is a testimonial")
corpDs <- Corpus(VectorSource(ts))
#This is not working
ownTokenizer <- function(x) unlist(strsplit(as.character(x), "i+"))
tdm <- DocumentTermMatrix(corpDs,control=list(tokenize=ownTokenizer))
as.matrix(tdm)
#This is working
ownTokenizer(ts)
Run Code Online (Sandbox Code Playgroud)
输出:
条款
文档证明了这一点
1 1 1
[1]“Th”“s”“sa 测试”“mon”“al”
谢谢你,
托比亚斯
我试图绘制一个单词的最高相关性.例如,我想绘制"鲸鱼"这个词的最高十个相关性.有人可以用这个命令帮助我吗?如果有帮助,我安装了RGraphViz.
s.dir1<-"/PATHTOTEXT/MobyDickTxt"
s.cor1<-Corpus(DirSource(s.dir1), readerControl=list(reader=readPlain))
s.cor1<-tm_map(s.cor1, removePunctuation)
s.cor1<-tm_map(s.cor1, stripWhitespace)
s.cor1<-tm_map(s.cor1, tolower)
s.cor1<-tm_map(s.cor1, removeNumbers)
s.cor1<-tm_map(s.cor1, removeWords, stopwords("english"))
tdm1 <- TermDocumentMatrix(s.cor1)
m1 <- as.matrix(tdm)
v1 <- sort(rowSums(m), decreasing=TRUE)
d1 <- data.frame(word = names(v),freq=v)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用众所周知的Reuters-21578数据集进行一些工作,并且在将sgm文件加载到我的语料库时遇到了一些麻烦.
现在我正在使用该命令
require(tm)
reut21578 <- system.file("reuters21578", package = "tm")
reuters <-Corpus(DirSource(reut21578),
readerControl = list(reader = readReut21578XML))
Run Code Online (Sandbox Code Playgroud)
试图将所有文件都包含在我的语料库中,但这会给我以下错误:
Error in DirSource(reut21578) : empty directory
Run Code Online (Sandbox Code Playgroud)
知道我可能会出错吗?
嘿伙计们我在传导LDA方面有点麻烦,因为出于某种原因,一旦我准备好进行分析,我就会出错.我会尽我所能去完成我正在做的事情,遗憾的是我无法提供数据,因为我使用的数据是专有数据.
dataset <- read.csv("proprietarydata.csv")
首先,我做了一些清理数据$ text和post是类字符
dataset$text <- as.character(dataset$text)
post <- gsub("[^[:print:]]"," ",data$Post.Content)
post <- gsub("[^[:alnum:]]", " ",post)
Run Code Online (Sandbox Code Playgroud)
帖子最终看起来像这样:`
`[1] "here is a string"
[2] "here is another string"
etc....`
Run Code Online (Sandbox Code Playgroud)
然后我创建了以下功能,它可以进行更多清洁:
createdtm <- function(x){
myCorpus <- Corpus(VectorSource(x))
myCorpus <- tm_map(myCorpus,PlainTextDocument)
docs <- tm_map(myCorpus,tolower)
docs <- tm_map(docs, removeWords, stopwords(kind="SMART"))
docs <- tm_map(docs, removeWords, c("the"," the","will","can","regards","need","thanks","please","http"))
docs <- tm_map(docs, stripWhitespace)
docs <- tm_map(docs, PlainTextDocument)
return(docs)}
predtm <- createdtm(post)
Run Code Online (Sandbox Code Playgroud)
这最终会返回一个语料库,为每个文档提供这样的内容:
[[1]]
<<PlainTextDocument (metadata: 7)>>
Here text string
[[2]]
<<PlainTextDocument (metadata: 7)>>
Here another …Run Code Online (Sandbox Code Playgroud) documents <- c("This is document number one", "document two is the second element of the vector")
Run Code Online (Sandbox Code Playgroud)
我试图创建的数据框是:
idealdf <- c("this", "is", "document", "number", "one", "document", "two", "is", "the", "second", "element", "of", "the", "vector")
Run Code Online (Sandbox Code Playgroud)
我一直在使用 tm 包将我的文档转换为语料库,并通过以下功能去除标点符号、转换为小写字母等:
#create a corpus:
myCorpus <- Corpus(VectorSource(documents))
#convert to lowercase:
myCorpus <- tm_map(myCorpus, content_transformer(tolower))
#remove punctuation:
myCorpus <- tm_map(myCorpus, removePunctuation)
Run Code Online (Sandbox Code Playgroud)
...但我在尝试将其放入 df 时遇到了麻烦,其中每个单词都有自己的行(我更喜欢每个单词都有自己的行 - 即使同一个单词显示为多行)。
谢谢。
我正在尝试tm在IBM的数据科学体验(DSX)上安装该软件包:
install.packages("tm")
Run Code Online (Sandbox Code Playgroud)
但是,我正在解决这个问题:
"dependency 'slam' is not available"
Run Code Online (Sandbox Code Playgroud)
这篇文章表明R版本3.3.1 will解决了这个问题,但是DSX上的R版本是:R version 3.3.0 (2016-05-03)
如何在IBM DSX上解决此问题?请注意,您在DSX上没有root访问权限.
我在stackoverflow上看到了类似的问题,但没有人问如何解决IBM DSX上的问题,例如在安装TM软件包时依赖'slam'不可用
更新:
install.packages("slam")
Run Code Online (Sandbox Code Playgroud)
返回:
Installing package into '/gpfs/global_fs01/sym_shared/YPProdSpark/user/s85d-88ebffb000cc3e-39ca506ba762/R/libs'
(as 'lib' is unspecified)
Warning message:
"package 'slam' is not available (for R version 3.3.0)"
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码创建文档术语矩阵.我创建矩阵没有问题,但当我尝试删除稀疏术语或查找常用术语时,我收到错误.
text<- c("Since I love to travel, this is what I rely on every time.",
"I got this card for the no international transaction fee",
"I got this card mainly for the flight perks",
"Very good card, easy application process",
"The customer service is outstanding!")
library(tm)
corpus<- Corpus(VectorSource(text))
corpus<- tm_map(corpus, content_transformer(tolower))
corpus<- tm_map(corpus, removePunctuation)
corpus<- tm_map(corpus, removeWords, stopwords("english"))
corpus<- tm_map(corpus, stripWhitespace)
dtm<- as.matrix(DocumentTermMatrix(corpus))
Run Code Online (Sandbox Code Playgroud)
结果如下:
Docs application card customer easy every ... etc.
1 0 0 0 1 0
2 0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试从一本意大利语书中获取文档术语矩阵。我有这本书的pdf文件,我写了几行代码:
\n#install.packages("pdftools")\nlibrary(pdftools)\nlibrary(tm)\ntext <- pdf_text("IoRobot.pdf")\n# collapse pdf pages into 1\ntext <- paste(unlist(text), collapse ="")\nmyCorpus <- VCorpus(VectorSource(text))\nmydtm <-DocumentTermMatrix(myCorpus,control = list(removeNumbers = TRUE, removePunctuation = TRUE,\n stopwords=stopwords("it"), stemming=TRUE))\ninspect(mydtm)\nRun Code Online (Sandbox Code Playgroud)\n我在最后一行之后得到的结果是:
\n<<DocumentTermMatrix (documents: 1, terms: 10197)>>\nNon-/sparse entries: 10197/0\nSparsity : 0%\nMaximal term length: 39\nWeighting : term frequency (tf)\nSample :\n Terms\nDocs calvin cosa donovan esser pi\xc3\xba poi powel prima quando robot\n 1 201 191 254 193 288 211 287 166 184 62\nRun Code Online (Sandbox Code Playgroud)\n我注意到稀疏度是 0%。这是正常的吗?
\n