1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで単語の出現頻度を求める。

1
Posted at

はじめに

Rで単語の全文書中の出現頻度を計算しました。
今回はあくまで備忘録です。

何か良いライブラリがありそうですが、見つかりませんでした。
(知っている人がいれば、教えてください・・)

やりたいこと

簡単です。


[["lemon", "apple", "banana", "lemon"], ["orange", "banana"]]

の全文書中の出現頻度を求めるだけです。

ただし、

apple banana lemon orange
1 2 2 1

ではなく、

apple banana lemon orange
1 2 1 1

として計算したいです。(1文書につき1カウント)

やってみた

大したことはしていません。
(Rはそこまで詳しくないので、もっと良い方法があれば教えてください・・)

まず、普通にtableで集計すると、

xxx <- list(c("lemon", "apple", "banana", "lemon"),c("orange", "banana"))
table(unlist(xxx))

 apple banana  lemon orange 
     1      2      2      1 

となってしまいます。

今回は、文書ごとにunique関数を適用してから集計することで対応しました。

xxx.distinct <- lapply(xxx, function(p){unlist(unique(p))})
table(unlist(xxx.distinct))

 apple banana  lemon orange 
     1      2      1      1

おわりに

とりあえずやってみましたが、素敵なライブラリがあると嬉しいです。
自然言語処理用の良いライブラリ等があれば教えて頂けると幸いです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?