diff --git a/lib/chronic/parser.rb b/lib/chronic/parser.rb index 3d4c95dd..a5988ab3 100644 --- a/lib/chronic/parser.rb +++ b/lib/chronic/parser.rb @@ -14,7 +14,8 @@ class Parser :guess => true, :ambiguous_time_range => 6, :endian_precedence => [:middle, :little], - :ambiguous_year_future_bias => 50 + :ambiguous_year_future_bias => 50, + :numerize => true } attr_accessor :now @@ -54,6 +55,10 @@ class Parser # look x amount of years into the future and past. If the # two digit year is `now + x years` it's assumed to be the # future, `now - x years` is assumed to be the past. + # :numerize - Attempt to parse numbers written in natural language + # (ex "twenty third" becomes "23rd"). Default is true (on). + # Disabling this option saves significant memory. + def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) @now = options.delete(:now) || Chronic.time_class.now @@ -102,7 +107,7 @@ def pre_normalize(text) text.gsub!(/,/, ' ') text.gsub!(/^second /, '2nd ') text.gsub!(/\bsecond (of|day|month|hour|minute|second)\b/, '2nd \1') - text = Numerizer.numerize(text) + text = Numerizer.numerize(text) if @options[:numerize] text.gsub!(/([\/\-\,\@])/) { ' ' + $1 + ' ' } text.gsub!(/(?:^|\s)0(\d+:\d+\s*pm?\b)/, ' \1') text.gsub!(/\btoday\b/, 'this day') diff --git a/test/test_parsing.rb b/test/test_parsing.rb index 577d00c4..444e6e24 100644 --- a/test/test_parsing.rb +++ b/test/test_parsing.rb @@ -1239,6 +1239,11 @@ def test_handle_rdn_rmn_od_sy assert_equal Time.local(2005, 12, 30, 12), time end + def test_handle_numerize_disabled + time = parse_now("aug 3", :numerize => false) + assert_equal Time.local(2007, 8, 3, 12), time + end + def test_normalizing_day_portions assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11") end