Here is a sketch that was based on a prototype by Daniel Schiffman to illustrate the use of the processing HashInt class, I can't see the HashInt class catching on really, particularly not for rubyists where alternative options abound. Anyway I liked the sketch and I think sometimes less is more, in later versions Schiffman sorts the words according to their frequency (to show off IntClass functionality), however to my mind it spoils the sketch!
class Word
attr_accessor :word
attr_reader :count
def initialize word
@word = word
@count = 1
end
def increment
@count += 1
end
end
attr_reader :concordance, :lines, :tokens, :counter
def setup
size 640, 360
@counter = 0
@concordance = {}
@tokens = File.read(data_path("dracula.txt")).scan(/[\w'-]+/)
text_font(create_font("Georgia", 24))
end
def draw
background 51
fill 255
s = (tokens[counter] == "I")? tokens[counter] : tokens[counter].downcase
@counter = (counter + 1) % tokens.length
if (concordance.has_key?(s))
w = concordance[s]
w.increment
else
concordance[s] = Word.new(s)
end
x = 0
y = height - 10
concordance.values.each do |w|
if (w.count > 3)
fsize = constrain(w.count, 0, 100)
text_size(fsize)
text(w.word, x, y)
x += text_width(w.word) + 1
end
if (y == 0)
no_loop
else
if (x > width)
x = 0
y = (y < 0)? 0 : y - 100
end
end
end
end
No comments:
Post a Comment