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

Treat "an"s equal to "a"s #22

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions lib/providers/english_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class EnglishProvider < GenericProvider

def preprocess(string, ignore)
string.gsub!(/ +|([^\d])-([^\d])/, '\1 \2') # will mutilate hyphenated-words
string.gsub!(/\ba$/, '') && string.rstrip! # doesn't make sense for an 'a' at the end to be a 1
string.gsub!(/\ban?$/, '') && string.rstrip! # doesn't make sense for an 'a' or an 'an' at the end to be a 1
end

def numerize_numerals(string, ignore, bias)
Expand All @@ -121,9 +121,9 @@ def numerize_numerals(string, ignore, bias)
string.gsub!(/(^|\W)(#{single_nums})\s(#{dir_nums_ten_prefs})(?=$|\W)/i) {$1 << $2 << ' hundred ' << $3}
string.gsub!(/(^|\W)(#{dir_single_nums})(?=$|\W)/i) { $1 << '<num>' << DIRECT_SINGLE_NUMS[$2].to_s}
if bias == :ordinal
string.gsub!(/(^|\W)\ba\b(?=$|\W)(?! (?:#{ALL_ORDINALS_REGEX}))/i, '\1<num>' + 1.to_s)
string.gsub!(/(^|\W)\ban?\b(?=$|\W)(?! (?:#{ALL_ORDINALS_REGEX}))/i, '\1<num>' + 1.to_s)
else
string.gsub!(/(^|\W)\ba\b(?=$|\W)/i, '\1<num>' + 1.to_s)
string.gsub!(/(^|\W)\ban?\b(?=$|\W)/i, '\1<num>' + 1.to_s)
end

# ten, twenty, etc.
Expand All @@ -145,7 +145,7 @@ def numerize_fractions(string, ignore, bias)
end
quarters = regexify(['quarter', 'quarters'], ignore: ignore)

string.gsub!(/a (#{fractionals})(?=$|\W)/i) {'<num>1/' << ALL_FRACTIONS[$1].to_s}
string.gsub!(/an? (#{fractionals})(?=$|\W)/i) {'<num>1/' << ALL_FRACTIONS[$1].to_s}
# TODO : Find Noun Distinction for Quarter
if bias == :fractional
string.gsub!(/(^|\W)(#{fractionals})(?=$|\W)/i) {'/' << ALL_FRACTIONS[$2].to_s}
Expand Down
4 changes: 3 additions & 1 deletion test/test_numerizer_en.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_fractions_in_words
assert_equal "1/4", Numerizer.numerize("one quarter")
assert_equal "1/4", Numerizer.numerize("a quarter")
assert_equal "1/8", Numerizer.numerize("one eighth")
assert_equal "1/8", Numerizer.numerize("an eighth")

assert_equal "3/4", Numerizer.numerize("three quarters")
assert_equal "2/4", Numerizer.numerize("two fourths")
Expand All @@ -81,6 +82,7 @@ def test_fractions_in_words
def test_fractional_addition
assert_equal "1.25", Numerizer.numerize("one and a quarter")
assert_equal "2.375", Numerizer.numerize("two and three eighths")
assert_equal "2.125", Numerizer.numerize("two and an eighth")
assert_equal "2.5", Numerizer.numerize("two and a half")
assert_equal "3.5 hours", Numerizer.numerize("three and a half hours")
end
Expand All @@ -101,7 +103,7 @@ def test_compatability
assert_equal '1/2', Numerizer.numerize('1/2')
assert_equal '05/06', Numerizer.numerize('05/06')
assert_equal "3.5 hours", Numerizer.numerize("three and a half hours")
assert_equal "1/2 an hour", Numerizer.numerize("half an hour")
assert_equal "1/2 1 hour", Numerizer.numerize("half an hour")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jduff this might be an unintentional consequence of this change.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, I don't really like that this changes, a fraction next to 1 is kind of weird. Can you look into it and see if there's a way to make this work without it affecting this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this problem existed earlier with "a".

assert_equal "1/2 a kilo", Numerizer.numerize("half a kilo")

Screenshot 2021-02-18 at 11 30 57 AM

end

def test_ordinal_strings
Expand Down