UNPKG

node-nlp

Version:

Library for NLU (Natural Language Understanding) done in Node.js

84 lines (54 loc) 2.53 kB
// QUnit Setup //------------------------------------- var test = function() { QUnit.test.apply(QUnit.test, arguments); } var ok = function() { QUnit.assert.ok.apply(QUnit.assert, arguments); } //------------------------------------- test("Test - Custom parser example", function() { var christmasParser = new chrono.Parser(); // Provide search pattern christmasParser.pattern = function () { return /Christmas/i } // This function will be called when matched pattern is found christmasParser.extract = function(text, ref, match, opt) { // Return a parsed result, that is 25 December return new chrono.ParsedResult({ ref: ref, text: match[0], index: match.index, start: { day: 25, month: 12, } }); } var custom = new chrono.Chrono(); custom.parsers.push(christmasParser); var resultDate = custom.parseDate("I'll arrive at 2.30AM on Christmas night", new Date(2013, 11, 10)) var expectDate = new Date(2013, 12-1, 25, 2, 30, 00); ok(Math.abs(expectDate.getTime() - resultDate.getTime()) < 100000, 'Test result.startDate ' + resultDate +'/' +expectDate) }); test("Test - Custom refiner example", function() { var guessPMRefiner = new chrono.Refiner(); // If there is no AM/PM specified, all time between 1:00 - 4:00 will be guessed as PM (13.00 - 16.00) guessPMRefiner.refine = function(text, results, opt) { results.forEach(function (result) { if (!result.start.isCertain('meridiem') && result.start.get('hour') >= 1 && result.start.get('hour') < 4) { result.start.assign('meridiem', 1); result.start.assign('hour', result.start.get('hour') + 12); } }); return results; } var custom = new chrono.Chrono(); custom.refiners.push(guessPMRefiner); var resultDate = custom.parseDate("This is at 2.30", new Date(2013, 11, 10)) var expectDate = new Date(2013, 12-1, 10, 14, 30, 00); ok(Math.abs(expectDate.getTime() - resultDate.getTime()) < 100000, 'Test result.startDate ' + resultDate +'/' +expectDate) var resultDate = custom.parseDate("This is at 2.30 AM", new Date(2013, 11, 10)) var expectDate = new Date(2013, 12-1, 10, 2, 30, 00); ok(Math.abs(expectDate.getTime() - resultDate.getTime()) < 100000, 'Test result.startDate ' + resultDate +'/' +expectDate) });