最近有個項目要實現自動產生標籤,發現discuz! 3.0x已經實現了這個功能,想問問discuz是如何?分詞和產生標籤的。例如:http://www.playsc.com/forum/forum.php?mod=viewthread&tid=359314&extra=page%3D1以上連結地址有標籤:SPL, Hero, 三星, 季後賽這些標籤都是自動產生的。這樣的功能如何??discuz!在哪裡實現這個功能?
主要是怎麼實現的分詞?哪裡有演算法?特別是php如何??
(我把discuz!下來了但是只找到tag.php類裡面updatedata($appid, $data)函數,沒有上一步分詞函數)
回複內容:
最近有個項目要實現自動產生標籤,發現discuz! 3.0x已經實現了這個功能,想問問discuz是如何?分詞和產生標籤的。例如:http://www.playsc.com/forum/forum.php?mod=viewthread&tid=359314&extra=page%3D1以上連結地址有標籤:SPL, Hero, 三星, 季後賽這些標籤都是自動產生的。這樣的功能如何??discuz!在哪裡實現這個功能?
主要是怎麼實現的分詞?哪裡有演算法?特別是php如何??
(我把discuz!下來了但是只找到tag.php類裡面updatedata($appid, $data)函數,沒有上一步分詞函數)
DZ系列都有一個線上分詞工具,具體的URI地址為:"http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=$SC[charset]&ocs=$SC[charset]"
以本標題為例子 discuz! 2.5x 3.0x的自動tag系統是如何?的?,下面為產生的uri地址: http://keyword.discuz.com/related_kw.html?title=discuz!%202.5x%203.0x%E7%9A%84%E8%87%AA%E5%8A%A8tag%E7%B3%BB%E7%BB%9F%E6%98%AF%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E7%9A%84%EF%BC%9F&content=discuz!%202.5x%203.0x%E7%9A%84%E8%87%AA%E5%8A%A8tag%E7%B3%BB%E7%BB%9F%E6%98%AF%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E7%9A%84%EF%BC%9F&ics=utf-8&ocs=utf-8請求後返回的是XML格式的內容:
36000 4 0 1291287160 0 的 是 discuz 如何
PHP具體調用代碼為:
function get_dz_tag($subject , $message){ global $_SC; $subjectenc = rawurlencode(strip_tags($subject)); $messageenc = rawurlencode(strip_tags(preg_replace("/\[.+?\]/U", '', $message))); $data = @implode('', file("http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=$_SC[charset]&ocs=$_SC[charset]")); if($data) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $data, $values, $index); xml_parser_free($parser); $kws = array(); foreach($values as $valuearray) { if($valuearray['tag'] == 'kw' || $valuearray['tag'] == 'ekw') { if(PHP_VERSION > '5' && $_SC['charset'] != 'utf-8') { $kws[] = siconv(trim($valuearray['value']), $_SC['charset'], 'utf-8');//??????? } else { $kws[] = trim($valuearray['value']); } } } $return = ''; if($kws) { foreach($kws as $kw) { $kw = shtmlspecialchars($kw); $return .= $kw.' '; } $return = trim($return); } return $return; } }
當然這隻是一個線上分詞工具,當然你也可以使用PHP擴充進行操作:如scws。
scws 有線上 api 和 擴充版兩個方式:
api代碼為:
function Scws($string){ $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_URL, "http://www.ftphp.com/scws/api.php"); curl_setopt($ch, CURLOPT_POSTFIELDS, "data={$string}&respond=json"); ob_start(); curl_exec($ch); $content = ob_get_contents(); curl_close($ch); ob_clean(); $content = json_decode($content ,true); return $content; }
具體擴充版請參考scws文檔:scws