【问题标题】:R text file and text mining...how to load dataR文本文件和文本挖掘...如何加载数据
【发布时间】:2011-12-17 03:53:00
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: r load text-mining tm


    【解决方案1】:

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

    mydoc.txt <-scan("./mydoc.txt", what = "character")
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

        像@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 包中,我将不胜感激。我根本无法解决这个问题。

        【讨论】:

        • 我刚刚发现,除非指定语言,否则 stemDocument 函数似乎根本不起作用,所以我编辑了上面的代码以包含它。
        【解决方案4】:

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

        看看这是否能满足你的需求:

        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”是您要查找的对象。

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

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

          text <- read.delim(textFileName, header=F, sep = "\n",stringsAsFactors = F)
          colnames(text) <- c("MyCol")
          docs <- text$MyCol
          a <- VCorpus(VectorSource(docs))
          

          【讨论】:

            【解决方案6】:

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

            唯一需要做的改变是替换 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))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-01
              • 2011-02-07
              • 2015-07-17
              • 2010-12-07
              • 1970-01-01
              • 2017-04-10
              相关资源
              最近更新 更多