Last active
August 8, 2018 14:58
-
-
Save seven0525/717e35fe44b4dbce30d221190b37db51 to your computer and use it in GitHub Desktop.
落合陽一っぽいツイートを自動生成してツイートさせてみた ref: https://qiita.com/ahpjop/items/9f532a72ac4666b9083a
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 twitter | |
auth = twitter.OAuth(consumer_key="", | |
consumer_secret="", | |
token="", | |
token_secret="") | |
t = twitter.Twitter(auth=auth) | |
#テキストのみツイート | |
status = s #投稿するツイート | |
t.statuses.update(status=status) #Twitterに投稿 |
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
from janome.tokenizer import Tokenizer | |
import json | |
# テキストファイルを読み込む | |
sjis = open('new_ochyai.txt', 'rb').read() | |
text = sjis.decode('utf_8') | |
# テキストを形態素解析読み込みます | |
t = Tokenizer() | |
words = t.tokenize(text) | |
# 辞書を生成 | |
def make_dic(words): | |
tmp = ["@"] | |
dic = {} | |
for i in words: | |
word = i.surface | |
if word == "" or word == "\r\n" or word == "\n": continue | |
tmp.append(word) | |
if len(tmp) < 3: continue | |
if len(tmp) > 3: tmp = tmp[1:] | |
set_word3(dic, tmp) | |
if word == "。": | |
tmp = ["@"] | |
continue | |
return dic | |
# 三要素のリストを辞書として登録 | |
def set_word3(dic, s3): | |
w1, w2, w3 = s3 | |
if not w1 in dic: dic[w1] = {} | |
if not w2 in dic[w1]: dic[w1][w2] = {} | |
if not w3 in dic[w1][w2]: dic[w1][w2][w3] = 0 | |
dic[w1][w2][w3] += 1 | |
dic = make_dic(words) | |
json.dump(dic, open("markov-blog.json", "w", encoding="utf-8")) |
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
text = open("ochyai.txt","r").read().split() | |
import string | |
for line in text: | |
for word in line: | |
if word in string.ascii_letters or word in string.digits: | |
if line in text: | |
text.remove(line) | |
f = open('new_ochyai.txt', 'w') | |
for x in text: | |
f.write(str(x) + "\n") | |
f.close() | |
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
# -*- coding: utf-8 -*- | |
#辞書の読み込み | |
import json | |
dic = open("markov-blog.json" , "r")#crontabを使う場合は、絶対パスに書き換える | |
dic = json.load(dic) | |
#文章を生成 | |
tweets_list = [] | |
import random | |
def word_choice(sel): | |
keys = sel.keys() | |
ran = random.choice(list(keys)) | |
return ran | |
def make_sentence(dic): | |
ret = [] | |
if not "@" in dic: return "no dic" | |
top = dic["@"] | |
w1 = word_choice(top) | |
w2 = word_choice(top[w1]) | |
ret.append(w1) | |
ret.append(w2) | |
while True: | |
w3 = word_choice(dic[w1][w2]) | |
ret.append(w3) | |
if w3 == ".": break | |
w1, w2 = w2, w3 | |
tweets_list.append(ret) | |
return "".join(ret) | |
for i in range(1): | |
s = make_sentence(dic) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment