Created
December 25, 2019 03:20
-
-
Save ayutaz/a5703dd971564be29dc29b06e17cd3ca to your computer and use it in GitHub Desktop.
Which one of the tweets is "しんどい" or "しあわせ"?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
import config | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# OAuth認証 | |
auth = tweepy.OAuthHandler(config.consumer_key,config.consumer_secret) | |
auth.set_access_token(config.access_token,config.access_token_secret) | |
#利用制限にひっかかた時に必要時間待機する | |
api = tweepy.API(auth, wait_on_rate_limit=True) | |
# ツイートの時刻(分のみ)を保存するリストの作成 | |
tweet_data_bad = [] | |
tweet_data_good = [] | |
#グラフの横幅 => ツイートの時間差の最大値 | |
graph_width = 60 | |
# 次のツイートとの差を計算したものを保存する配列,長くても30秒くらいのはず | |
bad_difference = np.zeros(graph_width) | |
good_difference = np.zeros(graph_width) | |
# 検索するワードの指定と検索する数 | |
word_bad = 'しんどい' | |
word_good = 'しあわせ' | |
count = 500 | |
result_type = 'recent' | |
# 時間だけを取得する テストコード | |
# date = '2017/10/19 10:54:29' | |
# new_date, new_time = date.split() | |
# new_date # => '2017/10/19' | |
# print("new_data:" + str(new_time)) | |
for status in api.search(q=word_bad, count=count, result_type=result_type): | |
print("create at:" + str(status.created_at.minute)+"m" + str(status.created_at.second) + "s") | |
# 取得したツイートの時刻をリストの中に入れていく | |
tweet_data_bad.append(status.created_at.second) | |
for status in api.search(q=word_good,count=count, result_type=result_type,locate='ja'): | |
tweet_data_good.append(status.created_at.second) | |
# 新しいツイートから取得していくので,リストの中身を前後反転させる | |
tweet_data_bad.reverse() | |
tweet_data_good.reverse() | |
print("bad tweet:" + str(tweet_data_bad)) | |
print("good tweet:" + str(tweet_data_good)) | |
#ここで,配列の添字のところに,時間差を計算して入れていく | |
# print("bad tweet difference time") | |
for i in range(len(tweet_data_bad) - 1): | |
if (tweet_data_bad[i + 1] < tweet_data_bad[i]): | |
# print("if:" + str((60 - tweet_data_bad[i]) + tweet_data_bad[i + 1])) | |
bad_difference[(60 - tweet_data_bad[i]) + tweet_data_bad[i + 1]] += 1 | |
else: | |
# print("else:" + str(tweet_data_bad[i + 1] - tweet_data_bad[i])) | |
bad_difference[tweet_data_bad[i + 1] - tweet_data_bad[i]] += 1 | |
# print("good tweet difference time") | |
for i in range(len(tweet_data_good) - 1): | |
if (tweet_data_good[i + 1] < tweet_data_good[i]): | |
# print("if:" + str((60 - tweet_data_good[i]) + tweet_data_good[i + 1])) | |
good_difference[(60 - tweet_data_good[i]) + tweet_data_good[i + 1]] += 1 | |
else: | |
# print("else:" + str(tweet_data_good[i + 1] - tweet_data_good[i])) | |
good_difference[tweet_data_good[i + 1] - tweet_data_good[i]] += 1 | |
print("リストの大きさ:" +str(len(tweet_data_bad)) + ":" + str(len(tweet_data_good))) | |
#グラフの棒の横の幅 | |
graph_stick_width = 0.4 | |
# グラフの横軸の配列を生成する | |
xAxis = np.arange(graph_width) | |
#グラフのサイズと解像度の設定 | |
plt.figure(figsize=[20, 11], dpi=60) | |
# 棒グラフを描写 | |
plt.bar(xAxis, bad_difference, label='bad', width=graph_stick_width, align="center") | |
plt.bar(xAxis + 0.4, good_difference, label='good', | |
width=graph_stick_width, align="center") | |
#二つのグラフを少しずらす | |
# plt.set_xticks(x_position + 0.2) | |
# 凡例の表示 | |
plt.legend() | |
#ラベルに名前をつける | |
plt.xlabel('time difference') | |
plt.ylabel('tweet index') | |
plt.show() | |
plt.savefig('figure.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment