function spellcheck($user_say) { global $dbconn; $wordArr = make_wordArray($user_say); $limit_res = count($wordArr); $sqlwords = glue_words_for_sql($wordArr); $sql = "SELECT * FROM `{$dbn}`.`spellcheck` WHERE `missspelling` IN ({$sqlwords}) LIMIT {$limit_res}"; $result = db_query($sql, $dbconn); if ($result) { if (db_res_count($result) > 0) { while ($row = db_res_array($result)) { $pattern = '/\\b' . $row['missspelling'] . '\\b/'; $replacement = $row['correction']; if ($user_say = preg_replace($pattern, $replacement, $user_say)) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Replacing " . $row['missspelling'] . " with " . $row['correction'], 4); } } } } }
/** * function get_conversation_to_display() * This function gets the conversation from the db to display/return to the user * @param array $convoArr - the conversation array * @return array $orderedRows - a list of conversation line **/ function get_conversation_to_display($convoArr) { global $con, $dbn, $bot_name; $user_id = $convoArr['conversation']['user_id']; $bot_id = $convoArr['conversation']['bot_id']; $sql = "select `name` from `users` where `id` = {$user_id} limit 1;"; $result = db_query($sql, $con); $row = mysql_fetch_assoc($result); $user_name = $row['name']; $user_name = !empty($user_name) ? $user_name : 'User'; $convoArr['conversation']['user_name'] = $user_name; $convoArr['conversation']['bot_name'] = $bot_name; if (empty($bot_name)) { $sql = "select `bot_name` from `bots` where `bot_id` = {$bot_id} limit 1;"; $result = db_query($sql, $con); $row = mysql_fetch_assoc($result); $bot_name = $row['bot_name']; } if ($convoArr['conversation']['conversation_lines'] != 0) { $limit = " LIMIT " . $convoArr['conversation']['conversation_lines']; } else { $limit = ""; } $sql = "SELECT * FROM `{$dbn}`.`conversation_log`\n\t\tWHERE \n\t\t`userid` = '" . $convoArr['conversation']['user_id'] . "'\n\t\tAND `bot_id` = '" . $convoArr['conversation']['bot_id'] . "'\n\t\tORDER BY id DESC {$limit} "; #$x = save_file('conversationLogSQL.txt', "SQL = \r\n$sql"); runDebug(__FILE__, __FUNCTION__, __LINE__, "get_conversation SQL: {$sql}", 3); $result = db_query($sql, $con); if (db_res_count($result) > 0) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $allrows[] = $row; } $orderedRows = array_reverse($allrows, false); } else { $orderedRows = array('id' => NULL, 'input' => "", 'response' => "", 'userid' => $convoArr['conversation']['user_id'], 'bot_id' => $convoArr['conversation']['bot_id'], 'timestamp' => ""); } runDebug(__FILE__, __FUNCTION__, __LINE__, "Found '" . db_res_count($result) . "' lines of conversation", 2); return $orderedRows; }
/** * function check_set_bot(() * A function to check and set the bot id * @param array $convoArr - the current state of the conversation array * @return $convoArr (updated) **/ function check_set_bot($convoArr) { global $con, $dbn, $default_bot_id, $error_response; //check to see if bot_id has been passed if not load default if (isset($_REQUEST['bot_id']) && trim($_REQUEST['bot_id']) != "") { $bot_id = trim($_REQUEST['bot_id']); } elseif (isset($convoArr['conversation']['bot_id'])) { $bot_id = $convoArr['conversation']['bot_id']; } else { $bot_id = $default_bot_id; } //get the values from the db $sql = "SELECT * FROM `{$dbn}`.`bots` WHERE bot_id = '{$bot_id}' and `bot_active`='1'"; runDebug(__FILE__, __FUNCTION__, __LINE__, "Checking bot exists SQL: {$sql}", 3); $result = db_query($sql, $con); if ($result && db_res_count($result) > 0) { $row = mysql_fetch_assoc($result); $bot_name = $row['bot_name']; $error_response = $row['error_response']; $convoArr['conversation']['bot_name'] = $bot_name; $convoArr['conversation']['bot_id'] = $bot_id; runDebug(__FILE__, __FUNCTION__, __LINE__, "BOT ID: {$bot_id}", 2); } else { $convoArr['debug']['intialisation_error'] = "Bot ID: {$bot_id} does not exist"; $convoArr['conversation']['bot_id'] = $bot_id; runDebug(__FILE__, __FUNCTION__, __LINE__, "ERROR - Cannot find bot id: {$bot_id}", 1); } return $convoArr; }