Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # -*- coding: utf-8 -*-
- require 'groonga'
- require 'pit'
- require 'time'
- require 'tweetstream'
- class Database
- def initialize
- @database = nil
- end
- attr_reader :database
- def open(path, encoding)
- Groonga::Context.default_options = {:encoding => encoding}
- if File.exist?(path) then
- @database = Groonga::Database.open(path)
- else
- @database = Groonga::Database.create(:path => path)
- create_table
- end
- end
- def close
- @database.close
- @database = nil
- end
- def closed?
- @database.nil? or @database.closed?
- end
- def create_table
- Groonga::Schema.create_table('Tweets', :type => :hash) do |table|
- table.text :screen_name
- table.long_text :text
- table.time :created_at
- end
- Groonga::Schema.create_table('Terms',
- :type => :patricia_trie,
- :key_normalize => true,
- :default_tokenizer => 'TokenBigram') do |table|
- table.index('Tweets.text')
- end
- end
- def search(word)
- results = []
- Groonga['Tweets'].select do |column|
- column['text'] =~ word
- end.each do |record|
- results << record.key
- end
- return results
- end
- end
- if __FILE__ == $0
- pit = Pit.get('twitter.com', :require => {
- 'username' => 'your twitter username',
- 'password' => 'your twitter password'
- })
- TweetStream.configure do |config|
- config.username = pit['username']
- config.password = pit['password']
- config.auth_method = :basic
- end
- db = Database.new
- db.open('./db/tweet.db', 'utf8')
- TweetStream::Client.new.sample do |tweet|
- next unless /\p{hiragana}|\p{katakana}/ =~ tweet.text
- Groonga['Tweets'].add(tweet.id_str,
- :screen_name => tweet.user.screen_name,
- :text => tweet.text,
- :created_at => Time.parse(tweet.created_at))
- puts "#{tweet.created_at} #{tweet.user.screen_name}: #{tweet.text}"
- end
- db.close
- end
Add Comment
Please, Sign In to add comment