【问题标题】:list of word frequencies using R使用 R 的词频列表
【发布时间】:2013-08-08 16:35:48
【问题描述】:

我一直在使用 tm 包来运行一些文本分析。 我的问题是创建一个包含单词及其频率的列表

library(tm)
library(RWeka)

txt <- read.csv("HW.csv",header=T) 
df <- do.call("rbind", lapply(txt, as.data.frame))
names(df) <- "text"

myCorpus <- Corpus(VectorSource(df$text))
myStopwords <- c(stopwords('english'),"originally", "posted")
myCorpus <- tm_map(myCorpus, removeWords, myStopwords)

#building the TDM

btm <- function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3))
myTdm <- TermDocumentMatrix(myCorpus, control = list(tokenize = btm))

我通常使用以下代码来生成频率范围内的单词列表

frq1 <- findFreqTerms(myTdm, lowfreq=50)

有没有什么办法可以自动完成,这样我们就可以得到一个包含所有单词及其频率的数据框?

我面临的另一个问题是将术语文档矩阵转换为数据框。当我处理大量数据样本时,我遇到了内存错误。 有没有简单的解决方案?

【问题讨论】:

    标签: r text-mining word-frequency term-document-matrix


    【解决方案1】:

    试试这个

    data("crude")
    myTdm <- as.matrix(TermDocumentMatrix(crude))
    FreqMat <- data.frame(ST = rownames(myTdm), 
                          Freq = rowSums(myTdm), 
                          row.names = NULL)
    head(FreqMat, 10)
    #            ST Freq
    # 1       "(it)    1
    # 2     "demand    1
    # 3  "expansion    1
    # 4        "for    1
    # 5     "growth    1
    # 6         "if    1
    # 7         "is    2
    # 8        "may    1
    # 9       "none    2
    # 10      "opec    2
    

    【讨论】:

    • 非常感谢您!给初学者的一个注意事项:myTdm
    • 我是这么认为的。刚开始时,R 有时会让人很困惑,所以为 R 新手添加它。
    • 如果你这样做就足够了:FreqMat &lt;- as.data.frame(as.table(myTdm))
    • 当我尝试 inspect() 时,无论 tdm/dtm 的大小如何,我都只会退出 [1:10,1:10]。
    • @user1603472 如果您选择myTdm &lt;- TermDocumentMatrix(crude),您将获得完整视图。
    【解决方案2】:

    我在R中有以下几行可以帮助创建词频并将它们放在一个表格中,它读取.txt格式的文本文件并创建词频,我希望这可以帮助任何有兴趣的人.

    avisos<- scan("anuncio.txt", what="character", sep="\n")
    avisos1 <- tolower(avisos)
    avisos2 <- strsplit(avisos1, "\\W")
    avisos3 <- unlist(avisos2)
    freq<-table(avisos3)
    freq1<-sort(freq, decreasing=TRUE)
    temple.sorted.table<-paste(names(freq1), freq1, sep="\\t")
    cat("Word\tFREQ", temple.sorted.table, file="anuncio.txt", sep="\n")
    

    【讨论】:

    • 这对我在文本挖掘方面的一个小型宠物项目非常有帮助.. 非常感谢 :))
    • 另外,一个问题..如果我想在文本转储中计算特定短语或句子的频率,有没有办法做到这一点?例如:假设我想在整本书中找到“多么奇怪的事件”这组词的频率。我应该对上面的代码做哪些更改?
    【解决方案3】:

    查看findFreqTermssource,似乎函数slam::row_sums 在术语-文档矩阵上调用时可以解决问题。试一试:

    data(crude)
    slam::row_sums(TermDocumentMatrix(crude))
    

    【讨论】:

      【解决方案4】:

      根据您的需要,使用一些tidyverse 函数可能是一个粗略的解决方案,它在处理大小写、标点和停用词方面提供了一些灵活性:

      text_string <- 'I have been using the tm package to run some text analysis. My problem is with creating a list with words and their frequencies associated with the same. I typically use the following code for generating list of words in a frequency range. Is there any way to automate this such that we get a dataframe with all words and their frequency?
      The other problem that i face is with converting the term document matrix into a data frame. As i am working on large samples of data, I run into memory errors. Is there a simple solution for this?'
      
      stop_words <- c('a', 'and', 'for', 'the') # just a sample list of words I don't care about
      
      library(tidyverse)
      data_frame(text = text_string) %>% 
        mutate(text = tolower(text)) %>% 
        mutate(text = str_remove_all(text, '[[:punct:]]')) %>% 
        mutate(tokens = str_split(text, "\\s+")) %>%
        unnest() %>% 
        count(tokens) %>% 
        filter(!tokens %in% stop_words) %>% 
        mutate(freq = n / sum(n)) %>% 
        arrange(desc(n))
      
      
      # A tibble: 64 x 3
        tokens      n   freq
        <chr>   <int>  <dbl>
      1 i           5 0.0581
      2 with        5 0.0581
      3 is          4 0.0465
      4 words       3 0.0349
      5 into        2 0.0233
      6 list        2 0.0233
      7 of          2 0.0233
      8 problem     2 0.0233
      9 run         2 0.0233
      10 that       2 0.0233
      # ... with 54 more rows
      

      【讨论】:

        【解决方案5】:
        a = scan(file='~/Desktop//test.txt',what="list")
        a1 = data.frame(lst=a)
        count(a1,vars="lst")
        

        似乎可以得到简单的频率。我使用了 scan 因为我有一个 txt 文件,但它也应该与 read.csv 一起使用。

        【讨论】:

        • 以上内容并不能帮助我弄清楚 n 克和单词关联。我有兴趣评估已生成的 n 克的频率
        【解决方案6】:

        apply(myTdm, 1, sum)rowSums(as.matrix(myTdm)) 是否提供您所追求的 ngram 计数?

        【讨论】:

          猜你喜欢
          • 2015-09-23
          • 2020-07-23
          • 1970-01-01
          • 2019-04-19
          • 1970-01-01
          • 2022-12-19
          • 1970-01-01
          • 2010-11-14
          • 1970-01-01
          相关资源
          最近更新 更多