Chrono Node - lmucs/grapevine GitHub Wiki
This page has information on how to use Chrono-Node.
chrono-node
Chrono is a natural language date parser in Javascript, designed for extracting date information from any given test. Source Code
###Setup Node.js
npm install chrono-node
Bower
npm install bower
bower install chrono
###Usage Simply pass a string to funciton chrono.parseDate or chrono.parse
> var chrono = require('chrono-node')
> chrono.parseDate('An appointment on Sep 12-13')
Fri Sep 12 2014 12:00:00 GMT-0500 (CDT)
> chrono.parse('An appointment on Sep 12-13');
[ { index: 18,
text: 'Sep 12-13',
tags: { ENMonthNameMiddleEndianParser: true },
start:
{ knownValues: [Object],
impliedValues: [Object] },
end:
{ knownValues: [Object],
impliedValues: [Object] } } ]
parse()
returns an array of ParsedResults. Since most posts will have zero or one events, the array that is returned will be empty or have a single element in most cases.
> myDate = chrono.parse('We will have pizza from 3-6pm tomorrow afternoon!');
[ { ref: Tue Sep 22 2015 10:21:39 GMT-0700 (PDT),
index: 19,
text: 'from 3-6pm tomorrow',
tags:
{ ENCasualDateParser: true,
ENTimeExpressionParser: true,
ENMergeDateAndTimeRefiner: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } } ]
###Examples
>var results = chrono.parse('ServeLA is Sept. 19 from 7:30 am-2:00 pm! Sign up for the free event here: http://bit.ly/1JXtzu7 @csa_lmu #ServeLA')
> results
[ { ref: Tue Sep 15 2015 16:48:06 GMT-0700 (PDT),
index: 11,
text: 'Sept. 19 from 7:30 am-2:00 pm',
tags:
{ ENMonthNameMiddleEndianParser: true,
ENTimeExpressionParser: true,
ENMergeDateAndTimeRefiner: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } } ]
> results[0].start.date()
Sat Sep 19 2015 07:30:00 GMT-0700 (PDT)
> results[0].end.date()
Sat Sep 19 2015 14:00:00 GMT-0700 (PDT)
> var results = chrono.parse('Not sure what is open today? The Lair is open 9am-7pm, Iggys from 11am-1am, C-Lion Levy 11am-2am, and C-Lion Del Rey 10am- 1:30am!')
> results
[ { ref: Tue Sep 15 2015 17:01:01 GMT-0700 (PDT),
index: 22,
text: 'today',
tags: { ENCasualDateParser: true },
start: { knownValues: [Object], impliedValues: [Object] } },
{ ref: Tue Sep 15 2015 17:01:01 GMT-0700 (PDT),
index: 46,
text: '9am-7pm',
tags: { ENTimeExpressionParser: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } },
{ ref: Tue Sep 15 2015 17:01:01 GMT-0700 (PDT),
index: 61,
text: 'from 11am-1am',
tags: { ENTimeExpressionParser: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } },
{ ref: Tue Sep 15 2015 17:01:01 GMT-0700 (PDT),
index: 88,
text: '11am-2am',
tags: { ENTimeExpressionParser: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } },
{ ref: Tue Sep 15 2015 17:01:01 GMT-0700 (PDT),
index: 117,
text: '10am- 1:30am',
tags: { ENTimeExpressionParser: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } } ]
> results[2].start.date()
Tue Sep 15 2015 11:00:00 GMT-0700 (PDT)
> results[2].end.date()
Tue Sep 15 2015 01:00:00 GMT-0700 (PDT)
Pros:
- Can recognize multiple events in one string
- Can be customized to recognize specific events (example can be found in the project's wiki)
- Output can be refined too
Cons:
- Takes the word 'today' by itself as an event in itself (can easily be ignored)
- Doesn't recognize an end time that goes to the next day as belonging to the next day. For example, The C-Lion event goes from 11am-1am, but chrono interprets the end date as being the same day than the start date
Issues With Chrono
- In the following example, Chrono will parse the single event into two separate events because of the period.
> myDate = chrono.parse("Next Sat. from 11:30-1:30, join LMU\'s second annual Community Open House! RSVPs are encouraged at [email protected] http://t.co/JIZwsotDVZ")
[ { ref: Tue Sep 22 2015 10:58:04 GMT-0700 (PDT),
index: 0,
text: 'Next Sat',
tags: {},
start: { knownValues: [Object], impliedValues: [Object] } },
{ ref: Tue Sep 22 2015 10:58:04 GMT-0700 (PDT),
index: 10,
text: 'from 11:30-1:30',
tags: { ENTimeExpressionParser: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } } ]
However, if we remove the period from Sat., we get the single event we desire.
> myDate = chrono.parse("Next Sat from 11:30-1:30, join LMU\'s second annual Community Open House! RSVPs are encouraged at [email protected] http://t.co/JIZwsotDVZ")
[ { ref: Tue Sep 22 2015 11:03:06 GMT-0700 (PDT),
index: 0,
text: 'Next Sat from 11:30-1:30',
tags:
{ ENTimeExpressionParser: true,
ENMergeDateAndTimeRefiner: true },
start: { knownValues: [Object], impliedValues: [Object] },
end: { knownValues: [Object], impliedValues: [Object] } } ]
We could process the string before passing it to Chrono, removing . from abbreviations.