UNPKG

node-nlp

Version:

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

128 lines (98 loc) 3.8 kB
var moment = require('moment'); function ParsedResult(result){ result = result || {}; this.ref = result.ref; this.index = result.index; this.text = result.text; this.tags = result.tags || {}; this.start = new ParsedComponents(result.start, result.ref) if(result.end){ this.end = new ParsedComponents(result.end, result.ref) } } ParsedResult.prototype.clone = function() { var result = new ParsedResult(this); result.tags = JSON.parse(JSON.stringify(this.tags)); result.start = this.start.clone(); if (this.end) { result.end = this.end.clone(); } } ParsedResult.prototype.hasPossibleDates = function() { return this.start.isPossibleDate() && (!this.end || this.end.isPossibleDate()); } function ParsedComponents (components, ref){ this.knownValues = {}; this.impliedValues = {}; if (components) { for (key in components) { this.knownValues[key] = components[key]; } } if (ref) { ref = moment(ref); this.imply('day', ref.date()) this.imply('month', ref.month() + 1) this.imply('year', ref.year()) } this.imply('hour', 12); this.imply('minute', 0); this.imply('second', 0); this.imply('millisecond', 0); } ParsedComponents.prototype.clone = function () { var component = new ParsedComponents(); component.knownValues = JSON.parse(JSON.stringify(this.knownValues)); component.impliedValues = JSON.parse(JSON.stringify(this.impliedValues)); return component; }; ParsedComponents.prototype.get = function(component, value) { if (component in this.knownValues) return this.knownValues[component]; if (component in this.impliedValues) return this.impliedValues[component]; }; ParsedComponents.prototype.assign = function(component, value) { this.knownValues[component] = value; delete this.impliedValues[component]; }; ParsedComponents.prototype.imply = function(component, value) { if (component in this.knownValues) return; this.impliedValues[component] = value; }; ParsedComponents.prototype.isCertain = function(component) { return component in this.knownValues; }; ParsedComponents.prototype.isPossibleDate = function() { var dateMoment = this.moment(); if (this.isCertain('timezoneOffset')) { dateMoment.utcOffset(this.get('timezoneOffset')) } if (dateMoment.get('year') != this.get('year')) return false; if (dateMoment.get('month') != this.get('month')-1) return false; if (dateMoment.get('date') != this.get('day')) return false; if (dateMoment.get('hour') != this.get('hour')) return false; if (dateMoment.get('minute') != this.get('minute')) return false; return true; }; ParsedComponents.prototype.date = function() { var dateMoment = this.moment(); return dateMoment.toDate(); }; ParsedComponents.prototype.moment = function() { var dateMoment = moment(); dateMoment.set('year', this.get('year')); dateMoment.set('month', this.get('month')-1); dateMoment.set('date', this.get('day')); dateMoment.set('hour', this.get('hour')); dateMoment.set('minute', this.get('minute')); dateMoment.set('second', this.get('second')); dateMoment.set('millisecond', this.get('millisecond')); // Javascript Date Object return minus timezone offset var currentTimezoneOffset = dateMoment.utcOffset(); var targetTimezoneOffset = this.isCertain('timezoneOffset') ? this.get('timezoneOffset') : currentTimezoneOffset; var adjustTimezoneOffset = targetTimezoneOffset - currentTimezoneOffset; dateMoment.add(-adjustTimezoneOffset, 'minutes'); return dateMoment; }; exports.ParsedComponents = ParsedComponents; exports.ParsedResult = ParsedResult;