comtravo / ctparse

Parse natural language time expressions in python
https://www.comtravo.com
MIT License
131 stars 24 forks source link

The duration of "1 hour and 42 minutes" returns just 42 minutes, debug returns the values correctly #102

Closed BenjaminAfrolabs closed 1 year ago

BenjaminAfrolabs commented 3 years ago

Description

When calling ctparse("1 hour and 42 minutes") I expected to get a duration containing 1 hours and 42 minutes and I got back just the 42 minutes.

What I Did

Enabled debug and I got 2 duration objects, the first the hour the second the minutes

gabrielelanaro commented 3 years ago

what happened here is that ctparse doesn't support combining duration (as of yet), so it will just return one of the (both incorrect) matches.

one of the issue with these "combined espressions" is how to represent them, right now the representation is:

duration = value + unit

so that 1h and 30 minutes can't be represented, but it could be represented as 90 minutes.

I guess the best solution would be to:

  1. change the representation for Duration so that it can have multiple values + units (a bit like timedelta does)
  2. implement the rule that combines 1 hour + 30 minute. This should be pretty straightforward.

Durations are not working very well for a bunch of reason but we'd like to work on it soon, I apologize but I can't give you an ETA right now

BenjaminAfrolabs commented 3 years ago

No worries on the ETA since I can get it working with the array from the debug flag and convert it into python's timedelta doing this (it does break if I add *seconds though):

duration_params = {}
for t in ctparse(duration, debug=True):
   duration_params[t.resolution.unit.value] = t.resolution.value
duration = timedelta(**duration_params)

Combined expressions might also be logical to be returned as a list

sebastianmika commented 1 year ago

Added a simple solution in #129 - closing this for now