Skip to content

RFC: Human readable duration parsing for Timex.Duration #362

Open

Description

@cevn

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?

require 'chronic_duration'
=> true
>> ChronicDuration.parse('4 minutes and 30 seconds')
=> 270
>> ChronicDuration.parse('0 seconds')
=> nil
>> ChronicDuration.parse('0 seconds', :keep_zero => true)
=> 0

Activity

  1. bitwalker commented on Sep 26, 2017

    @bitwalker
    Owner

    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!

  2. bitwalker commented on Sep 26, 2017

    @bitwalker
    Owner

    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.

  3. suprnova32 commented on Feb 28, 2018

    @suprnova32

    I wanted the same functionality, so I decided to port some of the ChronicDuration functionality and add it to my project. https://github.com/AlloyCI/alloy_ci/blob/master/lib/alloy_ci/lib/time_convert.ex#L36-L53

    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?

  4. ccapndave commented on Nov 5, 2020

    @ccapndave

    Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions