ndp.lua

Sun, 21 Jun 2009 23:12:23 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 21 Jun 2009 23:12:23 +0100
changeset 17
3a7672943895
parent 16
35fb87e8289c
child 21
983f96ebef08
permissions
-rw-r--r--

Correct (I think) notation to make optional phrases optional

0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module(..., package.seeall);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 require "luarocks.require"
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 require "lpeg"
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Add case-insensitive string matching to Lpeg
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function lpeg.Pi(s)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local patt = lpeg.P(true);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 for c in s:gmatch(".") do
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 patt = patt * (lpeg.P(c:lower()) + lpeg.P(c:upper()));
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return patt;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 function lpeg.one_of(list)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local patt = lpeg.P(false);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 for _, match in ipairs(list) do
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 patt = patt + lpeg.Pi(match);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return patt;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
12
a127265f3257 Match only at word boundaries
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
23 local wordsep = lpeg.S" ";
a127265f3257 Match only at word boundaries
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
24
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local ordinal = lpeg.P{ lpeg.C(lpeg.R("09")^-2) * (lpeg.Pi("st") + lpeg.Pi("nd") + lpeg.Pi("rd") + lpeg.Pi("th")) + 1 * lpeg.V(1) };
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local number = lpeg.R "09"^1
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local day_name = lpeg.one_of {'monday', 'tuesday', 'wednesday',
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 'thursday', 'friday', 'saturday', 'sunday'}
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local month_name = lpeg.one_of {'january', 'february', 'march', 'april', 'may', 'june',
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 'july', 'august', 'september', 'october', 'november', 'december' }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
5
3eee607e57e8 Add support for specifying a year in the input
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
34 local year = lpeg.R("09") * lpeg.R("09") * lpeg.R("09") * lpeg.R("09");
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local unit_of_time = lpeg.one_of { 'second', 'minute', 'hour', 'day', 'week', 'month', 'year' }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local time_of_day = lpeg.one_of { 'morning', 'noon', 'afternoon', 'evening', 'night', 'midnight' }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local time_of_days = { morning = 09, noon = 12, afternoon = 13, evening = 17, night = 21, midnight = 00 }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local quantity;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local quantities = {
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 ["a"] = 1;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 ["an"] = 1;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 ["a couple of"] = 2;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 ["a few"] = 3;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 ["several"] = 3;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 };
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- Create 'quantity' to match any of the quantities we know
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 do
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local quantity_list = {};
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 for k in pairs(quantities) do
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 quantity_list[#quantity_list+1] = k;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 table.sort(quantity_list, function (a,b) return #a>#b; end);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 quantity = number + lpeg.one_of(quantity_list);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 seconds_in_a = { second = 1 }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 seconds_in_a.minute = seconds_in_a.second * 60;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 seconds_in_a.hour = seconds_in_a.minute * 60;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 seconds_in_a.day = seconds_in_a.hour * 24;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 seconds_in_a.week = seconds_in_a.day * 7;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 seconds_in_a.month = seconds_in_a.week * 4;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 seconds_in_a.year = seconds_in_a.day * 365;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local function get_time_part(time, part)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return os.date("*t", time)[part];
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local function adjust_time(time, part, value)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local split_time = os.date("*t", time);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 split_time[part] = value;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return os.time(split_time);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
1
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
82 local function find_next_day_by_name(time, day_name)
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
83 day_name = day_name:lower():gsub("^.", string.upper); -- Normalize
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
84
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
85 for i=1,8 do
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
86 time = time + seconds_in_a.day;
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
87 if os.date("%A", time) == day_name then
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
88 return time;
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
89 end
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
90 end
6
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
91 return;
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
92 end
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
93
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
94 local function find_next_month_by_name(time, month_name)
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
95 month_name = month_name:lower():gsub("^.", string.upper); -- Normalize
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
96
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
97 local split_time = os.date("*t", time);
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
98 for i=1,13 do
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
99 split_time.month = split_time.month + 1;
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
100 if split_time.month == 13 then split_time.month = 1; end
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
101
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
102 time = os.time(split_time);
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
103 if os.date("%B", time) == month_name then
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
104 return time;
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
105 end
a8bbbfdd12db Implement find_next_month_by_name
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
106 end
1
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
107
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
108 return;
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
109 end
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
110
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 function when(str, relative_to)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local time = relative_to or os.time();
13
f59a3859363e Match case-insensitively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
113 local P, Pi = lpeg.P, lpeg.Pi;
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local patterns =
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 {
13
f59a3859363e Match case-insensitively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
117 { Pi"tomorrow" /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 function ()
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 time = time + seconds_in_a.day;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end };
13
f59a3859363e Match case-insensitively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
121 { Pi"next week" /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 function ()
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 time = time + seconds_in_a.week;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end };
13
f59a3859363e Match case-insensitively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
125 { Pi"next year" /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 function ()
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 time = adjust_time(time, "year", get_time_part(time, "year") + 1);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end };
5
3eee607e57e8 Add support for specifying a year in the input
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
129 { year /
3eee607e57e8 Add support for specifying a year in the input
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
130 function (year)
3eee607e57e8 Add support for specifying a year in the input
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
131 time = adjust_time(time, "year", tonumber(year));
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end };
17
3a7672943895 Correct (I think) notation to make optional phrases optional
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
133 { (Pi"in " + true) * month_name /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 function (month_name)
1
29a492373a7c Implement find_next_day_by_name
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
135 time = find_next_month_by_name(time, month_name:match("%S+$"));
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end };
17
3a7672943895 Correct (I think) notation to make optional phrases optional
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
137 { (Pi"on " + true) * day_name /
7
7f095ddcfd54 Restore 'on day_name' match
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
138 function (day_name)
7f095ddcfd54 Restore 'on day_name' match
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
139 time = find_next_day_by_name(time, day_name:match("%S+$"));
7f095ddcfd54 Restore 'on day_name' match
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
140 end };
17
3a7672943895 Correct (I think) notation to make optional phrases optional
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
141 { (Pi"in " + true) * ( quantity * P" " * unit_of_time ) * (P"s"^-1) /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 function (number_and_unit)
17
3a7672943895 Correct (I think) notation to make optional phrases optional
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
143 print("m", number_and_unit..".");
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$");
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 number = quantities[number] or tonumber(number);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 time = time + seconds_in_a[unit] * number;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end };
14
f5bbdcb5b148 Use one_of to improve matching
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
150 { lpeg.one_of{"this ", "in the ", "at "} * time_of_day /
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 function (time_of_day)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 time_of_day = time_of_day:match("%S+$");
15
9c9e64488e54 Allow to specify 'in the morning' to refer to tomorrow
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
153
9c9e64488e54 Allow to specify 'in the morning' to refer to tomorrow
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
154 if time_of_day == "morning" and get_time_part(time, "hour") > time_of_days.morning then
9c9e64488e54 Allow to specify 'in the morning' to refer to tomorrow
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
155 time = time + seconds_in_a.day; -- Morning has passed, so next morning
9c9e64488e54 Allow to specify 'in the morning' to refer to tomorrow
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
156 end
9c9e64488e54 Allow to specify 'in the morning' to refer to tomorrow
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
157
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 time = adjust_time(time, "hour", time_of_days[time_of_day]);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if time_of_day == "noon" or time_of_day == "midnight" then
16
35fb87e8289c Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
160 time = adjust_time(time, "min", 00);
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 else
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 time = adjust_time(time, "min", 30);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
16
35fb87e8289c Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
164 end };
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 }
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
9
dc0c8914ac04 Return the position of the start and end of the matched text
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
167 local ret, min_pos, max_pos;
11
a40db377a1b5 Optimisation by saving a function per loop iteration
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
168 local function check_min_pos(start) start = start - 1; if not min_pos or start < min_pos then min_pos = start; end end;
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 for _, pattern in pairs(patterns) do
12
a127265f3257 Match only at word boundaries
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
170 ret = lpeg.match(lpeg.P{ lpeg.Cp()*pattern[1] + 1 * (1-wordsep)^0 * wordsep * lpeg.V(1) }/check_min_pos, str);
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 if ret then
9
dc0c8914ac04 Return the position of the start and end of the matched text
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
172 if not max_pos or ret > max_pos then max_pos = ret; end
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 --print("Matches ".._.." until "..ret);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176
9
dc0c8914ac04 Return the position of the start and end of the matched text
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
177 return time, min_pos, max_pos;
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179

mercurial