0
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.

MARISA Trie でコーパスの語彙サイズや未知語率を調べる

Last updated at Posted at 2018-12-04

通常の大きさのコーパスであれば、sort コマンドや uniq コマンドを使って、語彙サイズなどを調査することが可能です。例えば、1行1文形式で格納されているコーパスの語彙サイズを調査する方法は以下の通りです。

最初に、1行1単語形式のファイルを作ります。

$ mecab < train_corpus.txt > train_corpus.mecab
$ grep -v EOS train_corpus.mecab | cut -f1 > train_corpus.tokens

1行1単語形式のファイルが作成できれば、後は簡単です。各単語の出現頻度を調べるには、以下のようにします。

$ sort train_corpus.tokens | uniq -c

語彙サイズを調べるには、以下のようにします。

$ sort train_corpus.tokens | uniq | wc -l

ただ、コーパスが大きくなると、sort コマンドの実行時間が無視できなくなってきます。そのような場合は、MARISA Trie のツールを使うと高速に調査することが可能です。なお、Debian GNU/Linux 環境では MARISA Trie のツールは、以下のコマンドで容易にインストールできます。

$ sudo apt install marisa

最初に、trie を作成します。

$ marisa-build < train_corpus.tokens > train_corpus.trie

語彙サイズを調べるには、以下のように trie に登録されている語彙を全て出力します。

$ marisa-dump train_corpus.trie > train_corpus.vocab
input: train_corpus.trie
# keys: 625
$ wc -l train_corpus.vocab
625

実のところ、marisa-dump コマンドは、標準エラー出力に trie に登録されている語彙サイズを出力してくれるので、wc コマンドは不要ですが。

次に、学習コーパス train_corpus.txt の語彙を既知語として、テストコーパス test_corpus.txt の未知語頻度などを調べてみます。この時は、marisa-lookup コマンドを使います。marisa-lookup コマンドは、標準入力から1行1単語形式で検索対象トークンを受け取り、標準出力に検索結果を出力します。特に、trie に登録されていない未知語に対しては「-1」を出力します。したがって、未知語頻度を調べるには、「-1」の行数を調べれば良いわけです。

$ marisa-lookup train_corpus.trie < test_corpus.tokens | grep -- -1 | wc -l
554

異なりの未知語率を調べる場合は、test_corpus.tokens ではなく、語彙に含まれる各単語が1回だけ出現する test_corpus.vocab を使えば良いことになります。

$ marisa-build < test_corpus.tokens > test_corpus.trie
# keys: 633
# nodes: 847
size: 7112
$ marisa-dump test_corpus.trie > test_corpus.vocab
input: test_corpus.trie
# keys: 633
$ marisa-lookup train_corpus.trie < test_corpus.vocab | grep -- -1 | wc -l
381
0
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
0
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?