/
buzzword-2021.py
144 lines (105 loc) · 3.52 KB
/
buzzword-2021.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import numpy as np
import MeCab
from wordcloud import WordCloud
import re
import json
import time
import collections
from dotenv import load_dotenv
load_dotenv('.env')
# SourceHanSans
FONT_PATH = os.environ.get("FONT_PATH")
TWEETS_DIR = "tweets/"
TWEETS_JSON_PATH = TWEETS_DIR + "tweets.json"
USER_IDS_PATH = "user_ids.json"
WORDCLOUD_TITLE = "misw-buzzword"
def generate_wordcloud(text):
wc = WordCloud(background_color="white", font_path=FONT_PATH, max_words=1000, max_font_size=300, width=800, height=600)
wc.generate(text)
wc.to_file("{}.png".format(WORDCLOUD_TITLE))
def list_to_string(l):
return ' '.join(l)
def get_words_from_text(text):
mecab = MeCab.Tagger("-d D:\Documents\MeCabDic")
text_dst = mecab.parse(text)
lines = text_dst.split('\n')
lines = lines[0:-2]
words = []
for line in lines:
# tabかカンマでsplitする
col = re.split('\t|,', line)
if col[1] in ["形容詞", "動詞","名詞", "副詞"]:
words.append(col[0])
return words
# 本質的でない単語を除く
def remove_words(list):
for s in list:
if s.startswith("https://") or s.startswith("http://"):
list.remove(s)
if s == "前日比":
list.remove(s)
elif s == "https":
list.remove(s)
elif s == "t":
list.remove(s)
elif s == "co":
list.remove(s)
elif s == "t":
list.remove(s)
elif s == "ー":
list.remove(s)
elif s == "てる":
list.remove(s)
elif s == "自分":
list.remove(s)
elif s == "て":
list.remove(s)
elif s == "の":
list.remove(s)
elif s == "それ":
list.remove(s)
elif s == "気":
list.remove(s)
elif s == "ん":
list.remove(s)
def remove_words_by_counter(list, counter):
max_val = 330
for s in list:
if counter[s] > max_val:
list.remove(s)
def print_counter(counter):
threshold = 80
for x in counter:
if counter[x] > threshold:
print("{}: {}".format(x, counter[x]))
def main():
user_ids_json = open(USER_IDS_PATH, 'r', encoding="utf-8")
user_ids = json.load(user_ids_json)
user_ids_json.close()
tweets_json = open(TWEETS_JSON_PATH, 'r', encoding="utf-8")
tweets = json.load(tweets_json)
tweets_json.close()
all_tweets = tweets["tweets"]
words_to_wc = []
start_time = time.perf_counter()
for user in user_ids["ids"]:
if user["name"] in all_tweets:
print("Loading {}'s tweets ... ({})".format(user["name"], len(all_tweets[user["name"]])))
for tweet in all_tweets[user["name"]]:
words_to_wc.extend(get_words_from_text(tweet["tweet"]))
# time_1 = time.perf_counter()
print("Removing unrelated words ...")
remove_words(words_to_wc)
counter = collections.Counter(words_to_wc)
remove_words_by_counter(words_to_wc, counter)
# time_2 = time.perf_counter()
print("Words to string ...")
words_str = list_to_string(words_to_wc)
# time_3 = time.perf_counter()
print("Generating wordcoud ...")
generate_wordcloud(words_str)
time_end = time.perf_counter()
print("Done! ({} [sec])".format(time_end - start_time))
if __name__ == '__main__':
main()