Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3772632

Browse files
author
benjamin.raethlein
committed
added support for star-tag. fixed issue with wildCardArray: was filled even if no wildcard was part of the pattern.
1 parent 769c2c1 commit 3772632

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed

AIMLInterpreter.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ var findCorrectCategory = function(clientInput, domCategories){
175175
text = resolveSpecialNodes(childNodesOfTemplate);
176176
return text;
177177
}
178+
else if(childNodesOfTemplate[i].name === 'star'){
179+
text = resolveSpecialNodes(childNodesOfTemplate);
180+
return text;
181+
}
178182
else{
179183
//this is the text of template node
180184
//after all special functions (bot, get, set,...) were resolved
@@ -218,6 +222,7 @@ var findCorrectCategory = function(clientInput, domCategories){
218222
else if(innerNodes[i].name === 'sr'){
219223
var result;
220224

225+
//for-loop to go through all loaded AIML files
221226
for(var j = 0; j < domArray.length; j++){
222227
result = findCorrectCategory(lastWildCardValue, domArray[j].children);
223228
//if in one of the dom trees a matching pattern was found, exit this inner loop
@@ -227,6 +232,9 @@ var findCorrectCategory = function(clientInput, domCategories){
227232
}
228233
}
229234
}
235+
else if(innerNodes[i].name === 'star'){
236+
text = text + lastWildCardValue;
237+
}
230238
else{
231239
//normal text (no special tag)
232240
text = text + innerNodes[i].text;
@@ -305,26 +313,27 @@ var getWildCardValue = function(userInput, patternText){
305313
var replaceArray = patternText.split('*');
306314
var wildCardInput = userInput;
307315

308-
//replace the string of the userInput which is fixed by the pattern
309-
for(var i = 0; i < replaceArray.length; i++){
310-
wildCardInput = wildCardInput.replace(replaceArray[i], '|');
311-
}
312-
//split the wildCardInput string by | to differentiate multiple * inputs
313-
//e.g. userInput = WHAT IS THE RELATION BETWEEN TIM AND STRUPPI?
314-
//-> | TIM | STRUPPI
315-
//-> [TIM, STRUPPI]
316-
wildCardInput = wildCardInput.split('|');
317-
//split function can create an array which also includes spaces etc. -> e.g. [TIM, " ", "", STRUPPI, " "]
318-
//we just want the information
319-
320-
var wildCardArrayIndex = 0;
321-
for(var i = 0; i < wildCardInput.length; i++){
322-
if(wildCardInput[i] != '' && wildCardInput[i] != ' ' && wildCardInput != undefined){
323-
wildCardArray[wildCardArrayIndex] = wildCardInput[i];
324-
wildCardArrayIndex++;
325-
326-
if(!wildCardInput[i+1]){
327-
lastWildCardValue = wildCardArray[wildCardArrayIndex-1];
316+
if(replaceArray.length > 1){
317+
//replace the string of the userInput which is fixed by the pattern
318+
for(var i = 0; i < replaceArray.length; i++){
319+
wildCardInput = wildCardInput.replace(replaceArray[i], '|');
320+
}
321+
//split the wildCardInput string by | to differentiate multiple * inputs
322+
//e.g. userInput = WHAT IS THE RELATION BETWEEN TIM AND STRUPPI?
323+
//-> | TIM | STRUPPI
324+
//-> [TIM, STRUPPI]
325+
wildCardInput = wildCardInput.split('|');
326+
//split function can create an array which also includes spaces etc. -> e.g. [TIM, " ", "", STRUPPI, " "]
327+
//we just want the information
328+
var wildCardArrayIndex = 0;
329+
for(var i = 0; i < wildCardInput.length; i++){
330+
if(wildCardInput[i] != '' && wildCardInput[i] != ' ' && wildCardInput != undefined){
331+
wildCardArray[wildCardArrayIndex] = wildCardInput[i];
332+
wildCardArrayIndex++;
333+
334+
if(!wildCardInput[i+1]){
335+
lastWildCardValue = wildCardArray[wildCardArrayIndex-1];
336+
}
328337
}
329338
}
330339
}

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Furthermore, the object has a function called <i>findAnswerInLoadedAIMLFiles(cli
2121
a message and a callback. The callback is called when an answer was found.
2222
The callback of <i>findAnswerInLoadedAIMLFiles</i> should look like this: <i>callback(result, wildCardArray)</i>.
2323
Result is the <i>answer</i> from the AIML file and <i>wildCardArray</i> stores the values of all wildcardInputs passed previously from the client.
24-
<br/><br/>
25-
<b>For example:</b><br/>
24+
<br/>
25+
<b>Example:</b><br/>
2626
<pre><code>
2727
AIMLInterpreter = require('./AIMLInterpreter');
2828
var aimlInterpreter = new AIMLInterpreter({name:'WireInterpreter', age:'42'});
@@ -35,19 +35,13 @@ var callback = function(answer, wildCardArray){
3535
aimlInterpreter.findAnswerInLoadedAIMLFiles('What is your name?', callback);
3636
aimlInterpreter.findAnswerInLoadedAIMLFiles('My name is Ben.', callback);
3737
aimlInterpreter.findAnswerInLoadedAIMLFiles('What is my name?', callback);
38-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Who are you?', callback);
39-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Give me a letter.', callback);
40-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test srai in random.', callback);
41-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test wildcard What is my name', callback);
42-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test sr tag', callback);
43-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test sr in random', callback);
44-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test the wildcard pattern!', callback);
45-
</code></pre>
38+
</code></pre><br/>
4639
<b>Supported AIML tags:</b><pre>
4740
&lt;bot name="<i>NAME</i>"/>
4841
&lt;get name="<i>NAME</i>"/>
4942
&lt;set name="<i>NAME</i>">TEXT&lt;/set>
5043
&lt;random>&lt;li><i>A</i>&lt;/li>&lt;li><i>B</i>&lt;/li>&lt;li><i>C</i>&lt;/li>&lt;/random>
5144
&lt;srai><i>PATTERN TEXT</i>&lt;/srai>
52-
&lt;sr/></pre>
45+
&lt;sr/>
46+
&lt;star/></pre>
5347

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aimlinterpreter",
3-
"version": "0.0.7",
3+
"version": "0.1.0",
44
"description": "Module to interpret AIML files in node.js",
55
"main": "AIMLInterpreter.js",
66
"dependencies": {

test.aiml.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<category><pattern>TEST WILDCARD *</pattern><template>Thanks for testing!</template></category>
1010
<category><pattern>TEST SR TAG</pattern><template><sr/></template></category>
1111
<category><pattern>TEST SR IN RANDOM</pattern><template><random><li><sr/></li></random></template></category>
12-
<category><pattern>*</pattern><template>I found nothing.</template></category>
12+
<category><pattern>TEST THE STAR TAG</pattern><template><star/></template></category>
13+
<category><pattern>*</pattern><template>I found nothing.</template></category>
1314
</aiml>

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ aimlInterpreter.findAnswerInLoadedAIMLFiles('What is my name?', callback);
1313
aimlInterpreter.findAnswerInLoadedAIMLFiles('Who are you?', callback);
1414
aimlInterpreter.findAnswerInLoadedAIMLFiles('Give me a letter.', callback);
1515
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test srai in random.', callback);
16-
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test wildcard What is my name', callback);
16+
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test wildcard What is my name?', callback);
1717
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test sr tag', callback);
1818
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test sr in random', callback);
19+
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test the star tag', callback);
1920
aimlInterpreter.findAnswerInLoadedAIMLFiles('Test the wildcard pattern!', callback);

0 commit comments

Comments
 (0)