从语料库到R的个人.text文件

时间:2014-10-16 04:10:45

标签: r text-mining tm corpus text-analysis

我有一个包含6000行和2列的.csv文件。我想将每一行写为单独的文本文件。有关如何在tm中完成此操作的任何想法?我试过writeCorpus()但是这个函数只吐出了150个.txt文件而不是6000个。这是一个内存问题还是代码我做错了什么?

 library(tm)
 revs<-read.csv("dprpfinals.csv",header=TRUE)
 corp<-Corpus(VectorSource(revs$Review))
 writeCorpus(corp,path=".",filenames=paste(seq_along(revs),".txt",sep=""))

2 个答案:

答案 0 :(得分:0)

以下是将文本拆分为段落,删除空行以及将行写入文本文件的示例。然后你需要处理文本文件。

txt="Argument split will be coerced to character, so you will see uses with split = NULL to mean split = character(0), including in the examples below.

Note that splitting into single characters can be done via split = character(0) ; the two are equivalent. The definition of 'character’ here depends on the locale: in a single-byte locale it is a byte, and in a multi-byte locale it is the unit represented by a ‘wide character’ (almost always a Unicode code point).

A missing value of split does not split the corresponding element(s) of x at all."

txt2<-data.frame(para = strsplit(txt, "\n")[[1]],stringsAsFactors=FALSE)
txt3<-txt2[txt2$para!="",]

npara = length(txt3)
for (ip in seq(1,npara)) {
  fname = paste("paragraph_",ip,".txt",sep="")
  fileConn<-file(fname)
  writeLines(txt3[ip], fileConn)
  close(fileConn)  
}

答案 1 :(得分:0)

无需使用tm,这是一个可重现的示例,它创建一个包含6000行和两列的CSV文件,将其读入,然后将其转换为6000个txt文件

首先为示例准备一些数据......

# from http://hipsum.co/?paras=4&type=hipster-centric
txt <- "Brunch single-origin coffee photo booth, meggings fixie stumptown pickled mumblecore slow-carb aesthetic ennui Odd Future blog plaid Bushwick. Seitan keffiyeh hashtag Portland, kitsch irony authentic vegan post-ironic. Actually pop-up flexitarian kale chips ethical authentic, stumptown meggings. Photo booth Helvetica farm-to-table Neutra. Selfies blog swag, lomo viral meh chillwave distillery deep v Truffaut. Squid Cosby sweater irony, art party mustache Vice Wes Anderson Bushwick McSweeney's locavore roof party paleo. 3 wolf moon salvia gentrify, taxidermy street art banh mi Portland deep v small batch Truffaut."

# get n random samples of this paragraph
n <- 6000
txt_split <- unlist(strsplit(txt, split = " "))
txts <- sapply(1:n, function(i) paste(sample(txt_split, 10, replace = TRUE), 
                                             collapse  = " "))

# make dataframe then CSV file, two cols, n rows.
my_csv <- data.frame( col_one = 1:n,
                      col_two = txts)
write.csv(my_csv, "my_csv.csv", row.names = FALSE, quote = TRUE)

现在我们有一个可能类似于您所拥有的CSV文件,我们可以在其中阅读:

# Read in the CSV file...

x <- read.csv("my_csv.csv", header = TRUE, stringsAsFactors = FALSE)

现在我们可以将CSV文件的每一行写入单独的文本文件(它们将出现在您的工作目录中):

# Write each row of the CSV to a txt file
sapply(1:nrow(x), function(i) write.table(paste(x[i,], collapse = " "), 
                                          paste0("my_txt_", i, ".txt"), 
                                          col.names = FALSE, row.names = FALSE))

如果你真的想使用tm,那么你就是在正确的轨道上,这对我来说很好:

# Read in the CSV file...
x <- read.csv("my_csv.csv", header = TRUE, stringsAsFactors = FALSE)
library(tm)
my_corpus <- Corpus(DataframeSource(x))
writeCorpus(my_corpus)

更接近你的例子也适合我:

corp <- Corpus(VectorSource(x$col_one))
writeCorpus(corp)

如果它不适合你,那么你的CSV文件可能会有些不寻常,一些奇怪的字符等等。没有关于您的具体问题的更多细节,很难说。