Skip to content

Instantly share code, notes, and snippets.

@erochest
Created November 15, 2011 20:59
Show Gist options
  • Save erochest/1368327 to your computer and use it in GitHub Desktop.
Save erochest/1368327 to your computer and use it in GitHub Desktop.
class Freq
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :input_text
validates :input_text, :presence => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
def tokens
@input_text.split
.map { |token| normalize(token) }
.select { |token| ! token.empty? }
end
def normalize(token)
token.gsub(/\W/, '').downcase
end
def freqs
counts = Hash.new(0)
tokens.each do |token|
counts[token] += 1
end
counts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment