Skip to content

Instantly share code, notes, and snippets.

@riywo
Created September 29, 2011 17:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riywo/1251364 to your computer and use it in GitHub Desktop.
Save riywo/1251364 to your computer and use it in GitHub Desktop.
形態素解析した結果を数えるとか

りすこがこんなの書いてて、聞いてみたら名詞だけ抜き出してcountとかやりたいとか。 そういう時はawk使うと便利ですよ(perlでもいいですが)

$ cat filename | awk '$4 ~ /^名詞/{print $1}' | sort | uniq -c | sort -n

4個目のカラムが「品詞」で始まる性質を使ってます。


あと、MeCabはライブラリなのでローカルに入れちゃうのも手です。 簡単なのはHomebrewを使える様にして (多分Xcode入れる必要があって、それがちょっとハードル高いかも)

$ brew install mecab mecab-ipadic

あとは解析したいファイルをhoge.txtとすると

$ mecab -Ochasen hoge.txt | awk '$4 ~ /^名詞/{print $1}' | sort | uniq -c | sort -n

みたいにやると、mecabのパラメータとかも自由自在なのでいい感じですねぇ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment