Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling Numerize to use far less memory #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/chronic/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down