@@ -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 );
0 commit comments