-
Notifications
You must be signed in to change notification settings - Fork 0
/
0sTweetAnalysis.py
92 lines (65 loc) · 1.69 KB
/
0sTweetAnalysis.py
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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import tweepy
from wordcloud import WordCloud, STOPWORDS
import collections
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import MeCab
import subprocess
import pandas as pd
import csv
# In[29]:
df = pd.read_csv("tweet_selection.csv")
data = ""
for i in range(len(df.index)):
if df.iat[i,1] == 0:
data = data + df.iat[i,0]
# In[4]:
#テキストの分かち書き
me = MeCab.Tagger ("-Owakati")
me.parseToNode('')
nodes = me.parseToNode(data)
#各変数の初期化
meishi_count = 0
doushi_count = 0
keiyou_count = 0
meishi_list=[]
doushi_list=[]
keiyou_list=[]
# In[24]:
#形態素のうち名詞,動詞,形容詞のみを抽出し,カウント
while nodes:
if nodes.feature.split(",")[0] == "名詞":
meishi_count = meishi_count + 1
meishi_list.append(nodes.surface)
elif nodes.feature.split(",")[0] == "動詞":
doushi_count = doushi_count + 1
doushi_list.append(nodes.surface)
elif nodes.feature.split(",")[0] == "形容詞":
keiyou_count = keiyou_count + 1
keiyou_list.append(nodes.surface)
else:
pass
nodes = nodes.next
text = ""+ " ".join(meishi_list) + " ".join(doushi_list) + " ".join(keiyou_list)
# In[27]:
#WCの下ごしらえ
stop_words = ["https", "co", "てる", "する", "そう", "すぎ", "いい", "さん", "こと"]
fpath = "/Library/Fonts//ヒラギノ丸ゴ ProN W4.ttc"
wc = WordCloud(
font_path = fpath,
background_color = "white",
max_words = 2000,
collocations = False,
stopwords = set(stop_words)
)
# WordCloudの実行
wc.generate(text)
wc.to_file("word_cloud.png")
plt.figure(figsize=(15,12))
plt.imshow(wc)
plt.axis("off")
plt.show()