From 5a0f11abff1a2bef9617635a00c9014bdfe4beb5 Mon Sep 17 00:00:00 2001 From: Daniel Reed Date: Mon, 19 Jun 2017 12:02:44 -0600 Subject: [PATCH] Refactoring some parts of the NLP to make it more testable. And adding some basic tests. There is an issue in the resolveDevices that will need to be resolved to make all of the tests work. --- src/middleware/nlp-middleware.js | 57 +++++++------- src/test/nlp-middleware.js | 125 +++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 src/test/nlp-middleware.js diff --git a/src/middleware/nlp-middleware.js b/src/middleware/nlp-middleware.js index aeebd7f..831c6e6 100644 --- a/src/middleware/nlp-middleware.js +++ b/src/middleware/nlp-middleware.js @@ -74,40 +74,39 @@ const sentenceSpacer = /\s*([?!])\s*|(?!\d*\.\d+)\s*([.])\s*/g; //Matches the end of a sentence, optionally matching . or ! or ? const sentenceTerminal = /\s*[.?!]{0,1}$/; -export default (robot) => { - robot.receiveMiddleware((context, next, done) => { - const message = context.response.message.message || context.response.message; - const cleaned = message.text.replace(wordSpacer, ' ').replace(sentenceSpacer, "$1$2 ").trim(); - - let cleanSentences = null; - // if text contains IP address don't run through nlp_compromise - if (/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/.test(cleaned)) { - cleanSentences = cleaned; - } else { - let normalized = nlp.text(cleaned)/*.to_present().toAmerican()*/.toNormal(); - if (normalized.contractions && normalized.contractions.expand) { - normalized = normalized.contractions.expand(); - } - //Ensure that all sentences end with '.' a note, this will replace ! and ? with . - cleanSentences = nlp.text(normalized.text()).sentences.map((s) => { - return s.str.trim().replace(sentenceTerminal, '.'); - }).join(' '); +export function runNLP (message, logger) { + const cleaned = message.text.replace(wordSpacer, ' ').replace(sentenceSpacer, "$1$2 ").trim(); + + let cleanSentences = null; + // if text contains IP address don't run through nlp_compromise + if (/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/.test(cleaned)) { + cleanSentences = cleaned; + } else { + let normalized = nlp.text(cleaned)/*.to_present().toAmerican()*/.toNormal(); + if (normalized.contractions && normalized.contractions.expand) { + normalized = normalized.contractions.expand(); } + //Ensure that all sentences end with '.' a note, this will replace ! and ? with . + cleanSentences = nlp.text(normalized.text()).sentences.map((s) => { + return s.str.trim().replace(sentenceTerminal, '.'); + }).join(' '); + } - const start = Date.now(); - const resolved = lex.resolveDevices(cleanSentences).trim(); - robot.logger.debug('Took', (Date.now() - start) +"ms", "to resolve dependencies, normalized string: ", resolved); - - - - //TODO Normalize the first question to ### questions in the case of multiple sentences + const start = Date.now(); + const resolved = lex.resolveDevices(cleanSentences).trim(); + logger.debug('Took', (Date.now() - start) +"ms", "to resolve dependencies, normalized string: ", resolved); - message.original_text = message.text; - message.text = resolved; - message.nlp = nlp.text(message.text); + message.original_text = message.text; + message.text = resolved; + message.nlp = nlp.text(message.text); +} +export default (robot) => { + robot.receiveMiddleware((context, next, done) => { + const message = context.response.message.message || context.response.message; + runNLP(message, robot.logger); - if (!resolved.includes('@' + robot.name)) { + if (!message.text.includes('@' + robot.name)) { context.response.message.done = true; } diff --git a/src/test/nlp-middleware.js b/src/test/nlp-middleware.js new file mode 100644 index 0000000..1e7526e --- /dev/null +++ b/src/test/nlp-middleware.js @@ -0,0 +1,125 @@ +/* +(c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +import { runNLP } from '../middleware/nlp-middleware'; +import { Lexer } from '../middleware/nlp-middleware'; +let chai = require('chai'); +let sinon = require('sinon'); + +chai.should(); + +describe('NLP Middleware', () => { + + const logger = { + debug: function(params) {} + }; + + //add a template + Lexer.addNamedDevice('template1', '/rest/server-profile-templates/ad51166f', 'name', 'https://10.1.1.1/#/profile-templates/show/overview/r/rest/server-profile-templates/ad51166f?s_sid=LTE', undefined); + //add a blade + Lexer.addNamedDevice('0000A661, bay 2', '/rest/server-hardware/30303437', 'name', 'https://10.1.1.1/#/server-hardware/show/overview/r/rest/server-hardware/30303437?s_sid=LTE', 'HPE Synergy Compute Module'); + //add a profile + Lexer.addNamedDevice('profile1', '/rest/server-profiles/eb13eab1', 'name', 'https://10.1.1.1/#/profiles/show/overview/r/rest/server-profiles/eb13eab1?s_sid=LTE', undefined); + //add a complex profile + Lexer.addNamedDevice('profile1 - 0000A661, bay 2', '/rest/server-profiles/eb13eab2', 'name', 'https://10.1.1.1/#/profiles/show/overview/r/rest/server-profiles/eb13eab2?s_sid=LTE', undefined); + //add a complex profile + Lexer.addNamedDevice('profile1 - docker', '/rest/server-profiles/eb13eab3', 'name', 'https://10.1.1.1/#/profiles/show/overview/r/rest/server-profiles/eb13eab3?s_sid=LTE', undefined); + + it('runNLP simple template', () => { + const Message = { + user: { id: '1', name: 'Shell', room: 'Shell' }, + text: '@hubot show template1', + id: 'messageId', + done: false, + room: 'Shell' + }; + + runNLP(Message, logger); + Message.text.should.equal('@hubot show /rest/server-profile-templates/ad51166f.'); + Message.original_text.should.equal('@hubot show template1'); + Message.nlp.raw_text.should.equal('@hubot show /rest/server-profile-templates/ad51166f.'); + }); + + it('runNLP simple profile', () => { + const Message = { + user: { id: '1', name: 'Shell', room: 'Shell' }, + text: '@hubot show profile1', + id: 'messageId', + done: false, + room: 'Shell' + }; + + runNLP(Message, logger); + Message.text.should.equal('@hubot show /rest/server-profiles/eb13eab1.'); + Message.original_text.should.equal('@hubot show profile1'); + Message.nlp.raw_text.should.equal('@hubot show /rest/server-profiles/eb13eab1.'); + }); + + it('runNLP simple hardware', () => { + const Message = { + user: { id: '1', name: 'Shell', room: 'Shell' }, + text: '@hubot show 0000A661, bay 2', + id: 'messageId', + done: false, + room: 'Shell' + }; + + runNLP(Message, logger); + Message.text.should.equal('@hubot show /rest/server-hardware/30303437.'); + Message.original_text.should.equal('@hubot show 0000A661, bay 2'); + Message.nlp.raw_text.should.equal('@hubot show /rest/server-hardware/30303437.'); + }); + + //TODO add tests for more complex resource names for each of the resources (SH, SP and SPT) + + // TODO fix resolveDevices so this test passes + // it('runNLP complex profile', () => { + // const Message = { + // user: { id: '1', name: 'Shell', room: 'Shell' }, + // text: '@hubot show profile1 - docker', + // id: 'messageId', + // done: false, + // room: 'Shell' + // }; + // + // runNLP(Message, logger); + // Message.text.should.equal('@hubot show /rest/server-profiles/eb13eab3.'); + // Message.original_text.should.equal('@hubot show profile1 - docker'); + // Message.nlp.raw_text.should.equal('@hubot show /rest/server-profiles/eb13eab37.'); + // }); + + // TODO fix resolveDevices so this test passes + // it('runNLP complex profile', () => { + // const Message = { + // user: { id: '1', name: 'Shell', room: 'Shell' }, + // text: '@hubot show profile1 - 0000A661, bay 2', + // id: 'messageId', + // done: false, + // room: 'Shell' + // }; + // + // runNLP(Message, logger); + // Message.text.should.equal('@hubot show /rest/server-profiles/eb13eab2.'); + // Message.original_text.should.equal('@hubot show profile1 - 0000A661, bay 2'); + // Message.nlp.raw_text.should.equal('@hubot show /rest/server-profiles/eb13eab2.'); + // }); +});