Skip to content

Instantly share code, notes, and snippets.

@sasamijp
Last active August 29, 2015 13:57
Show Gist options
  • Save sasamijp/9889373 to your computer and use it in GitHub Desktop.
Save sasamijp/9889373 to your computer and use it in GitHub Desktop.
しりとりしましょ
# -*- encoding: utf-8 -*-
require 'natto'
require 'twitter'
require 'tweetstream'
require './key.rb'
@rest = Twitter::REST::Client.new do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.access_token = Const::ACCESS_TOKEN
config.access_token_secret = Const::ACCESS_TOKEN_SECRET
end
TweetStream.configure do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.oauth_token = Const::ACCESS_TOKEN
config.oauth_token_secret = Const::ACCESS_TOKEN_SECRET
config.auth_method = :oauth
end
client = TweetStream::Client.new
natto = Natto::MeCab.new
class SiritoriAI
def initialize(str, natto)
@natto = natto
@nouns = []
searchforTweets(str).each do |vocabulary|
@natto.parse(vocabulary) do |n|
@nouns.push(n.surface) if n.feature.split(",")[0] == '名詞'
end
end
csvtoVocabulary()
end
def response(str)
@nouns.each do |noun|
if noun.start_with? str[-1]
return noun
end
end
return "負けました"+"!"*rand(25)
end
def writecsv(str)
File.open("./data/tweets.csv", "a") do |file|
file.write("#{str}\n")
end
end
def csvtoVocabulary
File.read("./data/tweets.csv", :encoding => Encoding::UTF_8).split("\n").each do |str|
@natto.parse(str) do |n|
@nouns.push(n.surface) if n.feature.split(",")[0] == '名詞'
end
end
end
private
def searchforTweets(str)
@rest = Twitter::REST::Client.new do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.access_token = Const::ACCESS_TOKEN
config.access_token_secret = Const::ACCESS_TOKEN_SECRET
end
result = []
@rest.search(str, :result_type => "latest").take(150).each do |status|
next if status.text.include?("RT") or status.text.include?("http") or status.text.include?(".co") or status.text.include?("@")
result.push(status.text)
end
return result
end
end
ai = SiritoriAI.new("あ", natto)
client.userstream do |status|
puts status.text
study = Thread.new do
unless status.text.include?("RT") or status.text.include?("http") or status.text.include?(".co") or status.text.include?("@")
ai.writecsv(status.text)
end
end
if status.text.include?('@sa2mi') and !status.text.start_with?("RT")
study2 = Thread.new do
ai.csvtoVocabulary()
end
tweeting = Thread.new do
tweet = "@#{status.user.screen_name} #{ai.response(status.text.gsub('@sa2mi ',''))}"
option = {"in_reply_to_status_id"=>status.id.to_s}
@rest.update tweet,option
end
[study, study2, tweeting].each do |thread|
thread.join
end
else
study.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment