Compare commits

..

17 Commits

Author SHA1 Message Date
Will Webberley
a82756ab73 updated refs to github repos 2018-07-11 13:15:04 +01:00
Will Webberley
2c527f95fd
Merge pull request #22 from jtorreggiani/multi-responses-relationship-questions
Multiple responses to relationship questions
2018-03-19 19:21:50 +00:00
Joe Torreggiani
6229aa5aaa Add test for what relationship questions 2018-03-18 00:45:49 -04:00
Joe Torreggiani
505dec2ac4 Add support for multiple responses to what relationship questions 2018-03-18 00:45:08 -04:00
Will Webberley
feb2962d36 fixed lint issues and bumped version 2017-07-27 18:31:41 +01:00
Will Webberley
7080db074a 3.0.12 2017-07-27 18:27:04 +01:00
Will Webberley
65da1e698d added gist card handlers and implemented 'gist policy' logic 2017-07-27 18:24:04 +01:00
Will Webberley
e28648a65a 3.0.11 2017-07-21 21:52:14 +01:00
Will Webberley
a215382af1 changed all *Parser error/success methods to be static 2017-07-21 21:09:03 +01:00
Will Webberley
1cf2e0d601 reduced the likelihood of uncaught exceptions in the CEParser and QuestionParser parse functions 2017-07-21 20:55:20 +01:00
Will Webberley
96e8adbcd0 reduced chance of questionparser returning a null value 2017-07-21 20:49:07 +01:00
Will Webberley
4b6ea42053 updated return type of key parsers to improve consistency 2017-07-21 19:46:56 +01:00
Will Webberley
e4b2955211 3.0.10 2017-07-07 20:14:50 +01:00
Will Webberley
d5ae77bc31 Allow the CEServer /instance endpoint to also return the instance gist 2017-07-07 20:14:38 +01:00
Will Webberley
3d4851d695 fixed lint issues 2017-07-07 19:13:53 +01:00
Will Webberley
94ddd18a5b 3.0.9 2017-07-07 19:11:48 +01:00
Will Webberley
3e68cc6966 allow for querying instances through CEServer by name as well as ID 2017-07-07 19:11:07 +01:00
14 changed files with 302 additions and 325 deletions

View File

@ -4,7 +4,7 @@ A pure JavaScript implementation of the ITA project's CEStore - called CENode. C
Please visit the project's [home page](http://cenode.io) for more information and for documentation. Please visit the project's [home page](http://cenode.io) for more information and for documentation.
**We recommend beginners check out the [Getting Started Guide](https://github.com/flyingsparx/CENode/wiki/Getting-Started-Guide) before continuing.** **We recommend beginners check out the [Getting Started Guide](https://github.com/willwebberley/CENode/wiki/Getting-Started-Guide) before continuing.**
## Getting started ## Getting started
@ -33,13 +33,13 @@ const CEModels = require('cenode/models'); // if requred
const node = new CENode(CEModels.core); const node = new CENode(CEModels.core);
``` ```
See the [Wiki](https://github.com/flyingsparx/CENode/wiki) for further guides and the API reference. See the [Wiki](https://github.com/willwebberley/CENode/wiki) for further guides and the API reference.
## Testing ## Testing
Clone the repository Clone the repository
``` ```
git clone git@github.com:flyingsparx/CENode.git git clone git@github.com:willwebberley/CENode.git
``` ```
Install the necessary dev dependencies. Install the necessary dev dependencies.
@ -54,7 +54,7 @@ npm test
## More Information ## More Information
See the CENode [Wiki](https://github.com/flyingsparx/CENode/wiki) for more information, guides, and the API reference. See the CENode [Wiki](https://github.com/willwebberley/CENode/wiki) for more information, guides, and the API reference.
## Licence ## Licence

View File

@ -1,21 +0,0 @@
module.exports = {
concept: {
create: '^conceptualise an? ~ ([a-zA-Z0-9 ]*) ~ ([A-Z0-9]+)(?: that)?',
edit: '^conceptualise the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) (?:has|is|~)',
parseValue: 'has the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) as ~ ([a-zA-Z0-9 ]*) ~',
parseParent: '^is an? ([a-zA-Z0-9 ]*)',
parseRel: '~ ([a-zA-Z0-9 ]*) ~ the ([a-zA-Z0-9 ]*) ([A-Z0-9]+)',
parseSyn: '~ is expressed by ~ ([a-zA-Z0-9 ]*)'
},
instance: {
create: '^there is an? ([a-zA-Z0-9 ]*) named ([a-zA-Z0-9_]+|\'[a-zA-Z0-9_ ]+\')(?: that)?',
edit: 'the ([a-zA-Z0-9_ ]+) ([a-zA-Z0-9_]+|\'[a-zA-Z0-9_ ]+\')',
parseRel: '(?!has)([a-zA-Z0-9 ]*) the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9_\' ]*)',
parseRawVal: '^has ([a-zA-Z0-9]*|\'[^\'\]*(?:\\.[^\'\]*)*\') as ([a-zA-Z0-9 ]*)',
parseInstanceVal: 'has the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9_]*|\'[a-zA-Z0-9_ ]*\') as ([a-zzA-Z0-9 ]*)',
parseInstanceSubConcept: '(?:is| )?an? ([a-zA-Z0-9 ]*)',
parseInstanceSynonym: 'is expressed by (\'[a-zA-Z0-9 ]*\'|[a-zA-Z0-9]*)'
},
and: 'and',
value: 'value'
};

View File

@ -1,3 +0,0 @@
var en = require('./en.js');
module.exports = {en};

View File

@ -18,6 +18,7 @@ module.exports = [
"conceptualise a ~ policy ~ P that is an entity and has the value V as ~ enabled ~ and has the agent A as ~ target ~", "conceptualise a ~ policy ~ P that is an entity and has the value V as ~ enabled ~ and has the agent A as ~ target ~",
"conceptualise a ~ tell policy ~ P that is a policy", "conceptualise a ~ tell policy ~ P that is a policy",
"conceptualise an ~ ask policy ~ P that is a policy", "conceptualise an ~ ask policy ~ P that is a policy",
"conceptualise a ~ gist policy ~ P that is a policy",
"conceptualise a ~ listen policy ~ P that is a policy", "conceptualise a ~ listen policy ~ P that is a policy",
"conceptualise a ~ listen onbehalfof policy ~ P that is a policy", "conceptualise a ~ listen onbehalfof policy ~ P that is a policy",
"conceptualise a ~ forwardall policy ~ P that is a policy and has the timestamp T as ~ start time ~ and has the value V as ~ all agents ~", "conceptualise a ~ forwardall policy ~ P that is a policy and has the timestamp T as ~ start time ~ and has the value V as ~ all agents ~",

View File

@ -1,13 +1,13 @@
{ {
"name": "cenode", "name": "cenode",
"version": "3.0.8", "version": "3.0.12",
"description": "A pure JavaScript implementation of the ITA project's CEStore - called CENode. CENode is able to understand the basic sentence types parsed by the CEStore, such as conceptualising and instance creation and modification.", "description": "A pure JavaScript implementation of the ITA project's CEStore - called CENode. CENode is able to understand the basic sentence types parsed by the CEStore, such as conceptualising and instance creation and modification.",
"homepage": "http://cenode.io", "homepage": "http://cenode.io",
"license": "Apache-2.0", "license": "Apache-2.0",
"author": "Will Webberley & Alun Preece", "author": "Will Webberley & Alun Preece",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/flyingsparx/CENode" "url": "https://github.com/willwebberley/CENode"
}, },
"files": [ "files": [
"src", "src",

View File

@ -16,7 +16,6 @@
*/ */
'use strict'; 'use strict';
const LanguageManager = require('./LanguageManager.js');
const CEAgent = require('./CEAgent.js'); const CEAgent = require('./CEAgent.js');
const CEParser = require('./CEParser.js'); const CEParser = require('./CEParser.js');
const QuestionParser = require('./QuestionParser.js'); const QuestionParser = require('./QuestionParser.js');
@ -119,7 +118,6 @@ class CENode {
const descendants = concept.descendants.concat(concept); const descendants = concept.descendants.concat(concept);
const childrenIds = []; const childrenIds = [];
for (const descendant of descendants) { childrenIds.push(descendant.id); } for (const descendant of descendants) { childrenIds.push(descendant.id); }
for (const ancestor of concept.ancestors) { childrenIds.push(ancestor.id); }
for (const instance of this.instances) { for (const instance of this.instances) {
if (instance && childrenIds.indexOf(instance.concept.id) > -1) { if (instance && childrenIds.indexOf(instance.concept.id) > -1) {
instanceList.push(instance); instanceList.push(instance);
@ -139,12 +137,12 @@ class CENode {
*/ */
addSentence(sentence, source) { addSentence(sentence, source) {
const ceResult = this.addCE(sentence, false, source); const ceResult = this.addCE(sentence, false, source);
if (ceResult.success) { if (!ceResult.error) {
return ceResult; return ceResult;
} }
const questionResult = this.askQuestion(sentence); const questionResult = this.askQuestion(sentence);
if (questionResult.success) { if (!questionResult.error) {
return questionResult; return questionResult;
} }
@ -172,13 +170,7 @@ class CENode {
* Returns: {success: bool, type: str, data: str} * Returns: {success: bool, type: str, data: str}
*/ */
addCE(sentence, source) { addCE(sentence, source) {
const success = this.ceParser.parse(sentence.trim().replace('{now}', new Date().getTime()).replace('{uid}', this.newCardId()), source); return this.ceParser.parse(sentence.trim().replace('{now}', new Date().getTime()).replace('{uid}', this.newCardId()), source);
return {
success: success[0],
type: 'gist',
data: success[1],
result: success[2] || undefined,
};
} }
/* /*
@ -188,12 +180,7 @@ class CENode {
* Returns: {success: bool, type: str, data: str} * Returns: {success: bool, type: str, data: str}
*/ */
askQuestion(sentence) { askQuestion(sentence) {
const success = this.questionParser.parse(sentence); return this.questionParser.parse(sentence);
return {
success: success[0],
type: success[0] ? 'gist' : undefined,
data: success[0] ? success[1] : undefined,
};
} }
/* /*
@ -203,11 +190,7 @@ class CENode {
* Returns: {type: str, data: str} * Returns: {type: str, data: str}
*/ */
addNL(sentence) { addNL(sentence) {
const success = this.nlParser.parse(sentence); return this.nlParser.parse(sentence);
return {
type: success[0] ? 'confirm' : 'gist',
data: success[1],
};
} }
/* /*
@ -247,7 +230,6 @@ class CENode {
* sentence sets to be processed. * sentence sets to be processed.
*/ */
constructor(...models) { constructor(...models) {
this.languageManager = new LanguageManager(this);
this.ceParser = new CEParser(this); this.ceParser = new CEParser(this);
this.questionParser = new QuestionParser(this); this.questionParser = new QuestionParser(this);
this.nlParser = new NLParser(this); this.nlParser = new NLParser(this);

View File

@ -18,7 +18,6 @@
const CEConcept = require('./CEConcept.js'); const CEConcept = require('./CEConcept.js');
const CEInstance = require('./CEInstance.js'); const CEInstance = require('./CEInstance.js');
const en = require('../langs/en.js');
const quotes = { const quotes = {
escape(string) { escape(string) {
@ -29,17 +28,16 @@ const quotes = {
}, },
}; };
const newConcept = new RegExp(en.concept.create, 'i');
const editConcept = new RegExp(en.concept.edit);
const newInstance = new RegExp(en.instance.create);
const editInstance = new RegExp(en.instance.edit);
const andRegex = new RegExp('\\b' + en.and + '\\b', 'gi');
const and = en.and;
const value = en.value;
class CEParser { class CEParser {
static error(message, concerns) {
return { error: true, response: { message, type: 'gist', concerns } };
}
static success(message, concerns) {
return { error: false, response: { message, type: 'gist', concerns } };
}
/* /*
* Submit CE to be processed by node. * Submit CE to be processed by node.
* This may result in * This may result in
@ -52,85 +50,85 @@ class CEParser {
* Returns: [bool, str] (bool = success, str = error or parsed string) * Returns: [bool, str] (bool = success, str = error or parsed string)
*/ */
parse(input, source) { parse(input, source) {
const t = input.replace(/\s+/g, ' ').replace(/\.+$/, '').trim(); // Whitespace -> single space try {
// Whitespace -> single space:
if (newConcept.test(t)){ const t = input.replace(/\s+/g, ' ').replace(/\.+$/, '').trim();
if (t.match(/^conceptualise an?/i)) {
return this.newConcept(t, source); return this.newConcept(t, source);
} else if (editConcept.test(t)) { } else if (t.match(/^conceptualise the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) (?:has|is|~)/i)) {
return this.modifyConcept(t, source); return this.modifyConcept(t, source);
} else if (newInstance.test(t)) { } else if (t.match(/^there is an? ([a-zA-Z0-9 ]*) named/i)) {
return this.newInstance(t, source); return this.newInstance(t, source);
} else if (editInstance.test(t)) { } else if (t.match(/^the ([a-zA-Z0-9 ]*)/i)) {
return this.modifyInstance(t, source); return this.modifyInstance(t, source);
} }
return [false, null]; return CEParser.error('Input not a valid CE sentence.');
} catch (err) {
return CEParser.error(`There was a problem parsing the CE. ${err}.`);
}
} }
newConcept(t, source) { newConcept(t, source) {
const match = newConcept.exec(t); const match = t.match(/^conceptualise an? ~ ([a-zA-Z0-9 ]*) ~ ([A-Z0-9]+)/i);
const conceptName = match[1]; const conceptName = match[1];
const storedConcept = this.node.getConceptByName(conceptName); const storedConcept = this.node.getConceptByName(conceptName);
let concept = null; let concept = null;
if (storedConcept) { if (storedConcept) {
return [false, 'This concept already exists.']; return CEParser.error('This concept already exists');
} }
concept = new CEConcept(this.node, conceptName, source); concept = new CEConcept(this.node, conceptName, source);
const remainder = t.replace(newConcept, ''); const remainder = t.replace(/^conceptualise an? ~ ([a-zA-Z0-9 ]*) ~ ([A-Z0-9]+) that/, '');
const facts = remainder.replace(andRegex, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g); const facts = remainder.replace(/\band\b/g, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g);
if (facts){
for (const fact of facts) { for (const fact of facts) {
this.processConceptFact(concept, fact, source); this.processConceptFact(concept, fact, source);
} }
} return CEParser.success(t, concept);
return [true, t, concept];
} }
modifyConcept(t, source) { modifyConcept(t, source) {
const conceptInfo = editConcept.exec(t); const conceptInfo = t.match(/^conceptualise the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) (?:has|is|~)/);
if (!conceptInfo) { if (!conceptInfo) {
return [false, 'Unable to parse sentence']; return CEParser.error('Unable to parse sentence');
} }
const conceptName = conceptInfo[1]; const conceptName = conceptInfo[1];
const conceptVar = conceptInfo[2]; const conceptVar = conceptInfo[2];
const concept = this.node.getConceptByName(conceptName); const concept = this.node.getConceptByName(conceptName);
if (!concept) { if (!concept) {
return [false, `Concept ${conceptInfo[1]} not known.`]; return CEParser.error(`Concept ${conceptInfo[1]} not known.`);
} }
const remainderRegex = new RegExp(`^conceptualise the ${conceptName} ${conceptVar}`, 'i'); const remainderRegex = new RegExp(`^conceptualise the ${conceptName} ${conceptVar}`, 'i');
const remainder = t.replace(remainderRegex, ''); const remainder = t.replace(remainderRegex, '');
const facts = remainder.replace(andRegex, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g); const facts = remainder.replace(/\band\b/g, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g);
for (const fact of facts) { for (const fact of facts) {
this.processConceptFact(concept, fact, source); this.processConceptFact(concept, fact, source);
} }
return [true, t, concept]; return CEParser.success(t, concept);
} }
processConceptFact(concept, fact, source) { processConceptFact(concept, fact, source) {
const parseVal = new RegExp(en.concept.parseValue); const input = fact.trim().replace(/\+/g, 'and');
const parsePar = new RegExp(en.concept.parseParent); if (input.match(/has the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) as ~ ([a-zA-Z0-9 ]*) ~/g)) {
const parseRel = new RegExp(en.concept.parseRel); const re = /has the ([a-zA-Z0-9 ]*) ([A-Z0-9]+) as ~ ([a-zA-Z0-9 ]*) ~/g;
const parseSyn = new RegExp(en.concept.parseSyn); const match = re.exec(input);
const input = fact.trim().replace(/\+/g, and);
if (parseVal.test(input)){
const match = parseVal.exec(input);
const valConceptName = match[1]; const valConceptName = match[1];
const label = match[3]; const label = match[3];
const valConcept = valConceptName === value ? 0 : this.node.getConceptByName(valConceptName); const valConcept = valConceptName === 'value' ? 0 : this.node.getConceptByName(valConceptName);
concept.addValue(label, valConcept, source); concept.addValue(label, valConcept, source);
} }
if (parsePar.test(input)){ if (input.match(/^is an? ([a-zA-Z0-9 ]*)/)) {
const match = parsePar.exec(input); const re = /^is an? ([a-zA-Z0-9 ]*)/;
const match = re.exec(input);
const parentConceptName = match[1]; const parentConceptName = match[1];
const parentConcept = this.node.getConceptByName(parentConceptName); const parentConcept = this.node.getConceptByName(parentConceptName);
if (parentConcept) { if (parentConcept) {
concept.addParent(parentConcept); concept.addParent(parentConcept);
} }
} }
if (parseRel.test(input)){ if (input.match(/~ ([a-zA-Z0-9 ]*) ~ the ([a-zA-Z0-9 ]*) ([A-Z0-9]+)/)) {
const match = parseRel.exec(input); const re = /~ ([a-zA-Z0-9 ]*) ~ the ([a-zA-Z0-9 ]*) ([A-Z0-9]+)/;
const match = re.exec(input);
const label = match[1]; const label = match[1];
const relConceptName = match[2]; const relConceptName = match[2];
const relConcept = this.node.getConceptByName(relConceptName); const relConcept = this.node.getConceptByName(relConceptName);
@ -138,77 +136,71 @@ class CEParser {
concept.addRelationship(label, relConcept, source); concept.addRelationship(label, relConcept, source);
} }
} }
if (parseSyn.test(input)){ if (input.match(/~ is expressed by ~ ([a-zA-Z0-9 ]*)/)) {
const match = parseSyn.exec(input); const re = /~ is expressed by ~ ([a-zA-Z0-9 ]*)/;
const match = re.exec(input);
const synonym = match[1]; const synonym = match[1];
concept.addSynonym(synonym); concept.addSynonym(synonym);
} }
} }
newInstance(t, source) { newInstance(t, source) {
const names = newInstance.exec(t) let names = t.match(/^there is an? ([a-zA-Z0-9 ]*) named '([^'\\]*(?:\\.[^'\\]*)*)'/i);
if (!names) {
names = t.match(/^there is an? ([a-zA-Z0-9 ]*) named ([a-zA-Z0-9_]*)/i);
if (!names) { return CEParser.error('Unable to determine name of instance.'); }
}
const conceptName = names[1]; const conceptName = names[1];
const instanceName = names[2].replace(/\\/g, '').replace(/'/g, ''); const instanceName = names[2].replace(/\\/g, '');
const concept = this.node.getConceptByName(conceptName); const concept = this.node.getConceptByName(conceptName);
const currentInstance = this.node.getInstanceByName(instanceName, concept); const currentInstance = this.node.getInstanceByName(instanceName, concept);
if (!concept) { if (!concept) {
return [false, `Instance type unknown: ${conceptName}`]; return CEParser.error(`Instance type unknown: ${conceptName}`);
} }
if (currentInstance && currentInstance.type.id === concept.id) { if (currentInstance && currentInstance.type.id === concept.id) {
return [false, 'There is already an instance of this type with this name.', currentInstance]; return CEParser.error('There is already an instance of this type with this name.', currentInstance);
} }
const instance = new CEInstance(this.node, concept, instanceName, source); const instance = new CEInstance(this.node, concept, instanceName, source);
instance.sentences.push(t); instance.sentences.push(t);
const remainder = t.replace(newInstance, ''); const remainder = t.replace(/^there is an? (?:[a-zA-Z0-9 ]*) named (?:[a-zA-Z0-9_]*|'[a-zA-Z0-9_ ]*') that/, '');
const facts = remainder.replace(andRegex, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g); const facts = remainder.replace(/\band\b/g, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g);
if (facts){
for (const fact of facts) { for (const fact of facts) {
this.processInstanceFact(instance, fact, source); this.processInstanceFact(instance, fact, source);
} }
} return CEParser.success(t, instance);
return [true, t, instance];
} }
modifyInstance(t, source) { modifyInstance(t, source) {
let concept; let concept;
let instance; let instance;
const names = editInstance.exec(t); let instanceName;
if (t.match(/^the ([a-zA-Z0-9 ]*)/i)) {
concept = this.node.getConceptByName(names[1]); const names = t.match(/^the ([a-zA-Z0-9 ]*)/i);
if (concept){
instance = this.node.getInstanceByName(names[2].replace(/\\/g, '').replace(/'/g, ''));
}
else {
const nameTokens = names[1].split(' '); const nameTokens = names[1].split(' ');
let currentName = ''; for (const conceptCheck of this.node.concepts) {
for (const index in nameTokens){ if (names[1].toLowerCase().indexOf(conceptCheck.name.toLowerCase()) === 0) {
currentName += ' ' + nameTokens[index]; concept = conceptCheck;
concept = this.node.getConceptByName(currentName.trim()); instanceName = nameTokens[concept.name.split(' ').length];
if (concept){ instance = this.node.getInstanceByName(instanceName, concept);
break; break;
} }
} }
if (concept){ }
const possibleInstances = this.node.getInstances(concept.name, true); if (!instance && t.match(/^the ([a-zA-Z0-9 ]*) '([^'\\]*(?:\\.[^'\\]*)*)'/i)) {
let lowestIndex = null; const names = t.match(/^the ([a-zA-Z0-9 ]*) '([^'\\]*(?:\\.[^'\\]*)*)'/i);
for (const potential of possibleInstances){ if (names) {
const check = new RegExp('\\b(' + potential.name + (potential.synonyms.length ? '|' + potential.synonyms.join('|') : '') + ')\\b', 'i'); concept = this.node.getConceptByName(names[1]);
const match = check.exec(t); instanceName = names[2].replace(/\\/g, '');
if (match && (lowestIndex === null || match.index < lowestIndex)){ instance = this.node.getInstanceByName(instanceName, concept);
lowestIndex = match.index;
instance = potential;
} }
} }
}
}
if (!concept || !instance) { if (!concept || !instance) {
return [false, `Unknown concept/instance combination in: ${t}`]; return CEParser.error(`Unknown concept/instance combination in: ${t}`);
} }
instance.sentences.push(t); instance.sentences.push(t);
const tokens = t.split(' '); const tokens = t.split(' ');
tokens.splice(0, 1 + concept.name.split(' ').length + instance.name.split(' ').length); tokens.splice(0, 1 + concept.name.split(' ').length + instanceName.split(' ').length);
const remainder = tokens.join(' '); const remainder = tokens.join(' ');
const facts = remainder.replace(/\band\b/g, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g); const facts = remainder.replace(/\band\b/g, '+').match(/(?:'(?:\\.|[^'])*'|[^+])+/g);
if (facts) { if (facts) {
@ -216,19 +208,14 @@ class CEParser {
this.processInstanceFact(instance, fact, source); this.processInstanceFact(instance, fact, source);
} }
} }
return [true, t, instance]; return CEParser.success(t, instance);
} }
processInstanceFact(instance, fact, source) { processInstanceFact(instance, fact, source) {
const input = fact.trim().replace(/\+/g, 'and'); const input = fact.trim().replace(/\+/g, 'and');
const parseRel = new RegExp(en.instance.parseRel); if (input.match(/^(?!has)([a-zA-Z0-9 ]*) the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9_' ]*)/)) {
const parseRawVal = new RegExp(en.instance.parseRawVal); const re = /^(?!has)([a-zA-Z0-9 ]*) the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9_' ]*)/;
const parseInstanceVal = new RegExp(en.instance.parseInstanceVal); const match = re.exec(input);
const parseInstanceSubConcept = new RegExp(en.instance.parseInstanceSubConcept);
const parseInstanceSynonym = new RegExp(en.instance.parseInstanceSynonym);
if (parseRel.test(input)){
const match = parseRel.exec(input);
const label = match[1]; const label = match[1];
const relConceptName = match[2]; const relConceptName = match[2];
const relInstanceName = match[3].replace(/'/g, ''); const relInstanceName = match[3].replace(/'/g, '');
@ -242,14 +229,16 @@ class CEParser {
instance.addRelationship(label, relInstance, true, source); instance.addRelationship(label, relInstance, true, source);
} }
} }
if (parseRawVal.test(input)){ if (input.match(/^has ([a-zA-Z0-9]*|'[^'\\]*(?:\\.[^'\\]*)*') as ([a-zA-Z0-9 ]*)/)) {
const match = parseRawVal.exec(input); const re = /^has ([a-zA-Z0-9]*|'[^'\\]*(?:\\.[^'\\]*)*') as ([a-zA-Z0-9 ]*)/;
const match = re.exec(input);
const value = quotes.unescape(match[1]); const value = quotes.unescape(match[1]);
const label = match[2]; const label = match[2];
instance.addValue(label, value, true, source); instance.addValue(label, value, true, source);
} }
if (parseInstanceVal.test(input)){ if (input.match(/^has the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9_]*|'[a-zA-Z0-9_ ]*') as ([a-zA-Z0-9 ]*)/)) {
const match = parseInstanceVal.exec(input); const re = /^has the ([a-zA-Z0-9 ]*) ([a-zA-Z0-9]*|'[a-zA-Z0-9 ]*') as ([a-zA-Z0-9 ]*)/;
const match = re.exec(input);
const valConceptName = match[1]; const valConceptName = match[1];
const valInstanceName = match[2].replace(/'/g, ''); const valInstanceName = match[2].replace(/'/g, '');
const label = match[3]; const label = match[3];
@ -262,12 +251,13 @@ class CEParser {
instance.addValue(label, valInstance, true, source); instance.addValue(label, valInstance, true, source);
} }
} }
if (parseInstanceSubConcept.test(input)){ if (input.match(/(?:is| )?an? ([a-zA-Z0-9 ]*)/g)) {
const match = parseInstanceSubConcept.exec(input); const re = /(?:is| )?an? ([a-zA-Z0-9 ]*)/g;
const match = re.exec(input);
instance.addSubConcept(this.node.getConceptByName(match && match[1] && match[1].trim())); instance.addSubConcept(this.node.getConceptByName(match && match[1] && match[1].trim()));
} }
if (parseInstanceSynonym.test(input)){ if (input.match(/is expressed by ('[a-zA-Z0-9 ]*'|[a-zA-Z0-9]*)/)) {
const match = parseInstanceSynonym.exec(input); const match = input.match(/is expressed by ('[a-zA-Z0-9 ]*'|[a-zA-Z0-9]*)/);
const synonym = match && match[1] && match[1].replace(/'/g, '').trim(); const synonym = match && match[1] && match[1].replace(/'/g, '').trim();
instance.addSynonym(synonym); instance.addSynonym(synonym);
} }

View File

@ -116,14 +116,22 @@ class CEServer {
}, },
'/instance': (request, response) => { '/instance': (request, response) => {
const idRegex = decodeURIComponent(request.url).match(/id=(.*)/); const idRegex = decodeURIComponent(request.url).match(/id=(.*)/);
const id = idRegex ? idRegex[1] : null; const nameRegex = decodeURIComponent(request.url).match(/name=(.*)/);
const instance = this.node.getInstanceById(id); const idQuery = idRegex ? idRegex[1] : null;
const nameQuery = nameRegex ? nameRegex[1] : null;
let instance;
if (idQuery) {
instance = this.node.getInstanceById(idQuery);
} else if (nameQuery) {
instance = this.node.getInstanceByName(nameQuery);
}
if (instance) { if (instance) {
const body = { const body = {
name: instance.name, name: instance.name,
conceptName: instance.concept.name, conceptName: instance.concept.name,
conceptId: instance.concept.id, conceptId: instance.concept.id,
ce: instance.ce, ce: instance.ce,
gist: instance.gist,
synonyms: instance.synonyms, synonyms: instance.synonyms,
subConcepts: [], subConcepts: [],
values: [], values: [],
@ -146,7 +154,7 @@ class CEServer {
return response.end(JSON.stringify(body)); return response.end(JSON.stringify(body));
} }
response.writeHead(404); response.writeHead(404);
return response.end('Concept not found'); return response.end('Unable to find the instance.');
}, },
'/info': (request, response) => { '/info': (request, response) => {
const body = { recentInstances: [], recentConcepts: [], instanceCount: this.node.instances.length, conceptCount: this.node.concepts.length }; const body = { recentInstances: [], recentConcepts: [], instanceCount: this.node.instances.length, conceptCount: this.node.concepts.length };
@ -246,7 +254,12 @@ class CEServer {
if (request.method in this.handlers) { if (request.method in this.handlers) {
const path = request.url.indexOf('?') > 1 ? request.url.slice(0, request.url.indexOf('?')) : request.url; const path = request.url.indexOf('?') > 1 ? request.url.slice(0, request.url.indexOf('?')) : request.url;
if (path in this.handlers[request.method]) { if (path in this.handlers[request.method]) {
try {
this.handlers[request.method][path](request, response); this.handlers[request.method][path](request, response);
} catch (err) {
response.writeHead(500);
response.end(`500: ${err}.`);
}
} else { } else {
response.writeHead(404); response.writeHead(404);
response.end(`404: Resource not found for method ${request.method}.`); response.end(`404: Resource not found for method ${request.method}.`);

View File

@ -38,9 +38,9 @@ class CardHandler {
// Prepare the response 'tell card' and add this back to the node // Prepare the response 'tell card' and add this back to the node
let urls; let urls;
let c; let c;
if (data.data) { if (data.response.message) {
urls = data.data.match(/(https?:\/\/[a-zA-Z0-9./\-+_&=?!%]*)/gi); urls = data.response.message.match(/(https?:\/\/[a-zA-Z0-9./\-+_&=?!%]*)/gi);
c = `there is a ${data.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.data.replace(/'/g, "\\'")}' as content`; c = `there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.response.message.replace(/'/g, "\\'")}' as content`;
} else { } else {
c = `there is a gist card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has 'Sorry; your question was not understood.' as content`; c = `there is a gist card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has 'Sorry; your question was not understood.' as content`;
} }
@ -63,12 +63,12 @@ class CardHandler {
// Add the CE sentence to the node // Add the CE sentence to the node
const data = this.node.addCE(card.content, card.is_from && card.is_from.name); const data = this.node.addCE(card.content, card.is_from && card.is_from.name);
if (!data.success && card.is_from) { if (data.error && card.is_from) {
// If unsuccessful, write an error back // If unsuccessful, write an error back
return this.node.addSentence(`there is a gist card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has 'Sorry. Your input was not understood.' as content and is in reply to the card '${card.name}'.`); return this.node.addSentence(`there is a gist card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has 'Sorry. Your input was not understood.' as content and is in reply to the card '${card.name}'.`);
} }
if (data.success === true) { if (!data.error) {
// Add sentence to any active tell policy queues // Add sentence to any active tell policy queues
for (const policy of this.node.getInstances('tell policy')) { for (const policy of this.node.getInstances('tell policy')) {
if (policy.enabled === 'true' && policy.target && policy.target.name) { if (policy.enabled === 'true' && policy.target && policy.target.name) {
@ -86,12 +86,12 @@ class CardHandler {
const ack = policy.acknowledgement; const ack = policy.acknowledgement;
if (policy.target.name.toLowerCase() === card.is_from.name.toLowerCase()) { if (policy.target.name.toLowerCase() === card.is_from.name.toLowerCase()) {
let c; let c;
if (ack === 'basic') { c = 'OK.'; } else if (data.type === 'tell') { if (ack === 'basic') { c = 'OK.'; } else if (data.response.type === 'tell') {
c = `OK. I added this to my knowledge base: ${data.data}`; c = `OK. I added this to my knowledge base: ${data.response.message}`;
} else if (data.type === 'ask' || data.type === 'confirm' || data.type === 'gist') { } else if (data.response.type === 'ask' || data.response.type === 'confirm' || data.response.type === 'gist') {
c = data.data; c = data.response.message;
} }
return this.node.addSentence(`there is a ${data.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${c.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`); return this.node.addSentence(`there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${c.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`);
} }
} }
} }
@ -102,17 +102,28 @@ class CardHandler {
'nl card': (card) => { 'nl card': (card) => {
let data = this.node.addCE(card.content, card.is_from && card.is_from.name); let data = this.node.addCE(card.content, card.is_from && card.is_from.name);
// If valid CE, then replicate the nl card as a tell card ('autoconfirm') // If valid CE, then replicate the nl card as a tell card ('autoconfirm')
if (data.success) { if (!data.error) {
return this.node.addSentence(`there is a tell card named 'msg_{uid}' that is from the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and is to the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${card.content.replace(/'/g, "\\'")}' as content.`); return this.node.addSentence(`there is a tell card named 'msg_{uid}' that is from the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and is to the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${card.content.replace(/'/g, "\\'")}' as content.`);
} }
data = this.node.askQuestion(card.content); data = this.node.askQuestion(card.content);
// If question was success replicate as ask card ('autoask') // If question was success replicate as ask card ('autoask')
if (data.success) { if (!data.error) {
return this.node.addSentence(`there is an ask card named 'msg_{uid}' that is from the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and is to the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${card.content.replace(/'/g, "\\'")}' as content.`); return this.node.addSentence(`there is an ask card named 'msg_{uid}' that is from the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and is to the agent '${this.agent.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${card.content.replace(/'/g, "\\'")}' as content.`);
} }
// If question not understood then place the response to the NL card in a new response // If question not understood then place the response to the NL card in a new response
data = this.node.addNL(card.content); data = this.node.addNL(card.content);
return this.node.addSentence(`there is a ${data.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.data.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`); return this.node.addSentence(`there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.response.message.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`);
},
'gist card': (card) => {
// Add sentence to any active gist policy queues
for (const policy of this.node.getInstances('gist policy')) {
if (policy.enabled === 'true' && policy.target && policy.target.name) {
const targetName = policy.target.name;
if (!(targetName in this.agent.policyHandler.unsentGistCards)) { this.agent.policyHandler.unsentGistCards[targetName] = []; }
this.agent.policyHandler.unsentGistCards[targetName].push(card);
}
}
}, },
}; };
} }

View File

@ -1,89 +0,0 @@
/*
* Copyright 2017 W.M. Webberley & A.D. Preece (Cardiff University)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
const langs = require('../langs');
const placeholders = {
conceptName: '([a-zA-Z0-9 ]*)',
conceptVar: '([A-Z0-9]+)',
instanceName: '([a-zA-Z0-9_]+|\'[a-zA-Z0-9_ ]+\')',
relationshipLabel: '([a-zA-Z0-9 ]*)'
};
class LanguageManager {
static getEntry(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1');
s = s.replace(/^\./, '');
const a = s.split('.');
for (let i = 0, n = a.length; i < n; ++i) {
const k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
getExpression(key) {
let pattern = LanguageManager.getEntry(this.lang, key);
const extractions = {};
if (pattern){
console.log(pattern)
for (const placeholder in placeholders){
if (pattern.indexOf(placeholder) > -1){
const re = new RegExp('<' + placeholder + '>');
pattern = pattern.replace(re, placeholders[placeholder]);
}
}
}
console.log(pattern)
return pattern;
}
is(key, string){
const re = new RegExp(this.getExpression(key), 'i');
return re.test(string);
}
parse(key, string){
const re = new RegExp(this.getExpression(key), 'i');
return re.exec(string);
}
extract(key, string){
}
addLanguage(key, language){
langs[key] = language;
}
setLanguage(key) {
if (key in langs){
this.lang = langs[key];
}
}
constructor(node) {
this.node = node;
this.lang = langs['en'];
}
}
module.exports = LanguageManager;

View File

@ -18,6 +18,14 @@
class NLParser { class NLParser {
static error(message) {
return { error: true, response: { message, type: 'gist' } };
}
static success(message) {
return { error: false, response: { message, type: 'confirm' } };
}
/* /*
* Submit natural language to be processed by node. * Submit natural language to be processed by node.
* This results in * This results in
@ -122,7 +130,7 @@ class NLParser {
} }
} }
if (facts.length > 0) { if (facts.length > 0) {
return [true, ce + facts.join(' and ')]; return NLParser.success(ce + facts.join(' and '));
} }
} }
@ -140,12 +148,12 @@ class NLParser {
} }
} }
if (newInstanceName && newInstanceName.length) { if (newInstanceName && newInstanceName.length) {
return [true, `there is a ${this.node.concepts[i].name} named '${newInstanceName.trim()}'`]; return NLParser.success(`there is a ${this.node.concepts[i].name} named '${newInstanceName.trim()}'`);
} }
return [true, `there is a ${this.node.concepts[i].name} named '${this.node.concepts[i].name} ${this.node.instances.length}${1}'`]; return NLParser.success(`there is a ${this.node.concepts[i].name} named '${this.node.concepts[i].name} ${this.node.instances.length}${1}'`);
} }
} }
return [false, `Un-parseable input: ${t}`]; return NLParser.error(`Un-parseable input: ${t}`);
} }
/* /*

View File

@ -86,6 +86,7 @@ class PolicyHandler {
this.node = agent.node; this.node = agent.node;
this.unsentTellCards = {}; this.unsentTellCards = {};
this.unsentAskCards = {}; this.unsentAskCards = {};
this.unsentGistCards = {};
this.lastSuccessfulRequest = 0; this.lastSuccessfulRequest = 0;
this.handlers = { this.handlers = {
@ -159,6 +160,36 @@ class PolicyHandler {
} }
}, },
'gist policy': (policy) => {
// For each gist policy in place, send all currently-untold cards to each target
// multiple cards to be sent to one target line-separated
if (policy.target && policy.target.name && policy.target.address) {
if (!(policy.target.name in this.unsentGistCards)) {
this.unsentGistCards[policy.target.name] = [];
}
let data = '';
for (const card of this.unsentGistCards[policy.target.name]) {
if (card.is_tos && card.is_from.name.toLowerCase() !== policy.target.name.toLowerCase()) { // Don't send back a card sent from target agent
// Make sure target is not already a recipient
let inCard = false;
for (const to of card.is_tos) {
if (to.id === policy.target.id) { inCard = true; break; }
}
if (!inCard) {
card.addRelationship('is to', policy.target);
}
data += `${card.ce}\n`;
}
}
if (data.length) {
net.makeRequest('POST', policy.target.address, POST_SENTENCES_ENDPOINT, data, () => {
this.lastSuccessfulRequest = new Date().getTime();
this.unsentGistCards[policy.target.name] = [];
});
}
}
},
'listen policy': (policy) => { 'listen policy': (policy) => {
// Make request to target to get cards addressed to THIS agent // Make request to target to get cards addressed to THIS agent
if (policy.target && policy.target.address) { if (policy.target && policy.target.address) {

View File

@ -18,6 +18,14 @@
class QuestionParser { class QuestionParser {
static error(message) {
return { error: true, response: { message, type: 'gist' } };
}
static success(message) {
return { error: false, response: { message, type: 'gist' } };
}
/* /*
* Submit a who/what/where question to be processed by node. * Submit a who/what/where question to be processed by node.
* This may result in * This may result in
@ -28,6 +36,7 @@ class QuestionParser {
* Returns: [bool, str] (bool = success, str = error or response) * Returns: [bool, str] (bool = success, str = error or response)
*/ */
parse(t) { parse(t) {
try {
const input = t.trim(); const input = t.trim();
if (t.match(/^where (is|are)/i)) { if (t.match(/^where (is|are)/i)) {
return this.whereIs(input); return this.whereIs(input);
@ -42,7 +51,10 @@ class QuestionParser {
} else if (t.match(/^list (\ball\b|\binstances\b)/i)) { } else if (t.match(/^list (\ball\b|\binstances\b)/i)) {
return this.listInstances(input); return this.listInstances(input);
} }
return [false, null]; return QuestionParser.error('Input is not a valid question');
} catch (err) {
return QuestionParser.error(`There was a problem with the question. ${err}.`);
}
} }
whereIs(t) { whereIs(t) {
@ -50,8 +62,7 @@ class QuestionParser {
const instance = this.node.getInstanceByName(thing); const instance = this.node.getInstanceByName(thing);
let message; let message;
if (!instance) { if (!instance) {
message = `I don't know what ${thing} is.`; return QuestionParser.success(`I don't know what ${thing} is.`);
return [true, message];
} }
const locatableInstances = this.node.getInstances('location', true); const locatableInstances = this.node.getInstances('location', true);
const locatableIds = []; const locatableIds = [];
@ -80,8 +91,7 @@ class QuestionParser {
} }
} }
if (!placeFound) { if (!placeFound) {
message = `I don't know where ${instance.name} is.`; return QuestionParser.success(`I don't know where ${instance.name} is.`);
return [true, message];
} }
message = instance.name; message = instance.name;
for (const place in places) { for (const place in places) {
@ -93,7 +103,7 @@ class QuestionParser {
message += ' and'; message += ' and';
} }
} }
return [true, `${message.substring(0, message.length - 4)}.`]; return QuestionParser.success(`${message.substring(0, message.length - 4)}.`);
} }
whatIsIn(t) { whatIsIn(t) {
@ -106,7 +116,7 @@ class QuestionParser {
} }
} }
if (!instance) { if (!instance) {
return [true, `${thing} is not an instance of type location.`]; return QuestionParser.success(`${thing} is not an instance of type location.`);
} }
const things = {}; const things = {};
let thingFound = false; let thingFound = false;
@ -133,7 +143,7 @@ class QuestionParser {
} }
} }
if (!thingFound) { if (!thingFound) {
return [true, `I don't know what is located in/on/at the ${instance.type.name} ${instance.name}.`]; return QuestionParser.success(`I don't know what is located in/on/at the ${instance.type.name} ${instance.name}.`);
} }
let message = ''; let message = '';
@ -144,7 +154,7 @@ class QuestionParser {
} }
message += ' and'; message += ' and';
} }
return [true, `${message.substring(0, message.length - 4)}.`]; return QuestionParser.success(`${message.substring(0, message.length - 4)}.`);
} }
whatIs(input) { whatIs(input) {
@ -156,7 +166,7 @@ class QuestionParser {
if (name) { if (name) {
instance = this.node.getInstanceByName(name[2]); instance = this.node.getInstanceByName(name[2]);
if (instance) { if (instance) {
return [true, instance.gist]; return QuestionParser.success(instance.gist);
} }
} }
@ -187,13 +197,14 @@ class QuestionParser {
} }
} }
if (possibilities.length > 0) { if (possibilities.length > 0) {
return [true, `'${name}' ${possibilities.join(' and ')}.`]; return QuestionParser.success(`'${name}' ${possibilities.join(' and ')}.`);
} }
// If nothing found, do fuzzy search // If nothing found, do fuzzy search
const searchReturn = this.fuzzySearch(t); const searchReturn = this.fuzzySearch(t);
let fuzzyGist = 'I know about '; let fuzzyGist = 'I know about ';
let fuzzyFound = false; let fuzzyFound = false;
if (searchReturn) {
for (const key in searchReturn) { for (const key in searchReturn) {
if (searchReturn[key].length > 1) { if (searchReturn[key].length > 1) {
for (let i = 0; i < searchReturn[key].length; i += 1) { for (let i = 0; i < searchReturn[key].length; i += 1) {
@ -217,14 +228,15 @@ class QuestionParser {
} }
fuzzyFound = true; fuzzyFound = true;
} }
}
if (fuzzyFound) { if (fuzzyFound) {
return [true, fuzzyGist]; return QuestionParser.success(fuzzyGist);
} }
return [true, 'I don\'t know who or what that is.']; return QuestionParser.success('I don\'t know who or what that is.');
} }
return [true, concept.gist]; return QuestionParser.success(concept.gist);
} }
return [true, instance.gist]; return QuestionParser.success(instance.gist);
} }
whatDoes(t) { whatDoes(t) {
@ -256,20 +268,21 @@ class QuestionParser {
property = instance.property(fixedPropertyName); property = instance.property(fixedPropertyName);
} }
if (property) { if (property) {
return [true, `${instance.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`]; return QuestionParser.success(`${instance.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`);
} }
return [true, `Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`]; return QuestionParser.success(`Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`);
} }
} catch (err) { } catch (err) {
return [false, 'Sorry - I can\'t work out what you\'re asking.']; return QuestionParser.success('Sorry - I can\'t work out what you\'re asking.');
} }
return null; return QuestionParser.success('Sorry - I can\'t work out what you\'re asking about.');
} }
whatRelationship(t) { whatRelationship(t) {
const data = t.match(/^(\bwho\b|\bwhat\b) ([a-zA-Z0-9_ ]*)/i); const data = t.match(/^(\bwho\b|\bwhat\b) ([a-zA-Z0-9_ ]*)/i);
const body = data[2].replace(/\ban\b/gi, '').replace(/\bthe\b/gi, '').replace(/\ba\b/gi, ''); const body = data[2].replace(/\ban\b/gi, '').replace(/\bthe\b/gi, '').replace(/\ba\b/gi, '');
const tokens = body.split(' '); const tokens = body.split(' ');
const uniqueResponses = new Set([]);
let instance; let instance;
for (let i = 0; i < tokens.length; i += 1) { for (let i = 0; i < tokens.length; i += 1) {
const testString = tokens.slice(tokens.length - (i + 1), tokens.length).join(' ').trim(); const testString = tokens.slice(tokens.length - (i + 1), tokens.length).join(' ').trim();
@ -283,6 +296,7 @@ class QuestionParser {
break; break;
} }
} }
if (instance) { if (instance) {
const propertyName = tokens.splice(0, tokens.length - instance.name.split(' ').length).join(' ').trim(); const propertyName = tokens.splice(0, tokens.length - instance.name.split(' ').length).join(' ').trim();
for (let i = 0; i < this.node.instances.length; i += 1) { for (let i = 0; i < this.node.instances.length; i += 1) {
@ -304,12 +318,16 @@ class QuestionParser {
property = subject.property(fixedPropertyName); property = subject.property(fixedPropertyName);
} }
if (property && property.name === instance.name) { if (property && property.name === instance.name) {
return [true, `${subject.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`]; uniqueResponses.add(`${subject.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`);
}
const responsesArray = Array.from(uniqueResponses);
if (responsesArray.length > 0 && i === this.node.instances.length - 1) {
return QuestionParser.success(responsesArray.join(' '));
} }
} }
return [true, `Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`]; return QuestionParser.success(`Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`);
} }
return null; return QuestionParser.success('Sorry - I don\'t know the instance you\'re referring to.');
} }
listInstances(t) { listInstances(t) {
@ -328,17 +346,16 @@ class QuestionParser {
s = 'All instances:'; s = 'All instances:';
} }
if (ins.length === 0) { if (ins.length === 0) {
return [true, 'I could not find any instances matching your query.']; return QuestionParser.success('I could not find any instances matching your query.');
} }
const names = []; const names = [];
for (let i = 0; i < ins.length; i += 1) { for (let i = 0; i < ins.length; i += 1) {
names.push(ins[i].name); names.push(ins[i].name);
} }
return [true, `${s} ${names.join(', ')}`]; return QuestionParser.success(`${s} ${names.join(', ')}`);
} }
/* /*
*
* Search the knowledge base for an instance name similar to the one asked about. * Search the knowledge base for an instance name similar to the one asked about.
*/ */
fuzzySearch(sentence) { fuzzySearch(sentence) {
@ -348,12 +365,10 @@ class QuestionParser {
let instancesFiltered = []; let instancesFiltered = [];
if (searchFor.indexOf(' ')) { if (searchFor.indexOf(' ')) {
// if theres spaces then split
multipleSearch = searchFor.split(' '); multipleSearch = searchFor.split(' ');
} }
if (multipleSearch) { if (multipleSearch) {
// loop through to create return string
for (let x = 0; x < multipleSearch.length; x += 1) { for (let x = 0; x < multipleSearch.length; x += 1) {
const instancesFilteredTemp = instances.filter((input) => { const instancesFilteredTemp = instances.filter((input) => {
if (input.name.toUpperCase().includes(multipleSearch[x].toUpperCase())) { if (input.name.toUpperCase().includes(multipleSearch[x].toUpperCase())) {
@ -364,7 +379,6 @@ class QuestionParser {
instancesFiltered = instancesFiltered.concat(instancesFilteredTemp); instancesFiltered = instancesFiltered.concat(instancesFilteredTemp);
} }
} else { } else {
// single search term
instancesFiltered = instances.filter((input) => { instancesFiltered = instances.filter((input) => {
if (input.name.toUpperCase().includes(searchFor.toUpperCase())) { if (input.name.toUpperCase().includes(searchFor.toUpperCase())) {
return input; return input;

40
test/QuestionParser.js Normal file
View File

@ -0,0 +1,40 @@
const CENode = require('../src/CENode.js');
const CEModels = require('../models/index.js');
const expect = require('expect.js');
const myName = 'User'
const PLANETS_MODEL = [
"there is a rule named 'r1' that has 'if the planet C ~ orbits ~ the star D then the star D ~ is orbited by ~ the planet C' as instruction.",
"there is a rule named 'r2' that has 'if the planet C ~ is orbited by ~ the moon D then the moon D ~ orbits ~ the planet C' as instruction.",
"conceptualise a ~ celestial body ~ C.",
"conceptualise the celestial body C ~ orbits ~ the celestial body D and ~ is orbited by ~ the celestial body E.",
"conceptualise a ~ planet ~ P that is a celestial body and is an imageable thing.",
"conceptualise a ~ star ~ S that is a celestial body.",
"there is a star named sun.",
"there is a planet named Venus that orbits the star 'sun' and has 'media/Venus.jpg' as image.",
"there is a planet named Mercury that orbits the star 'sun' and has 'media/Mercury.jpg' as image."
]
let node;
describe('CEQuestionParser', function() {
describe('What relation questions', function () {
this.timeout(2050);
before(function() {
node = new CENode(CEModels.core, PLANETS_MODEL);
node.attachAgent();
node.agent.setName('agent1');
});
it('returns the correct number of responses', (done) => {
const message = 'what orbits the sun?';
const askCard = "there is a nl card named '{uid}' that is to the agent 'agent1' and is from the individual '" + myName + "' and has the timestamp '{now}' as timestamp and has '" + message.replace(/'/g, "\\'")+"' as content.";
node.addSentence(askCard);
setTimeout(function() {
const cards = node.concepts.card.allInstances;
const card = cards[cards.length - 1];
expect(card.content).to.equal('Venus orbits the star sun. Mercury orbits the star sun.');
done();
}, 2000);
});
});
});