Skip to content

Commit

Permalink
Refactoring some parts of the NLP to make it more testable.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Daniel Reed committed Jun 19, 2017
1 parent 2dbc25e commit 5a0f11a
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/middleware/nlp-middleware.js
Expand Up @@ -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;
}

Expand Down
125 changes: 125 additions & 0 deletions 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.');
// });
});

0 comments on commit 5a0f11a

Please sign in to comment.