Created
November 15, 2011 20:59
-
-
Save erochest/1368327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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