web-dev-qa-db-ja.com

Rを使用した単語頻度のリスト

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)

これを自動化して、すべての単語とその頻度を含むデータフレームを取得する方法はありますか?

Iが直面するもう1つの問題は、ドキュメントマトリックスという用語をデータフレームに変換することです。データの大規模なサンプルで作業しているため、メモリエラーが発生します。これに簡単な解決策はありますか?

18
ProcRJ

これを試して

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
19
David Arenburg

findFreqTermsの-​​ source を見ると、関数slam::row_sumsは、用語ドキュメントマトリックスで呼び出されたときにトリックを実行します。例えば、試してみてください:

data(crude)
slam::row_sums(TermDocumentMatrix(crude))
9
Daniel Janus

Rに次の行があり、Wordの頻度を作成してテーブルに入れることができます。テキストのファイルを.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")
8
alejandro

ニーズに応じて、いくつかの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
3
sbha

apply(myTdm, 1, sum)またはrowSums(as.matrix(myTdm))は、後のngramカウントを提供しますか?

2
Ben
a = scan(file='~/Desktop//test.txt',what="list")
a1 = data.frame(lst=a)
count(a1,vars="lst")

単純な周波数を取得するように動作するようです。 txtファイルがあるのでスキャンを使用しましたが、read.csvでも動作するはずです。

2
Tahnoon Pasha