You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RFC: Human readable duration parsing for Timex.Duration #362
Similar to Ruby's ChronicDuration gem, I need an elixir library that will support parsing natural language. Since Timex can output in a human readable format, it seems like it would make sense to be able to parse in such a way as well. Thoughts?
A naive recursive descent parser for this would be relatively easy to put together, and if I recall correctly, my example for creating custom formatters/parsers actually builds one. That said, to properly handle natural language, we'd probably want a basic implementation of an Earley parser which can properly handle ambiguous context-free grammars. I've had this on my todo list for a long time, but haven't had time to implement it. I'm certainly open to working with anyone interested on getting an implementation built and merged!
I just realized you specified this for durations only, so that simplifies things a bit, I suspect the naive approach might be enough to satisfy the requirements.
It covers around 90% of the use cases for which you would use ChronicDuration.parse. To simplify it, I removed the possibility to use the words and, plus, and with to join the time units.
I also does not assume that a time unit without a number defaults to 1, so hour 5 minutes will not default to 1 hour 5 minutes, instead it will only parse the 5 minutes. For most use cases this should be fine.
@bitwalker I was thinking about adding this code to Timex but couldn't figure out the best place to put it. Any suggestions?
Activity
bitwalker commentedon Sep 26, 2017
A naive recursive descent parser for this would be relatively easy to put together, and if I recall correctly, my example for creating custom formatters/parsers actually builds one. That said, to properly handle natural language, we'd probably want a basic implementation of an Earley parser which can properly handle ambiguous context-free grammars. I've had this on my todo list for a long time, but haven't had time to implement it. I'm certainly open to working with anyone interested on getting an implementation built and merged!
bitwalker commentedon Sep 26, 2017
I just realized you specified this for durations only, so that simplifies things a bit, I suspect the naive approach might be enough to satisfy the requirements.
suprnova32 commentedon Feb 28, 2018
I wanted the same functionality, so I decided to port some of the
ChronicDurationfunctionality and add it to my project. https://github.com/AlloyCI/alloy_ci/blob/master/lib/alloy_ci/lib/time_convert.ex#L36-L53It covers around 90% of the use cases for which you would use
ChronicDuration.parse. To simplify it, I removed the possibility to use the wordsand,plus, andwithto join the time units.I also does not assume that a time unit without a number defaults to 1, so
hour 5 minuteswill not default to1 hour 5 minutes, instead it will only parse the 5 minutes. For most use cases this should be fine.@bitwalker I was thinking about adding this code to
Timexbut couldn't figure out the best place to put it. Any suggestions?ccapndave commentedon Nov 5, 2020
Thanks for this!