1
2

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.

日本語による文章表現の特徴を抽出するプログラム

Posted at

janomeによる日本語の形態素解析(名詞の出現頻度)

文学作品や新聞記事など日本語による文章表現から,形態素解析を通して何か特徴を抽出できないかというモチベーションから始めました。

janomeのインストール

まずは,形態素解析器のjanomeのインストールから:

janomeのインストール
$ pip install janome

janomeに関するドキュメントはこちら:

メインプログラムの実行

dataディレクトリ(フォルダ)に作品ごとのテキスト形式のファイル(拡張子は.txt)を用意してから実行します。

image.png

main.pyの実行
$ python main.py

メインプログラムのソースコード

とりあえず,ソースコードは以下のとおりです。

main.py
# coding: UTF-8

from collections import Counter
from itertools import chain
from janome.tokenizer import Tokenizer
import math
import glob

result = []
for file in glob.glob('./data/*.txt'):
	# ファイルからテキストを読み込む
	book = []
	length = 0
	print(file, '-'*16)
	for line in open(file, 'r', encoding="utf-8"):
		print(line, '')
		book.append(line)

	data = []
	each_data = []

	# 形態素解析(janome)
	t = Tokenizer()
	for b in book:
		tokens = t.tokenize(b)
		length += len(b)
		for token in tokens:
			partOfSpeech = token.part_of_speech.split(',')[0]
			print(token)
			if partOfSpeech == "名詞":
				each_data.append(token.surface)
			data.append(each_data)
			each_data = []

	# 名詞の出現頻度(TF)を求める
	chain_data = list(chain.from_iterable(data))
	c = Counter(chain_data)

	# 統計情報の表示
	print("Statistics for ", file, ":")

	print("length = ", length)

	sum = 0
	for cnt in c.values():
		sum += cnt
	p = 0
	for cnt in c.values():
		q = cnt / sum
		p += - q * math.log(q)

	print("entropy = ", p)

	result_rankings = c.most_common(10)
	for d in result_ranking:
		print(d)

	result.append([file, length, p, result_ranking])

# 結果の表示(まとめ)
for r in result:
	print(r)

exit()

メインプログラムの実行結果(一例)

いくつかの文献は,青空文庫のテキストデータを使用させていただきました。

実行結果の一例
['./data\\かいじん二十めんそう.txt', 9229, 4.869249479002453, [('の', 70), ('めん', 58), ('くん', 50), ('ん', 39), ('小', 37), ('二', 32), ('そう', 28), ('十', 28), ('おばけ', 27), ('中', 26)]]
['./data\\こころ.txt', 61906, 5.73922894743357, [('私', 1098), ('の', 465), ('先生', 356), ('事', 238), ('よう', 178), ('奥さ ん', 176), ('ん', 175), ('それ', 165), ('人', 142), ('もの', 142)]]
['./data\\ごんぎつね.txt', 3851, 4.7399830961522875, [('ん', 50), ('十', 33), ('兵', 32), ('中', 17), ('うなぎ', 15), ('の', 10), ('おれ', 10), ('栗', 10), ('家', 9), ('一', 8)]]
['./data\\坊っちゃん.txt', 22895, 6.168599304143815, [('おれ', 131), ('の', 103), ('ん', 90), ('事', 76), ('君', 59), ('もの', 55), ('シャツ', 55), ('山嵐', 54), ('赤', 53), ('人', 52)]]
['./data\\宇宙旅行の科学.txt', 16832, 5.9582839368126175, [('一', 83), ('こと', 81), ('ロケット', 73), ('の', 72), ('衛星', 58), ('マイル', 50), ('人工', 50), ('二', 50), ('地球', 44), ('五', 42)]]
['./data\\斜陽.txt', 73215, 6.35525411401746, [('私', 656), ('の', 418), ('お母さま', 268), ('事', 224), ('よう', 193), ('さん', 165), ('ん', 140), ('僕', 121), ('それ', 113), ('もの', 107)]]
['./data\\日本国憲法.txt', 11862, 5.884146792014421, [('条', 105), ('これ', 79), ('こと', 67), ('法律', 57), ('議員', 49), (' 国会', 46), ('国民', 45), ('内閣', 41), ('2', 40), ('議院', 32)]]
['./data\\日本国憲法(前文).txt', 650, 4.37189439697608, [('国民', 11), ('われ', 7), ('ら', 7), ('こと', 6), ('これ', 4), (' 平和', 4), ('日本', 3), ('憲法', 3), ('もの', 3), ('つて', 3)]]
['./data\\羅生門.txt', 2766, 4.853945084977317, [('下人', 27), ('事', 19), ('老婆', 18), ('の', 17), ('よう', 13), ('それ', 9), ('雨', 8), ('上', 8), ('門', 7), ('死骸', 7)]]
['./data\\茶わんの湯.txt', 3410, 4.813071551666044, [('湯', 30), ('よう', 22), ('茶わん', 21), ('の', 19), ('こと', 17), ('も の', 16), ('それ', 13), ('とき', 13), ('空気', 13), ('これ', 12)]]

展望

文書に出現するN個の名詞の出現確率

に対して,文書のエントロピーを

によって定義すると,文章の読みやすさに関係した特徴量として使えるのではないかと思って試してみました。

名詞だけでなく動詞や形容詞も含めて考えるなど,まだまだ検討の余地はありそうです。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?