Функция tm::Corpus()
, используемая с DirSource()
, обрабатывает каждый файл как отдельный документ, а не каждую строку в одном файле как отдельный документ.
Чтобы прочитать каждую строку текстового файла как отдельный документ, можно использовать синтаксис Corpus(VectorSource())
.
В качестве примера мы создадим текстовый файл, прочитав его из каталога, чтобы проиллюстрировать, как Corpus()
ведет себя с DirSource()
, в отличие от того, как мы будем читать его с VectorSource()
.
# represent contents of the text file that was stored in
# ./data/ExcelFile1.csv
aTextFile <- "This is line one of text.
This is line two of text. This is a second sentence in line two."
library(tm)
# read as the OP read it
corpusDir <- "./data/textMining"
aCorpus <- Corpus(DirSource(corpusDir))
length(aCorpus) # shows only one item in list, entire file
# use pipe as separator because documents include commas.
aDataFrame <- read.table("./data/textMining/ExcelFile1.csv",header=FALSE,
sep="|",stringsAsFactors=FALSE)
# use VectorSource to treat each row as a separate document
aCorpus <- Corpus(VectorSource(aDataFrame$V1))
# print the two documents
aCorpus[1]$content
aCorpus[2]$content
... и вывод.Во-первых, длина корпуса в том виде, как мы его читаем с помощью DirSource()
:
> length(aCorpus) # shows only one item in list, entire file
[1] 1
Во-вторых, мы напечатаем две строки из второго чтения, иллюстрируя, что они рассматриваются как отдельные документы.
> aCorpus <- Corpus(VectorSource(aDataFrame$V1))
> aCorpus[1]$content
[1] "This is line one of text."
> aCorpus[2]$content
[1] "This is line two of text. This is a second sentence in line two. "
>