LoginSignup
4
4

More than 3 years have passed since last update.

Janomeを使って品詞の出力頻度を調べる。

Last updated at Posted at 2019-05-26

Janomeをインストールする

$ pip install janome

読み込んだテキストファイルを形態素解析して、名詞をカウント

sample.py
from janome.tokenizer import Tokenizer
import zipfile
import os.path, urllib.request as req
import sys
from sys import argv
import time 

# テキスト読み込む
input_file_path = sys.argv[1]
part_of_speech = sys.argv[2]
f = open(input_file_path,'r',encoding = 'utf-8')
txt = f.read()

# 形態素解析オブジェクトの生成
t = Tokenizer()

# テキストを一行ずつ処理                    
word_dic = {}
lines = txt.split("\r\n")
for line in lines:
    malist = t.tokenize(line)
    for w in malist:
        word = w.surface
        # 品詞
        ps = w.part_of_speech
        # 名詞だけカウントする
        if ps.find(part_of_speech) < 0: 
            continue
        if not word in word_dic:
            word_dic[word] = 0
        word_dic[word] += 1

# 頻出単語を表示                                                                                    
keys = sorted(word_dic.items(), key=lambda x:x[1], reverse=True)
for word, cnt in keys[:50]:
    print("{0}({1}) ".format(word, cnt), end="")
$ python sample.py ファイル名 品詞

出力結果(名詞)

仕事(176) こと(155) 1(151) の(143) 円(134) ((129) 外(118) 利用(117) ,(113) 税(112) detail(107) https(106) jp(106) ://(104) chiebukuro(98) co(97) yahoo(96) 検査(92) 人(89) =(80) お客様(79) 名(75) 000(72) サービス(70) &(70) 人材(69) 当社(68) 場合(68) 2(64) 数(62) q(61) 会社(60) |(60) 私(59) よう(58) 採用(55) 精神(54) ]((54) ご(52) 時間(52) 何(51) qa(51) %(51) お(50) question(49)

出力結果(形容詞)

いい(37) ない(35) 良い(27) なく(14) 辛い(14) 無い(11) 欲しい(9) 問題(8) 若い(7) 甘い(6) 少なく(5) 大きく(5) 悪い(5) 仕方(5) 辛く(5) 良く(5) 高い(4) 楽しく(4) 多い(4) 新しい(4) すごく(4) 多く(4) 難しい(4) 怖い(4) 少ない(3) 悪く(3) 無く(3) 強く(3) 早く(3) 遅く(3) ふさわしい(3) 楽しい(3) 難い(3) なし(3) 低い(3) よく(3) 永く(2) ほしい(2) 悪かっ(2) 大きい(2) 少なから(2) 限り(2) 等しく(2) うまく(2) 暑い(2) 忙しい(2) しょうが(2) づらい(2) つらい(2) 酷い(2)

参考

形態素解析のライブラリ「Mecab」と「Janome」を使ってみよう - 日々の学びのアウトプットするブログ

4
4
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
4
4