Guest User

Untitled

a guest
Sep 1st, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # -*- coding: utf-8 -*-
  3.  
  4. require 'groonga'
  5. require 'pit'
  6. require 'time'
  7. require 'tweetstream'
  8.  
  9. class Database
  10. def initialize
  11. @database = nil
  12. end
  13.  
  14. attr_reader :database
  15.  
  16. def open(path, encoding)
  17. Groonga::Context.default_options = {:encoding => encoding}
  18. if File.exist?(path) then
  19. @database = Groonga::Database.open(path)
  20. else
  21. @database = Groonga::Database.create(:path => path)
  22. create_table
  23. end
  24. end
  25.  
  26. def close
  27. @database.close
  28. @database = nil
  29. end
  30.  
  31. def closed?
  32. @database.nil? or @database.closed?
  33. end
  34.  
  35. def create_table
  36. Groonga::Schema.create_table('Tweets', :type => :hash) do |table|
  37. table.text :screen_name
  38. table.long_text :text
  39. table.time :created_at
  40. end
  41.  
  42. Groonga::Schema.create_table('Terms',
  43. :type => :patricia_trie,
  44. :key_normalize => true,
  45. :default_tokenizer => 'TokenBigram') do |table|
  46. table.index('Tweets.text')
  47. end
  48. end
  49.  
  50. def search(word)
  51. results = []
  52. Groonga['Tweets'].select do |column|
  53. column['text'] =~ word
  54. end.each do |record|
  55. results << record.key
  56. end
  57. return results
  58. end
  59. end
  60.  
  61. if __FILE__ == $0
  62. pit = Pit.get('twitter.com', :require => {
  63. 'username' => 'your twitter username',
  64. 'password' => 'your twitter password'
  65. })
  66.  
  67. TweetStream.configure do |config|
  68. config.username = pit['username']
  69. config.password = pit['password']
  70. config.auth_method = :basic
  71. end
  72.  
  73. db = Database.new
  74. db.open('./db/tweet.db', 'utf8')
  75.  
  76. TweetStream::Client.new.sample do |tweet|
  77. next unless /\p{hiragana}|\p{katakana}/ =~ tweet.text
  78.  
  79. Groonga['Tweets'].add(tweet.id_str,
  80. :screen_name => tweet.user.screen_name,
  81. :text => tweet.text,
  82. :created_at => Time.parse(tweet.created_at))
  83.  
  84. puts "#{tweet.created_at} #{tweet.user.screen_name}: #{tweet.text}"
  85. end
  86.  
  87. db.close
  88. end
Add Comment
Please, Sign In to add comment