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
136
137
138
139
140
141
142
143
144
145
require 'rubygems'
require 'tweetstream'
require 'thread'

class TwitStream

  # Log in details.
  USER = "xxx"
  PASS = "xxx"

  attr_accessor :posts

  def initialize
    @posts = []
    @semaphore = Mutex.new
  end

  # Connects a stream to the twitter service.
  def stream
    TweetStream::Client.new(USER, PASS).sample do |status, client|

      # Each new posts we lock the posts so that we can update it.
      @semaphore.synchronize do
        @posts << status.text
      end
    end
  end


  # Returns the first item from the list
  def get_next_item

    next_item = nil

    # Each new request for information we lock the posts so we can pop from it.
    @semaphore.synchronize do
      if(@posts.length > 0)
        #puts @posts.inspect
        next_item = @posts.pop
      end
    end

    next_item
  end

end


class WordList

  # Word Sets
  FEELINGS      = ["happy", "sad", "angry", "joy", "upset", "love"]
  FOUR_LETTERS  = ["fuck", "cunt", "shit", "crap", "prick", "dick"]
  TXT_TERMS     = ["lol", "lmao", "rtfm", "rotfl", "cya", "afaik", "imo", "imho", "nsfw", "wtg", "ftw", "btw", "2moro", "brb", "sh", "tyvm", "pov", "np"]
  SIMILES       = [":)", "(:", ":(", "):", ":|", ":O", ";)", ":*", ":&", ":8", ":x"]
  SCOT_VS_ENG   = ["scotland", "england"]
  LOVE_VS_HATE  = ["love", "hate"]
  WORK_VS_PLAY  = ["work", "play"]
  SHORT_URL     = ["bit.ly", "tinyurl.com", "is.gd", "goo.gl", "tiny.cc", "is.gd", "ity.im", "ow.ly", "kxk.me", "cli.gs", "budurl.com", "zi.ma", "adjix.com", "tr.im", "snipurl.com", "poprl.com"]
  GAMES         = ["brink", "battlefield", "portal", "crysis"]
  PHONES        = ["iPhone", "Android", "iOS", "HTC"]


  attr_accessor :list, :source, :current_list

  def initialize(list = FEELINGS)
    @list = {}
    @source = TwitStream.new

    # Start the source
    Thread.new do
      @source.stream
    end

    @semaphore = Mutex.new

    @current_list = list
    setup(@current_list)
  end

  # setsup the list
  def setup(list)
    list.each do |w|
      @list[w.downcase] = 0
    end
  end

  def process
    while true
      words = @source.get_next_item

      unless words
        sleep(1)
      else
        words = words.gsub(/[,"'!?]/, " ") # strip some stuff that could lead to skewed results.

        @semaphore.synchronize do
          @current_list.each do |w|
            if words.include?(w.downcase)
              @list[w] += 1
            end
          end

        end
      end

    end
  end

end


#TwitStream.new.stream
word_list = WordList.new(WordList::SIMILES)

threads = []

# start 3 threads that just do processing
3.times do |t|
  Thread.new { word_list.process }
end

# game loop
while true

  if word_list.source.posts.length > 500
    break
  else
    word_list.list.sort {|a, b| a[1] <=> b[1]}.reverse.each do |i|
      print  "#{i[0]} = #{i[1]}   " if i[1] > 0
    end
    puts "\n - "
    puts "Pool Size: #{word_list.source.posts.length}"

    sleep(10)
  end

end

# Kill all the processing threads (the stream therad is still running)
threads.each { |t|  t.join }

puts "ENDED!!!"
puts word_list.list.inspect
puts word_list.source.posts.length