Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert English to Cron?

Tags:

parsing

nlp

I did some searching but haven't landed anything that looks useful yet but I am wondering if anyone knows of something (tool,lib etc) that can parse English phrases and translate them into a cron string.

For example: Every Tuesday at 15:00 converts to 0 15 * * 2

It seems like something that would have lots of gotchas and it would be preferable to benefit from someone elses work. You see it in a few nice sites/apps that can work out what you mean from a simple phrase rather than having some hideous user interface.

Thanks in advance.

like image 494
Tom Duckering Avatar asked Dec 16 '09 08:12

Tom Duckering


3 Answers

Though this is an old question I would like to list out all libraries/tools that I know so far, so that this answer might help others who arrive on this page looking for the same thing:

JavaScript:

  • natural-cron.js (link)

  • friendly-cron (link)

PHP:

  • natural-cron-expression (link)

Ruby:

  • whenever (link)

Feel free to reply in comments, if you know about any other library which is not listed here :)

(Full disclosure: natural-cron.js has been developed by me & my friend, when no other library satisfied the needs of our project)

like image 137
dedman Avatar answered Oct 02 '22 14:10

dedman


For Ruby there's "Whenever" which might provide a starting point: It translates quasi-english (actually it's valid Ruby) into cron strings.

like image 33
Beat Bolli Avatar answered Oct 02 '22 13:10

Beat Bolli


Depending on how flexible you need it to be, and how willing to roll up your own sleeves you are, you could define a simple grammar for this.

Every would be a quantifier. You may need others but I can't think of any. Valid syntax might be:

Every (day-spec) AT (time)

Where day-spec could be a literal day (ie. Monday) or be a day of the month (ie. 30th Day) or some other syntax (I'd suggest fortnights but I'm not sure if Cron can represent those well).

Time could be specified using either 24 hour (16:00) or 12 hour (4:00pm) format.

Another syntax that you might want is: Every (frequency) From (time) where frequency is basically (quantity) (unit) (ie. 10 Minutes). The from time enables you to set an offset (eg. Every 30 Minutes From 01:10am).

You'd probably need to sit down and figure out these details a bit more. But a rigid grammar could be implemented relatively easily using recursive descent.

like image 21
Adam Luchjenbroers Avatar answered Oct 02 '22 13:10

Adam Luchjenbroers