16

我正在使用 R 包tm,我想做一些文本挖掘。这是一个文档,被视为一袋单词。

我不了解有关如何加载文本文件和创建必要对象以开始使用诸如...等功能的文档。

stemDocument(x, language = map_IETF(Language(x)))

所以假设这是我的文档“这是对 R 负载的测试”

如何加载数据以进行文本处理并创建对象 x?

4

6 回答 6

23

像@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-11-09T07:39:31.113 回答
7

你不能只使用readPlain同一个库中的函数吗?或者您可以只使用更常见的scan功能。

mydoc.txt <-scan("./mydoc.txt", what = "character")
于 2011-10-28T09:38:09.437 回答
6

实际上,我一开始就觉得这很棘手,所以这里有一个更全面的解释。

首先,您需要设置文本文档的来源。我发现最简单的方法(特别是如果您计划添加更多文档,是创建一个目录源来读取您的所有文件。

source <- DirSource("yourdirectoryname/") #input path for documents
YourCorpus <- Corpus(source, readerControl=list(reader=readPlain)) #load in documents

然后,您可以将 StemDocument 功能应用于您的语料库。HTH。

于 2011-10-28T09:48:00.537 回答
2

我相信您想要做的是将单个文件读入语料库,然后使其将文本文件中的不同行视为不同的观察结果。

看看这是否给了你想要的东西:

text <- read.delim("this is a test for R load.txt", sep = "/t")
text_corpus <- Corpus(VectorSource(text), readerControl = list(language = "en"))

这是假设文件“this is a test for R load.txt”只有一列包含文本数据。

这里的“text_corpus”是您正在寻找的对象。

希望这可以帮助。

于 2013-06-23T06:34:50.793 回答
0

这是我对每个观察一行的文本文件的解决方案。tm 上的最新小插曲(2017 年 2 月)提供了更多细节。

text <- read.delim(textFileName, header=F, sep = "\n",stringsAsFactors = F)
colnames(text) <- c("MyCol")
docs <- text$MyCol
a <- VCorpus(VectorSource(docs))
于 2017-06-22T18:04:34.340 回答
0

以下假设您有一个文本文件目录,您想从中创建一个词袋。

唯一需要进行的更改是替换 path = "C:\\windows\\path\\to\\text\\files\\ 为您的目录路径。

library(tidyverse)
library(tidytext)

# create a data frame listing all files to be analyzed
all_txts <- list.files(path = "C:\\windows\\path\\to\\text\\files\\",   # path can be relative or absolute
                       pattern = ".txt$",  # this pattern only selects files ending with .txt
                       full.names = TRUE)  # gives the file path as well as name

# create a data frame with one word per line
my_corpus <- map_dfr(all_txts, ~ tibble(txt = read_file(.x)) %>%   # read in each file in list
                      mutate(filename = basename(.x)) %>%   # add the file name as a new column
                      unnest_tokens(word, txt))   # split each word out as a separate row

# count the total # of rows/words in your corpus
my_corpus %>%
  summarize(number_rows = n())

# group and count by "filename" field and sort descending
my_corpus %>%
  group_by(filename) %>%
  summarize(number_rows = n()) %>%
  arrange(desc(number_rows))

# remove stop words
my_corpus2 <- my_corpus %>%
  anti_join(stop_words)

# repeat the count after stop words are removed
my_corpus2 %>%
  group_by(filename) %>%
  summarize(number_rows = n()) %>%
  arrange(desc(number_rows))
于 2020-02-20T14:15:30.150 回答