File size: 2,530 Bytes
a5d77d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41594bc
a5d77d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import streamlit as st
from janome.tokenizer import Tokenizer
from janome.analyzer import Analyzer
from janome.tokenfilter import *

import matplotlib.pyplot as plt
import japanize_matplotlib
import numpy as np
import pandas as pd
import collections
import itertools
from collections import Counter
import networkx as nx
from wordcloud import WordCloud

text = st.text_area(label='テキストを貼り付けてください', value='')

if len(text) > 0:

  # トークン化
  tokenizer = Tokenizer()
  stop_word_list = []
  noun_list = []

  for token in tokenizer.tokenize(text):
      split_token = token.part_of_speech.split(',')
      
      if (split_token[0] == '名詞') | (split_token[0] == '動詞'):

          if token.surface not in stop_word_list:
              noun_list.append(token.surface)

  # ワードクラウドの作成
  wordcloud = WordCloud(
      background_color='whitesmoke',  # 背景色
      font_path="ipaexg.ttf",  # ダウンロードしたフォントのパス
      width=500,  # 横幅
      height=500,  # 高さ
  )
  noun_space = ' '.join(noun_list)
  wordcloud.generate(noun_space)

  # 共起行列
  pair_list = list(itertools.combinations([n for n in noun_list if len(n) >= 2], 2))
  cnt_pairs = Counter(pair_list)
  tops = sorted(
      cnt_pairs.items(), 
      key=lambda x: x[1], reverse=True
      )[:50]

  # 重み付きデータの生成
  noun_1 = []
  noun_2 = []
  frequency = []

  # データフレームの作成
  for n,f in tops:
      noun_1.append(n[0])    
      noun_2.append(n[1])
      frequency.append(f)

  df_G = pd.DataFrame({'前出名詞': noun_1, '後出名詞': noun_2, '出現頻度': frequency})

  # 重み付きデータの設定
  weighted_edges = np.array(df_G)

  # グラフオブジェクトの生成
  G = nx.Graph()

  # 重み付きデータの読み込み
  G.add_weighted_edges_from(weighted_edges)


  # ワードクラウドとネットワーク図の表示
  # ネットワーク図の描画
  fig = plt.figure(figsize=(12, 20))
  plt.rcParams["font.size"] = 18

  plt.subplot(2, 1, 1)
  plt.title("ネットワーク図")
  nx.draw_networkx(G,
                  node_shape = "s",
                  node_color = "c", 
                  node_size = 200,
                  edge_color = "gray", 
                  font_family = "IPAexGothic"  #"ipaexg00401/ipaexg.ttf" # フォント指定
  );

  plt.subplot(2, 1, 2)
  plt.title("ワードクラウド")
  plt.axis('off')
  plt.imshow(wordcloud)

  plt.tight_layout()
  
  st.pyplot(fig)