DEV Community

Toru Furukawa
Toru Furukawa

Posted on

2

入門自然言語処理 pp.195-198

5.2 タグ付きコーパスの導入。単語とタグのタプルとして、コーパスが格納されている。tagged_words() メソッドでイテレートできる。

tagged_sents() なら、 [[(word, tag), (word, tag), ...], [...], ...] として文ごとにイテレートできる。

Python 3.6 + matplotlib==2.1.1 では、p.198 のプロットするサンプルでエラーが出る。FreqDict のキーに「PP$」のように $ が入っていて、この $をパースできないようだ。ここで根本原因を解決するのは本来の目的ではないので、適当に済ませる。

import nltk
tagged_words = nltk.corpus.brown.tagged_words(categories="news")
dist = nltk.FreqDist(tag.replace("$", "") for (word, tag) in tagged_words)
dist.plot()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay