毎日テキストマイニング

180日間、毎日テキストマイニングをするブログです

2018/6/30【8日目】データーベースの構造を考える

本日、一日中MySQLを触っていたのですが(ほとんどエラー対応)、せっかくなのでPythonと連携させていきたいと思います。

ひとまず作るデーターベースの設計からはじめてみます。

データーベースの設計

Twitter APIでどんな情報が得られるのか再確認してみます。 取得できる情報のリストは下記の公式情報から。

Twitter APIページ

https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html

分析に使えそうなのが下記のResponseです。試しにprintで出力してみます。

  • created_at
  • text
  • screen_name
  • followers_count
  • friends_count
  • statuses_count
  • listed_count
  • favourites_count
  • listed_count
  • retweet_count
  • favorite_count

python for i in range(1): if res.status_code == 200: for tweet in timeline: print(tweet["created_at"]) print(tweet["text"]) print(tweet["user"]["screen_name"]) print(tweet["user"]["followers_count"]) print(tweet["user"]["friends_count"]) print(tweet["user"]["statuses_count"]) print(tweet["user"]["listed_count"]) print(tweet["user"]["favourites_count"]) print(tweet["user"]["listed_count"]) print(tweet["retweet_count"]) print(tweet["favorite_count"])

実行結果。

Fri Jun 29 08:35:14 +0000 2018
梅雨明けだーーー🐸!!!!!
暑い実に暑い、、🔥🌞
暑さにみなさんが負けませんように...
더우니까 열심히 😊👍✨! https://t.co/3PY5LyXNG3
48_asainanami
9845
91
89
260
77
260
195
879

うーん、数字が並んでいるだけなのでわかりずらいですね。print文にちょっと情報を加えてみます。

for i in range(1):
    if res.status_code == 200:
        for tweet in timeline:
            print("日付:" + tweet["created_at"])
            print(“ツィート:” + tweet["text"])
            print("名前:" + tweet["user"]["screen_name"])
            print("フォロワー数:" + tweet["user"]["followers_count"])
            print(“フォロー数:" + tweet["user"]["friends_count"])
            print(“ツィートした数" + tweet["user"]["statuses_count"])
            print("ユーザーのリスト数" + tweet["user"]["listed_count"])
            print("ユーザーのファボライトした数" + tweet["user"]["favourites_count"])
            print("リツイート数" + tweet["retweet_count"])
            print("ファボライト数" + tweet["favorite_count"])
TypeError: must be str, not int

エラーが起こりました。そうでした。数値型と文字列型は結合できないんですね。 str()で囲んであげると文字列型に変換されるそうです。

for i in range(1):
    if res.status_code == 200:
        for tweet in timeline:
            print("日付:" + str(tweet["created_at"]))
            print("ツィート:" + tweet["text"])
            print("名前:" + tweet["user"]["screen_name"])
            print("ツィート数:" + str(tweet["user"]["statuses_count"]))
            print("フォロー数:" + str(tweet["user"]["friends_count"]))
            print("フォロワー数:" + str(tweet["user"]["followers_count"]))
            print("ユーザーのリスト数:" + str(tweet["user"]["listed_count"]))
            print("ユーザーのファボライト数:" + str(tweet["user"]["favourites_count"]))
            print("リツイート数:" + str(tweet["retweet_count"]))
            print("ファボライト数:" + str(tweet["favorite_count"]))

実行結果

日付:Fri Jun 29 08:35:14 +0000 2018
ツィート:梅雨明けだーーー🐸!!!!!
暑い実に暑い、、🔥🌞
暑さにみなさんが負けませんように...
더우니까 열심히 😊👍✨! https://t.co/3PY5LyXNG3
名前:48_asainanami
ツィート数:89
フォロー数:91
フォロワー数:9845
ユーザーのリスト数:260
ユーザーのファボライト数:77
リツイート数:195
ファボライト数:880

わかりやすくなりました。 リスト数というのが何を示しているのか謎ですが、、、まぁ、これもいちおう保存しておきます。 とりあえず保存する価値がありそうなのはこれくらいかと。 画像があるかどうか、コメント数がどのくらいかは教えてくれないんですね。

テーブルの設計

テーブルとしては下記になるのかと思います。

Table名 : メンバーの名前 id : int name : char(50) created_data ; daytime tweet : text count_tweets : int count_follows : int count_followers : int count_favolites : int count_lists users_favorites : in retweet : int favorite : int

早速明日から実装していきたいとおもいます。

今日の結果

今日呟かれた件数は41件でした。 形容詞 '暑い': 4, 'ない': 4, '楽しい': 4, '嬉しい': 3, '懐かしい': 2, '面白い': 2, 'たのしい': 2, '遅い': 1, 'やばい': 1, '賢い': 1, 'うれしい': 1, '可愛い': 1, 'すごい': 1, '新しい': 1, '悔しい': 1, '優しい': 1, '心強い': 1, 'いい': 1, 'ものすごい': 1, 'やすい': 1, 'ほしい': 1, '人懐っこい': 1

名詞 'さん': 11, '時': 8, '今日': 7, '祭': 7, '写真': 6, '笑': 6, 'ちゃん': 6, '一緒': 5, '生誕': 5, '暑い': 4, 'ない': 4, '楽しい': 4, '私': 4, '生': 4, 'こと': 4, '人': 4, 'ミラクル': 4, '嬉しい': 3, 'さ': 3, 'ん': 3, '時間': 3, '撮影': 3, '会': 3, 'たち': 3, '子': 3, '分': 3, 'の': 3, '皆さま': 3, '公演': 3, 'あと': 3, '手紙': 3, '日': 3, '衣装': 3, 'チーム': 3, 'せい': 3, '感謝': 3, '参加': 3,

動詞 'する': 25, 'さん': 11, '見る': 9, '時': 8, 'なる': 8, '今日': 7, '祭': 7, 'くる': 7, 'くださる': 7, '写真': 6, '笑': 6, 'ちゃん': 6, 'いる': 6, '一緒': 5, '生誕': 5, 'くれる': 5, 'せる': 5, '暑い': 4, 'ない': 4, '楽しい': 4, '私': 4, '生': 4, 'こと': 4, '人': 4, 'ミラクル': 4, 'もえる': 4, '嬉しい': 3, 'さ': 3, 'ん': 3, '時間': 3, '撮影': 3, '会': 3, 'たち': 3, '子': 3, '分': 3, 'の': 3, '皆さま': 3, '公演': 3, 'あと': 3, '手紙': 3, '日': 3, '衣装': 3, 'チーム': 3, 'せい': 3, '感謝': 3, '参加': 3, '来る': 3, '行く': 3, '思う': 3, 'てる': 3, '呼ぶ': 3,

今日学んだこと

データベースの基礎的な知識を一通り。 MySQL Workbenchの基礎的な使い方。 int型からstr型への変換と結合の仕方。