mojombo / chronic

Chronic is a pure Ruby natural language date parser.
http://injekt.github.com/chronic
MIT License
3.23k stars 452 forks source link

Chronic thinks it can parse a date if the string begins "a " #402

Open colinlieberman opened 3 years ago

colinlieberman commented 3 years ago

Apologies if this has already been addressed and I didn't find it when searching.

ruby 2.4.0p0 chronic 0.10.2

First had this issue with the string "A Swiftly Turning Planet", and expected a nil return:

> Chronic.parse("A Swiftly Tilting Planet")
 => 2020-09-24 13:00:00 -0700

After some poking around, it looks like it's the letter A followed by a space that's the issue:

2.4.0 :001 > Chronic.parse("tomatoes")
 => nil
2.4.0 :002 > Chronic.parse("A Swiftly Tilting Planet")
 => 2020-09-24 13:00:00 -0700
2.4.0 :003 > Chronic.parse("A Swiftly Tilting Planet", guess: false)
 => 2020-09-24 13:00:00 -0700..2020-09-24 13:00:01 -0700
2.4.0 :004 > Chronic.parse("A Swiftly Tilting Planet")
 => 2020-09-24 13:00:00 -0700
2.4.0 :005 > Chronic.parse("A Swiftly zTilting Planet")
 => 2020-09-24 13:00:00 -0700
2.4.0 :006 > Chronic.parse("A Swiftly zTilting Planetzzzz")
 => 2020-09-24 13:00:00 -0700
2.4.0 :007 > Chronic.parse("abracadabra")
 => nil
2.4.0 :008 > Chronic.parse("abracadabra tomatoes")
 => nil
2.4.0 :009 > Chronic.parse("abracadabra tomatoes planet")
 => nil
2.4.0 :010 > Chronic.parse("abracadabra tomatoes planet swift")
 => nil
2.4.0 :011 > Chronic.parse("A potatoe")
 => 2020-09-24 13:00:00 -0700
2.4.0 :012 > Chronic.parse("A potato")
 => 2020-09-24 13:00:00 -0700
2.4.0 :013 > Chronic.parse("B potato")
 => nil
2.4.0 :014 > Chronic.parse("A potato")
 => 2020-09-24 13:00:00 -0700
2.4.0 :015 > Chronic.parse("Apotato")
 => nil
anuja-joshi commented 3 years ago

Same happens for string starting with letter "p" and having number

[91] pry(main)> Chronic.parse("p 2")
=> 2021-01-22 14:00:00 +0530
[92] pry(main)> Chronic.parse("panything random 2")
=> 2021-01-22 14:00:00 +0530
[93] pry(main)> Chronic.parse("p nything random 2")
=> 2021-01-22 14:00:00 +0530
[94] pry(main)> Chronic.parse("p nything random")
=> nil
nanobowers commented 2 years ago

@colinlieberman it's due to this line in the pre-normalization step:

    text.gsub!(/^\s?an? /i, '1 ')

which converts any a/an followed by a space into the numeric '1' which is allowed as a standalone scalar value and interpreted as a time (1pm?). AFAICT it's there to address interpreting "a minute" as "1 minute" or "an hour" as "1 hour" cases.