我正在使用R包tm,我想做一些文本挖掘。这是一个文档,并被视为一袋单词。
我不理解有关如何加载文本文件以及如何创建必要的对象以开始使用以下功能的文档。
stemDocument(x, language = map_IETF(Language(x)))假设这是我的文档"this is a test for R load“
如何加载用于文本处理的数据并创建对象x?
发布于 2011-11-09 15:39:31
就像@richiemorrisroe一样,我发现这方面的记录很少。下面是我如何将文本与tm包一起使用,并创建文档术语矩阵:
library(tm) #load text mining library
setwd('F:/My Documents/My texts') #sets R's working directory to near where my files are
a <-Corpus(DirSource("/My Documents/My texts"), readerControl = list(language="lat")) #specifies the exact folder where my text file(s) is for analysis with tm.
summary(a) #check what went in
a <- tm_map(a, removeNumbers)
a <- tm_map(a, removePunctuation)
a <- tm_map(a , stripWhitespace)
a <- tm_map(a, tolower)
a <- tm_map(a, removeWords, stopwords("english")) # this stopword file is at C:\Users\[username]\Documents\R\win-library\2.13\tm\stopwords
a <- tm_map(a, stemDocument, language = "english")
adtm <-DocumentTermMatrix(a)
adtm <- removeSparseTerms(adtm, 0.75)在这种情况下,您不需要指定确切的文件名。只要它是第3行中引用的目录中唯一的一个,它就会被tm函数使用。我之所以这样做,是因为我在第3行指定文件名时没有任何成功。
如果有人能建议如何将文本放入lda包中,我将不胜感激。我完全不能解决这个问题。
发布于 2011-10-28 17:38:09
不能只使用同一个库中的函数readPlain吗?或者,您可以只使用更常见的scan函数。
mydoc.txt <-scan("./mydoc.txt", what = "character")发布于 2011-10-28 17:48:01
实际上,我发现这在一开始就相当棘手,所以这里有一个更全面的解释。
首先,您需要为文本文档设置一个源。我发现最简单的方法(尤其是如果您计划添加更多文档)是创建一个将读取所有文件的目录源。
source <- DirSource("yourdirectoryname/") #input path for documents
YourCorpus <- Corpus(source, readerControl=list(reader=readPlain)) #load in documents然后,您可以将StemDocument函数应用于语料库。HTH。
https://stackoverflow.com/questions/7927367
复制相似问题