Skip to content

Commit

Permalink
changed measurements to floats, added boolean, and turned off ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
evan wheeler committed Jan 12, 2009
1 parent 125a30c commit d9340b6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/fuzz.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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"

2 changes: 1 addition & 1 deletion lib/fuzz/token.rb
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions 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
10 changes: 7 additions & 3 deletions lib/fuzz/token/height.rb
Expand Up @@ -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
12 changes: 11 additions & 1 deletion lib/fuzz/token/length.rb
Expand Up @@ -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
10 changes: 7 additions & 3 deletions lib/fuzz/token/weight.rb
Expand Up @@ -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

0 comments on commit d9340b6

Please sign in to comment.