source code †
require 'rubygems'
require 'dbi'
require 'mecab_util.rb'
# ---------------------------------------
# --- define
# ---------------------------------------
DBNAME = 'YOURDB'
DBLOGIN = 'LOGIN'
DBPASS = 'PASS'
VERB = false
# ---------------------------------------
# --- subroutine
# ---------------------------------------
def add_trend_noun(db, noun)
id1 = 0
sql = 'SELECT id FROM trend_word WHERE word=?'
sth = db.execute(sql, noun)
sth.each do |row|
id1 = row[0].to_i
end
sth.finish
if id1 == 0 then
sql = 'INSERT INTO trend_word VALUES (NULL,?,?)'
db.do(sql, noun, Time.now.to_i)
else
sql = 'UPDATE trend_word SET lastupdate=? WHERE id=?'
db.do(sql, Time.now.to_i, id1)
end
end
def process_t_cloud(db, tweet)
line = []
tweet.each do |chunk|
line << chunk
end
spot = line[0].split
trendword = spot[0]
if trendword =~ /^[^@#]/ then
puts trendword if VERB
add_trend_noun(db, trendword)
end
end
def process_buzztter(db, tweet)
chunk = tweet.split
chunk.each do |line|
line.gsub!(/,$/, '')
next if line =~ /^http/
next if line =~ /^HOT:/
next if line =~ /^[@#\[]/
token = MecabParse.new(line)
nounlist = token.get_noun
nounlist.each do |yomi|
noun = yomi.split
trendword = noun[0]
next if trendword =~ /^[@#\[]/
next if trendword =~ /:/
puts trendword if VERB
add_trend_noun(db, trendword)
end
end
end
# ---------------------------------------
# --- main routine
# ---------------------------------------
# --- open DB
db = DBI.connect(DBNAME, DBLOGIN, DBPASS)
db.do('SET NAMES utf8')
# --- tweet query
sql = "SELECT id,text FROM tweet_log WHERE process=1 AND screen_name='t_cloud'"
item_num = 0
begin
puts "open t_cloud log" if VERB
sth = db.execute(sql)
puts "start" if VERB
sth.each do |row|
process_t_cloud(db, row[1])
donesql = 'UPDATE tweet_log SET process=2 WHERE id=?'
db.do(donesql, row[0].to_i)
end
sth.finish
end
sql = "SELECT id,text FROM tweet_log WHERE process=1 AND screen_name='buzztter'"
item_num = 0
begin
puts "open buzztter log" if VERB
sth = db.execute(sql)
puts "start" if VERB
sth.each do |row|
process_buzztter(db, row[1])
donesql = 'UPDATE tweet_log SET process=2 WHERE id=?'
db.do(donesql, row[0].to_i)
end
sth.finish
end