Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Last active December 17, 2015 16:19
Show Gist options
  • Save rummelonp/5637806 to your computer and use it in GitHub Desktop.
Save rummelonp/5637806 to your computer and use it in GitHub Desktop.
マルコフ連鎖でツイートするやつ
# -*- coding: utf-8 -*-
source 'https://rubygems.org'
gem 'twitter'
gem 'natto'
# -*- coding: utf-8 -*-
require 'natto'
require 'twitter'
require 'uri'
MARKER_BEGIN = '__BEGIN__'
MARKER_END = '__END__'
Twitter.configure do |config|
config.consumer_key = '<!!! consumer key !!!>'
config.consumer_secret = '<!!! consumer secret !!!>'
config.oauth_token = '<!!! oauth token !!!>'
config.oauth_token_secret = '<!!! oauth token secret !!!>'
end
tagger = Natto::MeCab.new('-O wakati')
client = Twitter.client
statuses = client.user_timeline '<!!! scree_name !!!>', :count => 200
original_texts = []
nodes = []
statuses.each do |status|
text = status.text
# url が含まれてたらスキップ
next if text.match(URI.regexp)
# screen_name を除外
text = text.gsub(/@[\w_]+/, '')
# RT / QR を除外
text = text.gsub(/(RT|QT):? @[\w_]+.*(\n|$)/, '')
# ハッシュタグを除外
text = text.gsub(/#[\wA-Za-z0-9ぁ-ヶ亜-黑]+/, '')
# 前後の空白削除
text = text.strip
original_texts << text
words = tagger.parse(text).split(' ')
nodes.concat([MARKER_BEGIN, *words, MARKER_END].each_cons(3).to_a)
end
# 原文そのままでないやつが出来るまで繰り返す
text = nil
loop do
# マルコフ連鎖
node = nodes.select { |n| n[0] == MARKER_BEGIN }.sample
text = node.join
loop do
node = nodes.select { |n| n[0] == node[1] && n[1] == node[2] }.sample
break unless node
text += node[2]
break if node[2] == MARKER_END
end
text = text.gsub(MARKER_BEGIN, '').gsub(MARKER_END, '')
break unless original_texts.include?(text)
end
client.update text
puts text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment