TPTブログ

テックポート株式会社のブログです。 技術情報や製品・サービス情報、 また未経験社員がデータサイエンティストを 目指す奮闘記など、更新していきます。

▲心くじけず言語処理100本ノック==35~39==

f:id:TBT_matsu:20200515145048p:plain
こんにちは!
テービーテックの村松です。

「言語処理100本ノック2020]」
nlp100.github.io
に挑戦中!
途中でくじけないか見守ってください・・・。
そして、皆さんも一緒に挑戦してみましょう!

本日は第4章:形態素解析 35~39です!
間違い・コード改善点などありましたら教えていただけると嬉しいです。

4章:形態素解析

「夏目漱石の小説『吾輩は猫である』の文章をMeCabを使って形態素解析し,その結果をneko.txt.mecabというファイルに保存せよ.このファイルを用いて,以下の問に対応するプログラムを実装せよ. なお,問題37, 38, 39はmatplotlibもしくはGnuplotを用いるとよい.」
ここからはneko.txtを使用して問題に取り組んでいきます。

使用データ

ds-blog.tbtech.co.jp
↑↑こちらの30で作成した「dicts」を使用します。

35.単語の出現頻度

「文章中に出現する単語とその出現頻度を求め,出現頻度の高い順に並べよ.」

words = []
for dict_ in dicts:
  for n in range(len(dict_)):
    #「base」の要素を取り出してリストに格納します。
    words.append(dict_[n]['base'])

#collections.Counter()でリスト内の単語の出現頻度を出力します。
from collections import Counter
frequency = Counter(words) 
print(frequency)
##結果
Counter({'の': 9194, '。': 7486, 'て': 6848, '、': 6772, 'は': 6420, 'に': 6243, 'を': 6071, 'だ': 5975, 'と': 5508, 'が': 5337, '・・・以下略
36.頻度上位10語

「出現頻度が高い10語とその出現頻度をグラフ(例えば棒グラフなど)で表示せよ.」

#出現頻度が高い10語とその出現頻度の表示。most_common()
#35で作成した「frequency」を使用します。
frequency.most_common(10)
#上位10単語とその頻度をそれぞれリストに格納します。
f_lists = list(zip(*frequency.most_common(10)))

#matplotlibでグラフの表示します。
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font='IPAGothic')

labels = f_lists[0]
height = f_lists[1]
plt.bar(labels, height)
plt.show();

f:id:TBT_matsu:20200601111610p:plain
なお、グラフを表示する際に日本語が文字化けしてしまったのでこちらを参考に表示できるようにしました。

37.「猫」と共起頻度の高い上位10語

「「猫」とよく共起する(共起頻度が高い)10語とその出現頻度をグラフ(例えば棒グラフなど)で表示せよ.」

#「猫」を含む文を抽出します。
not_nekos = []
neko_lists = []
for dict_ in dicts:
  #「猫」を含む文を抽出します。
  if any(d['base'] == '猫' for d in dict_):
    neko_lists.append(dict_)
    #その中に出てくる「猫」以外の単語を抽出します。
    #※問題文に指定はありませんが今回は名詞に限定します。また、「*」も除外とします。
    not_neko = [g['base'] for g in dict_ if g['base'] != '猫' and g['pos'] == '名詞' and g['base'] != '*']
    not_nekos.extend(not_neko)

#35と同様にcollections.Counter()でリスト内の単語の出現頻度を出力します。
from collections import Counter
f_not_nekos = Counter(not_nekos) 
f = list(zip(*f_not_nekos.most_common(10)))

#matplotlibでグラフの表示します。
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font='IPAGothic')

labels = f[0]
height = f[1]
plt.bar(labels, height)
plt.show();

f:id:TBT_matsu:20200601111751p:plain

38.ヒストグラムPermalink

「単語の出現頻度のヒストグラム(横軸に出現頻度,縦軸に出現頻度をとる単語の種類数を棒グラフで表したもの)を描け.」

#「base」の単語を抽出します。
words = []
for dict_ in dicts:
  for d in dict_:
    words.append(d['base'])

#35と同様にcollections.Counter()でリスト内の単語の出現頻度を出力します。
from collections import Counter
f_words = Counter(words)

#matplotlibでヒストグラムの表示します。
import matplotlib.pyplot as plt

plt.hist(f_words.values(), bins=10, range=(1,10))
plt.show();

f:id:TBT_matsu:20200601112027p:plain

39.Zipfの法則

「単語の出現頻度順位を横軸,その出現頻度を縦軸として,両対数グラフをプロットせよ.」

#これまでと同様の流れでmost_common()で出現回数順に要素を取得します。
words = []
for dict_ in dicts:
  for d in dict_:
    words.append(d['base'])

#35と同様にcollections.Counter()でリスト内の単語の出現頻度を出力し、
#most_common()で出現回数順に要素を取得します。
from collections import Counter
f_most_common = [f[1] for f in Counter(words).most_common()]

#両対数グラフを表示します。
import matplotlib.pyplot as plt
import numpy as np

plt.scatter(np.log(range(1, len(f_most_common)+1)), np.log(f_most_common))
plt.show();

f:id:TBT_matsu:20200601112246p:plain



ここまでご覧いただきありがとうございます。
以上、第4章35~39でした!