README

Mon, 19 Mar 2012 22:26:07 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 19 Mar 2012 22:26:07 +0000
changeset 28
3ddc836b7845
parent 24
3ae5d7a20cec
permissions
-rw-r--r--

Add MIT/X11 licence

24
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 ndp is a "natural date parser" library. It allows you to convert natural language user input to a more machine-friendly time value.
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 ## Demo
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 An example utility is provided called readdate.lua, which reads lines from stdin, and
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 prints the interpreted date along with any text that wasn't considered part of the time
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 specification.
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 ## Examples
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 today
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 => Sun Jun 21 23:45:57 2009 (1245624357) [?,?]
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 feed the cat in the morning
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 => Mon Jun 22 09:30:09 2009 (1245659409): feed the cat [13,28]
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 go shopping next tuesday
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 => Tue Jun 23 23:46:27 2009 (1245797187): go shopping next [17,25]
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 service the car in September
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 => Mon Sep 21 23:46:48 2009 (1253573208): service the car [16,29]
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 Watch the games in July 2012
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 => Sat Jul 21 23:48:06 2012 (1342910886): Watch the games [16,29]
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 ## Usage
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 ...is simple :)
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 To get a time from an input string, simply do:
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local ndp = require "ndp"; -- Load the ndp library
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local mytime = ndp.when("tomorrow"); -- Get the date tomorrow
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 print(os.date("%c", mytime)); -- Print it out
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 A slightly more advanced usage is extracting information in the input string which
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 was not concerned with the time. This is useful for extracting things like "feed the cat"
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 from "feed the cat tomorrow afternoon". ndp.when() returns 2 extra values which are the start
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 and finish positions of the text it actually used in the string. By removing the text between
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 these positions you will be left with the real data.
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local ndp = require "ndp"; -- Load the ndp library
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local mytime, startpos, endpos = ndb.when("tomorrow afternoon, feed the cat");
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local message = (line:sub(1, start)..line:sub(finish, -1)) -- returns ", feed the cat"
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 -- To make things a bit neater, we can use Lua's gsub function to trim leftover spaces and
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 -- punctuation from the result:
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 message = message:gsub("^[%s%p]+", ""):gsub("[%s%p]+$", "");
3ae5d7a20cec Add initial README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 print(message); -- feed the cat

mercurial