From d9340b63087d18e4a620d60931f0d9e7da7cba87 Mon Sep 17 00:00:00 2001 From: evan wheeler Date: Mon, 12 Jan 2009 21:42:45 +0200 Subject: [PATCH] changed measurements to floats, added boolean, and turned off ratio --- lib/fuzz.rb | 4 ++-- lib/fuzz/token.rb | 2 +- lib/fuzz/token/boolean.rb | 28 ++++++++++++++++++++++++++++ lib/fuzz/token/height.rb | 10 +++++++--- lib/fuzz/token/length.rb | 12 +++++++++++- lib/fuzz/token/weight.rb | 10 +++++++--- 6 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 lib/fuzz/token/boolean.rb diff --git a/lib/fuzz.rb b/lib/fuzz.rb index d78c2b3..1625d25 100644 --- a/lib/fuzz.rb +++ b/lib/fuzz.rb @@ -5,7 +5,7 @@ module Fuzz # The character inserted in place of a Token # when it is plucked out of a string (to prevent - # the surrounding text from beind considered a + # the surrounding text from being considered a # single token, when it is clearly not) Replacement = 0.chr @@ -33,6 +33,6 @@ module Fuzz require "#{dir}/fuzz/token/age.rb" require "#{dir}/fuzz/token/length.rb" require "#{dir}/fuzz/token/height.rb" -require "#{dir}/fuzz/token/ratio.rb" +require "#{dir}/fuzz/token/boolean.rb" require "#{dir}/fuzz/token/weight.rb" diff --git a/lib/fuzz/token.rb b/lib/fuzz/token.rb index 1cab042..4082e6f 100644 --- a/lib/fuzz/token.rb +++ b/lib/fuzz/token.rb @@ -71,7 +71,7 @@ def pattern pat = self.class.const_get(:Pattern) # If the pattern contains no captures, wrap - # it in parenthesis to captures the whole + # it in parenthesis to capture the whole # thing. This is vanity, so we can omit # the parenthesis from the Patterns of # simple Token subclasses. diff --git a/lib/fuzz/token/boolean.rb b/lib/fuzz/token/boolean.rb new file mode 100644 index 0000000..8783776 --- /dev/null +++ b/lib/fuzz/token/boolean.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +# vim: noet + +module Fuzz::Token + class Boolean < Base + True = ["true", "t", "yes", "y"] + False = ["false", "f", "no", "n"] + Pattern = (True + False).join("|") + + # whatever variation was + # matched, return a regular symbol + def normalize(boolean_str) + True.include?(boolean_str) ? (:true) : (:false) + end + + def humanize(boolean_str) + boolean_str == :true ? "Y" : "N" + end + + Examples = { + "n" => :false, + "y" => :true, + "true" => :true, + "false" => :false, + "yes" => :true, + "no" => :false } + end +end diff --git a/lib/fuzz/token/height.rb b/lib/fuzz/token/height.rb index 64fe88a..4e603ea 100644 --- a/lib/fuzz/token/height.rb +++ b/lib/fuzz/token/height.rb @@ -5,12 +5,16 @@ module Fuzz::Token class Height < Base # this should be merged with the Length # token, by wrapping and patching the pattern - Pattern = '(?:(?:height of|height:?|standing)\s*)?(\d+)(?:\s*(?:centimeters?|cm))?(?:\s*(?:tall))?' + Pattern = '(?:(?:height of|height:?|standing)\s*)?(\d+)(\.\d+)?(?:\s*(?:centimeters?|cm))?(?:\s*(?:tall))?' # convert captured digits - # into a fixnum object + # into a float object def normalize(height_str) - height_str.to_i + height_str.to_f + end + + def humanize(height_f) + height_f.to_s + "cm" end end end diff --git a/lib/fuzz/token/length.rb b/lib/fuzz/token/length.rb index a4513bb..9aac66b 100644 --- a/lib/fuzz/token/length.rb +++ b/lib/fuzz/token/length.rb @@ -3,6 +3,16 @@ module Fuzz::Token class Length < Base - Pattern = '(\d+)(?:\s*(?:centimeters?|cm))?' + Pattern = '(\d+)(\.\d+)?(?:\s*(?:centimeters?|cm))?' + + # convert captured digits + # into a float object + def normalize(length_str) + length_str.to_f + end + + def humanize(length_f) + length_f.to_s + "cm" + end end end diff --git a/lib/fuzz/token/weight.rb b/lib/fuzz/token/weight.rb index 7951df3..b50bf03 100644 --- a/lib/fuzz/token/weight.rb +++ b/lib/fuzz/token/weight.rb @@ -5,16 +5,20 @@ module Fuzz::Token class Weight < Base Prefix = '(?:weighing?\s*)?' - Meat = '(\d+)' + Meat = '(\d+)(\.\d+)?' Suffix = '(?:\s*(?:kilogram?|kilogrammes?|kg))?' # create one big ugly regex Pattern = Prefix + Meat + Suffix # convert captured digits - # into a fixnum object + # into a float object def normalize(weight_str) - weight_str.to_i + weight_str.to_f + end + + def humanize(weight_f) + weight_f.to_s + "kg" end end end