Skip to content

Commit 25f3560

Browse files
committed
Bug Fix and Code Cleanup
Fixed a bug in the scoring function that added an extra "empty" element to the scoring array, which could cause the script to return the default response under certain conditions
1 parent e50a8c1 commit 25f3560

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

chatbot/core/aiml/find_aiml.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ function make_like_pattern($sentence, $field)
7171

7272
$sql_like_pattern .= " `$field` like '% $last_word%' OR `$field` like '$first_word % $last_word' OR `$field` like '$last_word'";
7373
runDebug(__FILE__, __FUNCTION__, __LINE__, "returning like pattern:\n$sql_like_pattern", 4);
74-
//return
7574
return $sql_like_pattern;
7675
}
7776

@@ -114,38 +113,38 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
114113
//loop through the results array
115114
foreach ($allrows as $all => $subrow)
116115
{
117-
$aiml_pattern = $subrow['pattern'];
116+
// set the score to zero
117+
$tmp_rows[$i]['track_score'] = '';
118118
//get the pattern
119-
$aiml_topic = $subrow['topic'];
119+
$aiml_pattern = $subrow['pattern'];
120120
//get the topic
121-
$aiml_thatpattern = $subrow['thatpattern'];
121+
$aiml_topic = $subrow['topic'];
122122
//get the that
123-
//$aiml_pattern = (IS_MB_ENABLED) ? mb_strtolower($aiml_pattern) : strtolower($aiml_pattern);
124-
//$default_pattern = (IS_MB_ENABLED) ? mb_strtolower($default_pattern) : strtolower($default_pattern);
123+
$aiml_thatpattern = $subrow['thatpattern'];
124+
//if it is a direct match with our default pattern then add to tmp_rows
125125
if ($aiml_pattern == $default_pattern)
126126
{
127-
//if it is a direct match with our default pattern then add to tmp_rows
128127
$tmp_rows[$i] = $subrow;
129128
$tmp_rows[$i]['score'] = 0;
130-
$tmp_rows[$i]['track_score'] = "za";
129+
$tmp_rows[$i]['track_score'] .= "za";
131130
$i++;
132131
}
132+
//build an aiml_pattern with wild cards to check for a match
133133
else
134134
{
135135
$aiml_pattern_matchme = match_wildcard_rows($aiml_pattern);
136-
//build an aiml_pattern with wild cards to check for a match
137136
if ($aiml_thatpattern != '')
138137
{
139-
$aiml_thatpattern_matchme = match_wildcard_rows($aiml_thatpattern);
140138
//build an aiml_thatpattern with wild cards to check for a match
139+
$aiml_thatpattern_matchme = match_wildcard_rows($aiml_thatpattern);
141140
$that_match = preg_match($aiml_thatpattern_matchme, $current_thatpattern, $matches);
142141
//see if that patterns match
143-
$tmp_rows[$i]['track_score'] = "b";
142+
$tmp_rows[$i]['track_score'] .= "b";
144143
}
145144
else
146145
{
147146
$that_match = "$aiml_thatpattern == ''";
148-
$tmp_rows[$i]['track_score'] = "c";
147+
$tmp_rows[$i]['track_score'] .= "c";
149148
}
150149
if ($aiml_topic != '')
151150
{
@@ -162,25 +161,24 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
162161

163162
if (count($matches)>0)
164163
{
165-
//echo "<br/>TRUE1";
166164
if ($that_match)
167165
{
168-
//echo "<br/>TRUE2";
169166
if ($topic_match)
170167
{
171168
if ((isset ($subrow['pattern'])) && ($subrow['pattern'] != ''))
172169
{
173-
// echo "<br/>TRUE3";
174170
$tmp_rows[$i] = $subrow;
175171
$tmp_rows[$i]['score'] = 0;
176-
$tmp_rows[$i]['track_score'] = "f";
172+
$tmp_rows[$i]['track_score'] .= "f";
177173
$i++;
178174
}
179175
}
180176
}
181177
}
182178
else
183179
{
180+
continue;
181+
/*
184182
$tmp_rows[$i] = array(
185183
'aiml_id' => -1,
186184
'bot_id' => -1,
@@ -190,7 +188,6 @@ function unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpatte
190188
'score' => 0,
191189
'track_score' => ''
192190
);
193-
/*
194191
*/
195192
}
196193
//echo "<br/>--------------";
@@ -406,11 +403,6 @@ function score_matches($convoArr, $bot_parent_id, $allrows, $lookingfor, $curren
406403
//send off for debugging
407404
sort2DArray("show top scoring aiml matches", $allrows, "score", 1, 10);
408405

409-
/*
410-
echo "<pre>";
411-
print_r($allrows);
412-
echo "</pre>";
413-
*/
414406
runDebug(__FILE__, __FUNCTION__, __LINE__,"Returned array:\n" . print_r($allrows, true), 4);
415407
return $allrows;
416408
//return the scored rows
@@ -473,7 +465,7 @@ function sort2DArray($opName, $thisArr, $sortByItem, $sortAsc = 1, $limit = 10)
473465
//get the limited top results
474466
$outArr = array_slice($resArr, 0, $limit);
475467
//send to debug
476-
runDebug(__FILE__, __FUNCTION__, __LINE__, "$opName " . print_r($outArr, true), 3);
468+
runDebug(__FILE__, __FUNCTION__, __LINE__, "$opName " . print_r($outArr, true), 4);
477469
}
478470

479471
/**
@@ -494,6 +486,7 @@ function get_highest_score_rows($allrows, $lookingfor)
494486
{
495487
if (!isset ($subrow['score']))
496488
{
489+
continue;
497490
}
498491
elseif ($subrow['score'] > $last_high_score)
499492
{

0 commit comments

Comments
 (0)