Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Saturday 16 March 2013

Another word counting sketch translated to ruby processing

Here is another sketch that was based on a examples by Daniel Schiffman to illustrate the use of a HashMap in vanilla processing.
NB: tested with development version of ruby-processing see here.
Here is the required word.rb:-
#######
# word.rb
# the word class stores data about words (text, source, frequency)
# thanks to processing mixin words are rendered according to
# source and frequency
########
class Word
  include Processing::Proxy
  # Store a count for occurences in two different books
  attr_reader :count_dracula, :count_franken, :total_count, :word, :position, :width, :height, :speed

  def initialize(s)
    @width, @height = $app.width, $app.height
    @count_dracula, @count_franken, @total_count = 0, 0, 0
    @position = [rand(width), rand(-height .. height*2)]
    @word = s
  end

  # We will display a word if it appears at least 5 times
  # and only in one of the books
  def qualify?
    return ((count_dracula == total_count || count_franken == total_count) && total_count > 5)
  end

  # Increment the count for Dracula
  def increment_dracula
    @count_dracula += 1
    @total_count += 1
  end


  # Increment the count for Frankenstein
  def increment_franken
    @count_franken += 1
    @total_count += 1
  end

  # The more often it appears, the faster it falls
  def move
    @speed = map(total_count, 5, 25, 0.1, 0.4).to_f
    @speed = constrain(speed, 0, 10.0)
    @position[Y] += speed

    if (position[Y] > height*2)
      @position[Y] = -height
    end
  end


  # Depending on which book it gets a color
  def display
    if (count_dracula > 0)
      fill(255)
    elsif (count_franken > 0)
      fill(0)
    end
    # Its size is also tied to number of occurences
    fs = map(total_count,5,25,2,24.0).to_f
    fs = constrain(fs, 2, 48)
    text_size(fs)
    text_align(CENTER)
    text(word, position[X], position[Y])
  end
end

Here is the sketch:-
#
# After a HashMap example
# by Daniel Shiffman.  
# 
# This example demonstrates how to use a Hash to store 
# a collection of objects referenced by a key. This is much like an array, 
# only instead of accessing elements with a numeric index, we use a String.
# If you are familiar with associative arrays from other languages,
# this is the same idea.
#
# The Processing classes IntHash, FloatHash, and StringHash offer a simple
# way of pairing Strings with numbers or other Strings. But are probably of 
# less interest to rubyists.
#
# In this example, words that appear in one book (Dracula) are colored white 
# whilst words in the other book (Frankenstein) are colored black.
#
load './word.rb'

DRACULA = "dracula.txt"
FRANKENSTEIN = "frankenstein.txt"

attr_accessor :words

def setup
  size(640, 360)

  # Create the HashMap
  @words = {}
  # Load two files
  load_file(DRACULA)
  load_file(FRANKENSTEIN)
  # Create the font
  text_font(create_font("Georgia", 24))
end

def draw
  background(126)
  # Show words
  words.values.each do |w|
    if w.qualify?
      w.display
      w.move
    end
  end
end

# Load a file
def load_file(filename)
  tokens = File.open(data_path(filename), "r"){|file| file.read.scan(/[\w'-]+/)}
  tokens.each do |s|
    s = s.downcase
    # Is the word in the HashMap
    if (words.has_key?(s))
      # Get the word object and increase the count
      # We access objects from a Hash via its key, the String
      w = words[s]
      # Which book am I loading?
      if (filename == DRACULA)
        w.increment_dracula
      elsif (filename == FRANKENSTEIN)
        w.increment_franken
      end
    else
      # Otherwise make a new word
      w = Word.new(s)
      # And add entry to the Hash 
      # The key for us is the String and the value is the Word object
      words[s] = w
      if (filename == DRACULA)
        w.increment_dracula
      elsif (filename == FRANKENSTEIN)
        w.increment_franken
      end
    end
  end
end

Here is the display:-

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2