/ AIML

修改program-o使之支持中文(四)

接下来要修改的是后台管理部分,主要要修改的地方是插入或者更新数据库时对中文进行处理。

所有要修改的文件都在admin\funcs目录下。

打开config.php,在最后添加两个函数

//----------------------------------------------------------- //formatchinese($str) add a space into each Chinese character //----------------------------------------------------------- function formatchinese($str){ if(preg_match_all("/[\x{4e00}-\x{9fa5}]{1}/u",$str,$out)){ $tmp=$str; foreach($out[0] as $value){ $str = preg_replace("/".$value."/",$value." ",$str); $str = preg_replace("/\s+/"," ",$str); } } return trim(str_replace(""," ",$str)); } //----------------------------------------------------------------- //restorechinese($str) remove the space after each Chinse character //----------------------------------------------------------------- function restorechinese($str){ if(preg_match_all("/[\x{4e00}-\x{9fa5}]{1} /u",$str,$out)){ foreach($out[0] as $value){ $str = preg_replace("/$value/",trim($value),$str); } } return trim($str); }

打开teach.php,找到如下代码

$template = mysql_escape_string(trim($_POST['template'])); $pattern = strtoupper(mysql_escape_string(trim($_POST['pattern']))); $thatpattern = strtoupper(mysql_escape_string(trim($_POST['thatpattern']))); $topic = strtoupper(mysql_escape_string(trim($_POST['topic'])));

替换

$template = mysql_escape_string(trim(formatchinese($_POST['template']))); $pattern = strtoupper(mysql_escape_string(trim(formatchinese($_POST['pattern'])))); $thatpattern = strtoupper(mysql_escape_string(trim(formatchinese($_POST['thatpattern'])))); $topic = strtoupper(mysql_escape_string(formatchinese(trim($_POST['topic']))));

搜索

$sql = "INSERT INTO $dbn.aiml (id,pattern,thatpattern,template,topic,filename) VALUES (NULL,'$pattern','$thatpattern','$template','$topic','ADMIN ADDED')";

替换

$sql = "INSERT INTO $dbn.aiml (id,aiml,pattern,thatpattern,template,topic,filename) VALUES (NULL,'','$pattern','$thatpattern','$template','$topic','ADMIN ADDED')";

teach功能就修改完毕了,可以在后台测试一下。

User Input填“你是谁”,Bot Response填“我是机器人”,点击“teach”,不出意外会提示AIML added。

然后到前台,输入“你是谁”,应该就能得到正确的回复。

打开upload.php,找到

$insert_sql .= "(null, '" . $aiml_sql . "', '" . $pattern_sql . "', '" . $that_sql . "', '" . $template_sql . "', '" . $topic_sql . "', '" . $file . "')";

替换

$insert_sql .= "(null, '" . $aiml_sql . "', '" . formatchinese($pattern_sql) . "', '" . formatchinese($that_sql) . "', '" . formatchinese($template_sql) . "', '" . formatchinese($topic_sql) . "', '" . $file . "')";

找到

$pattern_sql = strtoupper($text);

替换

$pattern_sql .= strtoupper($text);

修改完毕。

最后打开search.php,搜索

$search = mysql_escape_string(trim($_POST['search']));

替换

$search = mysql_escape_string(trim(formatchinese($_POST['search'])));

找到

$topic = $row['topic']; $pattern = $row['pattern']; $thatpattern = $row['thatpattern']; $template = htmlentities($row['template']);

总共有两处,都替换

$topic = restorechinese($row['topic']); $pattern = restorechinese($row['pattern']); $thatpattern = restorechinese($row['thatpattern']); $template = restorechinese(htmlentities($row['template'],ENT_QUOTES,"UTF-8"));

找到

$htmltbl .= "";

在下面添加一行

$search = restorechinese($search);

找到

$template = mysql_escape_string(trim($_POST['template'])); $filename = mysql_escape_string(trim($_POST['filename'])); $pattern = strtoupper(mysql_escape_string(trim($_POST['pattern']))); $thatpattern = strtoupper(mysql_escape_string(trim($_POST['thatpattern']))); $topic = strtoupper(mysql_escape_string(trim($_POST['topic'])));

替换

$template = mysql_escape_string(trim(formatchinese($_POST['template']))); $filename = mysql_escape_string(trim($_POST['filename'])); $pattern = strtoupper(mysql_escape_string(trim(formatchinese($_POST['pattern'])))); $thatpattern = strtoupper(mysql_escape_string(trim(formatchinese($_POST['thatpattern'])))); $topic = strtoupper(mysql_escape_string(trim(formatchinese($_POST['topic']))));

至此,全部代码修改完毕。

修改program-o使之支持中文(四)
Share this