Skip to content

Commit f829edf

Browse files
committed
Multiple bug fixes
Fixed several bugs related to parsing the <condition> tag, and re-designed the function that handles unsetting non-relevant AIML "matches".
1 parent d31bbbb commit f829edf

File tree

2 files changed

+157
-34
lines changed

2 files changed

+157
-34
lines changed

chatbot/core/aiml/find_aiml.php

Lines changed: 145 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,134 @@ function wordsCount_inSentence($sentence)
9898
* @return array tmp_rows - the RELEVANT results
9999
**/
100100
function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpattern, $current_topic, $default_pattern)
101+
{
102+
//if default pattern keep
103+
//if wildcard pattern matches found aiml keep
104+
//if wildcard pattern and wildard thatpattern keep
105+
//the end......
106+
107+
runDebug(__FILE__, __FUNCTION__, __LINE__, "NEW FUNC Searching through " . count($allrows) . " rows to unset bad matches", 4);
108+
if (($allrows[0]['pattern'] == "no results") and (count($allrows) == 1)) {
109+
$tmp_rows[0] = $allrows[0];
110+
$tmp_rows[0]['score'] = 1;
111+
runDebug(__FILE__, __FUNCTION__, __LINE__, "Returning error as no results where found", 1);
112+
return $tmp_rows;
113+
}
114+
$i = 0;
115+
$j = 0;
116+
//loop through the results array
117+
foreach ($allrows as $all => $subrow) {
118+
$message[$j]['new turn looking for']="$lookingfor";
119+
$message[$j]['found pattern'] = $subrow['pattern'];
120+
$message[$j]['found thatpattern'] = $subrow['thatpattern'];
121+
$message[$j]['found topic'] = $subrow['topic'];
122+
$message[$j]['checking against']= implode(",",$subrow);
123+
$aiml_pattern = $subrow['pattern'];
124+
$aiml_pattern = (IS_MB_ENABLED) ? mb_strtolower($aiml_pattern) : strtolower($aiml_pattern);
125+
$aiml_pattern_wildcards = match_wildcard_rows($aiml_pattern);
126+
//get the pattern
127+
$aiml_thatpattern = $subrow['thatpattern'];
128+
$aiml_thatpattern = (IS_MB_ENABLED) ? mb_strtolower($aiml_thatpattern) : strtolower($aiml_thatpattern);
129+
preg_match($aiml_pattern_wildcards, $lookingfor, $matches);
130+
131+
$topicMatch =FALSE;
132+
$aiml_topic = trim($subrow['topic']);
133+
$aiml_topic = (IS_MB_ENABLED) ? mb_strtolower($aiml_topic) : strtolower($aiml_topic);
134+
$current_topic_lc = (IS_MB_ENABLED) ? mb_strtolower($current_topic) : strtolower($current_topic);
135+
//runDebug(__FILE__, __FUNCTION__, __LINE__, "TOPICHK '".$aiml_topic."'", 4);
136+
if($aiml_topic==''){
137+
//runDebug(__FILE__, __FUNCTION__, __LINE__, "NO TOPIC this is true", 4);
138+
$topicMatch = TRUE;
139+
}elseif(($aiml_topic == $current_topic_lc)){
140+
$topicMatch = TRUE;
141+
//runDebug(__FILE__, __FUNCTION__, __LINE__, "TOPIC MATCH this is true", 4);
142+
$message[$j]['topic match'] = "Found topic match $aiml_topic and $current_topic_lc";
143+
}else{
144+
$message[$j]['topic match'] = "NO topic match $aiml_topic and $current_topic_lc";
145+
$topicMatch = FALSE;
146+
}
147+
148+
$message[$j]['have made the pattern wildcard to do reg exp'] = $aiml_pattern_wildcards;
149+
150+
if(count($matches)>0){
151+
$aiml_patternmatch=TRUE;
152+
#$message[$j]['found some matches'] = print_r($matches, true);
153+
$message[$j]['found some matches'] = $matches[0];
154+
$message[$j]['using'] ="$aiml_pattern_wildcards REGEXPIN $lookingfor";
155+
}else{
156+
$aiml_patternmatch = FALSE;
157+
}
158+
$message[$j]['found a match'] = $aiml_patternmatch;
159+
160+
$message[$j]['what is the current thatpattern'] = $current_thatpattern;
161+
$message[$j]['do we have a thatpattern'] = $aiml_patternmatch;
162+
if($aiml_thatpattern!=''){
163+
$aiml_thatpattern_wildcards = match_wildcard_rows($aiml_thatpattern);
164+
preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $thatmatches);
165+
if (count($thatmatches)>0) {
166+
$aiml_thatpatternmatch = TRUE;
167+
$message[$j]['there are thatpattern matches'] = "$aiml_thatpattern_wildcards in $current_thatpattern";
168+
$message[$j]['thatpattern matches are'] = print_r($thatmatches, true);
169+
} else {
170+
$aiml_thatpatternmatch = FALSE;
171+
$message[$j]['there arent any thatpattern matches'] = "$aiml_thatpattern_wildcards in $current_thatpattern";
172+
}
173+
174+
175+
176+
177+
} else {
178+
$aiml_thatpattern_wildcards = FALSE;
179+
$message[$j]['no thatpattern'] = $aiml_thatpattern;
180+
}
181+
182+
183+
184+
//if default pattern keep
185+
if (($aiml_pattern == $default_pattern) || (strtolower($aiml_pattern) == strtolower($default_pattern)) || (strtoupper($aiml_pattern) == strtoupper($default_pattern))) {
186+
//if it is a direct match with our default pattern then add to tmp_rows
187+
188+
$tmp_rows[$i]['score'] = 0;
189+
$tmp_rows[$i]['track_score'] = "default pick up line ($aiml_pattern = $default_pattern) ";
190+
} elseif((!$aiml_thatpattern_wildcards)&&($aiml_patternmatch)){ // no thatpattern and a pattern match keep
191+
192+
$tmp_rows[$i]['score'] = 1;
193+
$tmp_rows[$i]['track_score'] = " no thatpattern in result and a pattern match";
194+
} elseif (($aiml_thatpattern_wildcards) && ($aiml_thatpatternmatch) && ($aiml_patternmatch)) { //pattern match and a wildcard match on the thatpattern keep
195+
196+
$tmp_rows[$i]['score'] = 2;
197+
$tmp_rows[$i]['track_score'] = " thatpattern match and a pattern match";
198+
} else {
199+
$tmp_rows[$i]['score'] = -1;
200+
$tmp_rows[$i]['track_score']= "dismissing nothing is matched";
201+
}
202+
203+
if($topicMatch === FALSE){
204+
$tmp_rows[$i]['score'] = -1;
205+
$tmp_rows[$i]['track_score']= "dismissing wrong topic";
206+
}
207+
208+
209+
if($tmp_rows[$i]['score']>=0){
210+
$relevantRow[]=$subrow;
211+
}
212+
213+
$message[$j]['sore']= $tmp_rows[$i]['score'];
214+
$message[$j]['track score'] = $tmp_rows[$i]['track_score'];
215+
$i++;
216+
$j++;
217+
}
218+
219+
//runDebug(__FILE__, __FUNCTION__, __LINE__, print_r($message, true), 4);
220+
sort2DArray("show top scoring aiml matches", $relevantRow, "good matches", 1, 10);
221+
222+
runDebug(__FILE__, __FUNCTION__, __LINE__, "Found ".count($relevantRow)." relevant rows", 4);
223+
runDebug(__FILE__, __FUNCTION__, __LINE__, print_r($relevantRow, true), 4);
224+
return $relevantRow;
225+
226+
}
227+
228+
function unset_all_bad_pattern_matches_old($allrows, $lookingfor, $current_thatpattern, $current_topic, $default_pattern)
101229
{
102230
global $error_response;
103231
$tmp_rows = array();
@@ -117,7 +245,7 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
117245
// set the score to zero
118246
$tmp_rows[$i]['track_score'] = '';
119247
//get the pattern
120-
$aiml_pattern = strtoupper($subrow['pattern']);
248+
$aiml_pattern = (IS_MB_ENABLED) ? mb_strtoupper($subrow['pattern']) : strtoupper($subrow['pattern']);
121249
//get the topic
122250
$aiml_topic = $subrow['topic'];
123251
//get the that
@@ -147,6 +275,7 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
147275
$that_match = ($aiml_thatpattern == '');
148276
$tmp_rows[$i]['track_score'] .= "c";
149277
}
278+
150279
if ($aiml_topic != '')
151280
{
152281
$topic_match = ($aiml_topic == $current_topic);
@@ -162,17 +291,14 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
162291

163292
if (count($matches)>0)
164293
{
165-
if ($that_match)
294+
if ((isset ($subrow['pattern'])) && ($subrow['pattern'] != ''))
166295
{
167-
if ($topic_match)
296+
if (($topic_match) || ($that_match))
168297
{
169-
if ((isset ($subrow['pattern'])) && ($subrow['pattern'] != ''))
170-
{
171298
$tmp_rows[$i] = $subrow;
172299
$tmp_rows[$i]['score'] = 0;
173300
$tmp_rows[$i]['track_score'] .= "f";
174301
$i++;
175-
}
176302
}
177303
}
178304
}
@@ -292,7 +418,7 @@ function score_matches($convoArr, $bot_parent_id, $allrows, $lookingfor, $curren
292418
$allrows[$all]['track_score'] .= "b";
293419
}
294420
//if the result topic matches the user stored aiml topic increase score
295-
if (($aiml_topic == $current_topic) && ($aiml_topic != ''))
421+
if (($aiml_topic == $current_topic) && ($current_topic != ''))
296422
{
297423
$allrows[$all]['score'] += $topic_match;
298424
$allrows[$all]['track_score'] .= "c";
@@ -416,7 +542,7 @@ function sort2DArray($opName, $thisArr, $sortByItem, $sortAsc = 1, $limit = 10)
416542
{
417543
$thisCount = count($thisArr);
418544
$showLimit = ($thisCount < $limit) ? $thisCount : $limit;
419-
runDebug(__FILE__, __FUNCTION__, __LINE__, print_r($thisArr, true), 4);
545+
//runDebug(__FILE__, __FUNCTION__, __LINE__, print_r($thisArr, true), 4);
420546
runDebug(__FILE__, __FUNCTION__, __LINE__, "$opName - sorting $thisCount results by $sortByItem and getting the top $showLimit for debugging.", 4);
421547
$i = 0;
422548
$tmpSortArr = array();
@@ -459,7 +585,7 @@ function sort2DArray($opName, $thisArr, $sortByItem, $sortAsc = 1, $limit = 10)
459585
//get the limited top results
460586
$outArr = array_slice($resArr, 0, $limit);
461587
//send to debug
462-
runDebug(__FILE__, __FUNCTION__, __LINE__, "$opName " . print_r($outArr, true), 4);
588+
runDebug(__FILE__, __FUNCTION__, __LINE__, "$opName " . print_r($resArr, true), 4);
463589
}
464590

465591
/**
@@ -618,7 +744,7 @@ function get_client_property($convoArr, $name)
618744
if ($rowCount != 0)
619745
{
620746
$row = mysql_fetch_assoc($result);
621-
$response = $row['value'];
747+
$response = trim($row['value']);
622748
$convoArr['client_properties'][$name] = $response;
623749
runDebug(__FILE__, __FUNCTION__, __LINE__, "Found client property '$name' in the DB. Adding it to the conversation array and returning '$response'", 2);
624750

@@ -689,19 +815,20 @@ function get_aiml_to_parse($convoArr)
689815
$sendConvoArr = $convoArr;
690816
//check if match in user defined aiml
691817
$allrows = find_userdefined_aiml($convoArr);
692-
//if there is no match in the user defined aiml tbl
818+
//if there is no match in the user defined aiml table
693819
if ((!isset ($allrows)) || (count($allrows) <= 0))
694820
{
695821
//look for a match in the normal aiml tbl
696822
$allrows = find_aiml_matches($convoArr);
823+
#save_file(_DEBUG_PATH_ . 'allrows.txt', print_r($allrows, true));
697824
//unset all irrelvant matches
698825
$allrows = unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpattern, $current_topic, $aiml_pattern);
699826
//score the relevant matches
700827
$allrows = score_matches($convoArr, $bot_parent_id, $allrows, $lookingfor, $current_thatpattern, $current_topic, $aiml_pattern);
701828
//get the highest
702829
$allrows = get_highest_scoring_row($allrows, $lookingfor);
703830
//READY FOR v2.5 do not uncomment will not work
704-
//check if this is an unknown input and place in the unknown_inputs tbl if true
831+
//check if this is an unknown input and place in the unknown_inputs table if true
705832
//check_and_add_unknown_inputs($allrows,$convoArr);
706833
}
707834
//Now we have the results put into the conversation array
@@ -767,8 +894,8 @@ function find_aiml_matches($convoArr)
767894
$lastInputWord = get_last_word($lookingfor);
768895
$firstInputWord = get_first_word($lookingfor);
769896
//get the stored topic
770-
$storedtopic = (isset($convoArr['topic'][1])) ? mysql_real_escape_string($convoArr['topic'][1]) : '';
771-
if ($storedtopic == '') $storedtopic = get_topic($convoArr);
897+
$storedtopic = get_topic($convoArr);
898+
772899
//get the cleaned user input
773900
$lastthat = (isset($convoArr['that'][1][1])) ? $convoArr['that'][1][1] : '';
774901
//build like patterns
@@ -794,17 +921,16 @@ function find_aiml_matches($convoArr)
794921
}
795922
if ($storedtopic != '')
796923
{
797-
$topic_select = "(`topic`='') OR (`topic`='$storedtopic')";
924+
$topic_select = "AND ((`topic`='') OR (`topic`='$storedtopic'))";
798925
}
799-
else $topic_select = "`topic`=''";
926+
else $topic_select = '';
800927
if ($word_count == 1)
801928
{
802929
//if there is one word do this
803930
$sql = "SELECT `id`, `bot_id`, `pattern`, `thatpattern`, `topic` FROM `$dbn`.`aiml` WHERE
804931
$sql_bot_select AND (
805932
((`pattern` = '_') OR (`pattern` = '*') OR (`pattern` = '$lookingfor') OR (`pattern` = '$aiml_pattern' ) )
806-
AND ((`thatpattern` = '_') OR (`thatpattern` = '*') OR (`thatpattern` = '') OR (`thatpattern` = '$lastthat') $thatPatternSQL )
807-
AND ( (`topic`='') OR (`topic`='$storedtopic')))";
933+
$topic_select) order by `topic` desc, `id` desc, `pattern` asc;";
808934
}
809935
else
810936
{
@@ -817,8 +943,7 @@ function find_aiml_matches($convoArr)
817943
(`pattern` like '$lookingfor') OR
818944
($sql_add) OR
819945
(`pattern` = '$aiml_pattern' ))
820-
AND ((`thatpattern` = '_') OR (`thatpattern` = '*') OR (`thatpattern` = '') OR (`thatpattern` like '%') OR (`thatpattern` = '$lastthat') $thatPatternSQL )
821-
AND ($topic_select));";
946+
$topic_select) order by `topic` desc, `id` desc, `pattern` asc;";
822947
}
823948
runDebug(__FILE__, __FUNCTION__, __LINE__, "Match AIML sql: $sql", 3);
824949
$result = db_query($sql, $con);

chatbot/core/aiml/parse_aiml_as_XML.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ function parse_condition_tag($convoArr, $element, $parentName, $level)
488488
if (in_array($choice_name, $exclude)) continue;
489489
$exclude[] = $choice_name;
490490
runDebug(__FILE__, __FUNCTION__, __LINE__, 'Client properties = ' . print_r($convoArr['client_properties'], true), 4);
491-
$choice_value = get_client_property($convoArr, $choice_name);
491+
$choice_value = trim(get_client_property($convoArr, $choice_name));
492492
$condition_xPath .= "li[@name=\"$choice_name\"][@value=\"$choice_value\"]|";
493493
}
494494
$condition_xPath .= 'li[not(@*)]';
@@ -503,18 +503,17 @@ function parse_condition_tag($convoArr, $element, $parentName, $level)
503503
{
504504
runDebug(__FILE__, __FUNCTION__, __LINE__, 'Parsing a CONDITION tag with 2 attributes.', 4);
505505
$condition_name = (string)$element['name'];
506-
$test_value = get_client_property($convoArr, $condition_name);
507-
$test_value = trim($test_value);
506+
$test_value = trim(get_client_property($convoArr, $condition_name));
508507
switch (true)
509508
{
510509
case (isset($element['value'])):
511510
$condition_value = (string)$element['value'];
512511
break;
513-
case (isset($element['value'])):
514-
$condition_value = (string)$element['value'];
512+
case (isset($element['contains'])):
513+
$condition_value = (string)$element['contains'];
515514
break;
516515
case (isset($element['value'])):
517-
$condition_value = (string)$element['value'];
516+
$condition_value = (string)$element['exists'];
518517
break;
519518
default:
520519
runDebug(__FILE__, __FUNCTION__, __LINE__, 'Something went wrong with parsing the CONDITION tag. Returning the error response.', 1);
@@ -526,30 +525,29 @@ function parse_condition_tag($convoArr, $element, $parentName, $level)
526525
{
527526
runDebug(__FILE__, __FUNCTION__, __LINE__, 'Parsing a CONDITION tag with only the NAME attribute', 4);
528527
$condition_name = (string)$element['name'];
529-
$test_value = get_client_property($convoArr, $condition_name);
530-
//$path = "li[@name=*][not(@value)]|li[not(@*)]";
531-
$path = "li[@name]|li[not(@*)]";
532-
//trigger_error("path = $path");
528+
$test_value = trim(get_client_property($convoArr, $condition_name));
529+
runDebug(__FILE__, __FUNCTION__, __LINE__, "Looking for test value '$test_value'", 4);
530+
$path = "li[@value]|li[not(@*)]";
533531
runDebug(__FILE__, __FUNCTION__, __LINE__, "search string = $path", 4);
534532
$choice = $element->xpath($path);
533+
runDebug(__FILE__, __FUNCTION__, __LINE__,'element = ' . print_r($element, true), 4);
535534
runDebug(__FILE__, __FUNCTION__, __LINE__,'Choices = ' . print_r($choice, true), 4);
536535
if (count($choice) != 0)
537536
{
538537
$test_value = rtrim($test_value);
539538
runDebug(__FILE__, __FUNCTION__, __LINE__,'parent XML = ' . $element->asXML(), 4);
540539
foreach ($choice as $pick)
541540
{
542-
$testVarName = (string)$pick['name'];
543-
$testVarValue = get_client_property($convoArr, $testVarName);
544-
$testVarValue = trim($testVarValue);
541+
runDebug(__FILE__, __FUNCTION__, __LINE__,'Pick = ' . print_r($pick, true), 4);
542+
$testVarValue = get_client_property($convoArr, $condition_name);
543+
//$testVarValue = trim($testVarValue);
545544
runDebug(__FILE__, __FUNCTION__, __LINE__,"Checking to see if $testVarValue ($testVarName) is equal to $test_value.", 4);
546545
if (strtolower($testVarValue) == strtolower($test_value))
547546
{
548547
runDebug(__FILE__, __FUNCTION__, __LINE__,'Pick XML = ' . $pick->asXML(), 4);
549548
break;
550549
}
551550
}
552-
//$pick = $choice[0];
553551
runDebug(__FILE__, __FUNCTION__, __LINE__, 'Found a match. Pick = ' . print_r($pick, true), 4);
554552
}
555553
else

0 commit comments

Comments
 (0)