/ AIML

program-o人机对话评分规则

program-o需要从数据库中查询符合条件的对话,但是有时候不仅仅有一条符合的结果,这个时候就需要评分机制来选取最佳对话。

下面是评分用的代码,具体规则看注释:

foreach($ansArr as $item => $set) { $points = 0;//初始化分数 foreach($set as $tag => $bit) { if($tag == "pattern")//判断用户输入匹配项 { $chkpat = $bit; //if($input == $bit) //exact match if(preg_match('/\b('.$input.')\b/i', $bit,$found)) { $points += 11;//符合匹配+11分 //echo "

$input == $bit

"; } //echo "

$input == $bit

"; if($bit == "RANDOM PICKUP LINE") { $points = 7;//如果结果的匹配项是RANDOM PICKUP LINE则分数为7 break; } else { $p = preg_split("/\s/",trim($bit)); foreach($p as $w => $word) { if($word == "") { $points += 100;//如果匹配项是_加100分 //echo "

100 $bit

"; } elseif($word == "") { $points += 5;//如果匹配项是加100分 } else {//下面是非中文的匹配,忽略。 if(preg_match('/[^A-Za-z0-9]/i', $word,$found)) //matching on symbol like = or + { $points += 11; } elseif(preg_match('/\b(x¦the¦of¦and¦a¦to¦in¦is¦you¦that¦it¦he¦was¦for¦on¦as¦with¦his¦they¦I¦at¦be¦this¦have¦from¦or¦one¦had¦by¦word¦but¦not¦what¦all¦were¦we¦when¦your¦can¦said¦there¦use¦an¦each¦which¦she¦do¦how¦their¦if¦will¦up¦other¦about¦out¦many¦then¦them¦these¦so¦some¦her¦would¦make¦like¦him¦into¦time¦has¦look¦two¦more¦write¦go¦see¦number¦no¦way¦could¦people¦my¦than¦first¦water¦been¦call¦who¦oil¦its¦now¦find¦long¦down¦day¦did¦get¦come¦made¦may¦part)\b/i', $word,$found)) { $points += 11; } else { $points += 30; } } } } } if($tag == "topic") { if($bit!="") { if(trim(strtoupper($bit))trim(strtoupper($storedtopic))) { $points += 100;//如果topic匹配则+100 } else { $points -= 100;//不匹配就-100 } } } if($tag == "#") { $i = $bit; } if($tag == "thatpattern") { if($bit!="") { if($bit$storedthatpattern) { $points += 90;//如果之前的对话匹配+90 } elseif( (strpos($bit,"*")!==FALSE)&&(strpos($bit,"
")!==FALSE))//if the that pattern has no stars or underscores { $points -= 1000;//如果之前的对话不匹配就-1000 } elseif($bit!=$storedthatpattern) { //将_替换为之后再匹配测试 $bit = str_replace("_","(.)",$bit); $bit = str_replace("","(.)",$bit); if(preg_match('/'.$bit.'/is',$storedthatpattern,$found)) { $points += 85;//如果之前的对话匹配+85 } else { $points -= 1000;//如果之前的对话不匹配就-1000 } } } } } $score[$i]=$points;//最终分数 if($ansArr[$i]['pattern'] == 'RANDOM PICKUP LINE') { $botoutput="RANDOM LINE"; } else { $botoutput=htmlentities($ansArr[$i]['template']); } $debug[$i][$points][$ansArr[$i]['pattern']]=$botoutput."###".$ansArr[$i]['thatpattern']."###".$ansArr[$i]['topic']; }

所以如果要提高一个回答的优先级,可以在topic和thatpattern中填入*,就能提高优先级。

program-o人机对话评分规则
Share this