-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
135 lines (119 loc) · 3.37 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- encoding: utf-8 -*-
require 'tweetstream'
require 'twitter'
require 'bimyou_segmenter'
require 'natto'
require './key.rb'
Twitter.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
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
client.userstream do |status|
if status.text.include?("@sa2mi") && !status.text.include?("RT") then
nouns=[]
input = status.text.sub("@sa2mi ","")
nm = Natto::MeCab.new
nm.parse(input) do |n|
type = n.feature.split(",")[0]
if type == "名詞"
nouns.push(n.surface)
end
end
c = []
Twitter.search("#{nouns[0]}", :count => 50, :result_type => "latest").results.map do |status|
next if status.text.include?("RT") or status.text.include?("http") or status.text.include?(".co") or status.text.include?("@")
c.push("#{status.text.gsub("\n","")}")
end
print c
dictionary=[]
c.each do |cc|
text=BimyouSegmenter.segment(cc)
p text
p text.length
text.push("endpoint")
p text
p text.length
if text.length == 5
deathflag = true
end
if !deathflag
n=0
counter = 0
inputs = []
while true do
breakflag = false
if n == 0 then
inputs[counter] = "beginpoint #{text[n]} #{text[n+1]} #{text[n+2]} #{text[n+3]}"
n+=2
elsif n+2>=text.length+1 then
breakflag = true
else
inputs[counter] = "#{text[n]} #{text[n+1]} #{text[n+2]} #{text[n+3]} #{text[n+4]}"
n+=1
end
break if breakflag
counter+=1
end
p inputs
inc = inputs.include?("endpoint")
inputs.each do|input|
if !inc
dictionary.push("#{input}")
else
dictionary.push("#{input} endpoint")
end
end
end
end
begins=[]
text = ''
isStatement = true
dictionary.each do |word| #最初の一文節の候補を選ぶ
sword = word.split
begins.push(word) if sword[0] == "beginpoint"
end
rnum = rand(begins.length)
first = begins[rnum].sub('beginpoint ','') #最初の一文節を候補の中から乱数で決定
text << first #なんかこう書くと速いらしい
isStatement = false if text.reverse[0..7] == "endpoint".reverse
p text
puts isStatement
if isStatement
puts isStatement
candidate = []
7.times do
dictionary.each do |word|
stext=text.split
endword = stext[stext.length-1]
candidate.push(word.sub(endword,'')) if word.split[0] == endword
end
p "2nd"
p candidate
break if candidate.length == 0
rnum = rand(candidate.length)
addword = candidate[rnum]
stext = text.split
p "3rd"
text << addword
break if text.split[stext.length-1] == "endpoint"
end
end
text.gsub!(" endpoint","")
text.gsub!(" ","")
text.gsub!("@","")
puts text
text = "@#{status.user.screen_name} #{text}"
option = {"in_reply_to_status_id"=>status.id.to_s}
Twitter.update text,option
end
end