国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

中文分詞的php代碼

2024-05-04 21:48:48
字體:
來源:轉載
供稿:網友

以前有用過dedecms分詞功能,經過測試還是不理想,后來經過一些處理得到的結果還是可以接受的,今天我再看到這款分詞法,拿出來給大家看看,實例代碼如下:

  1. <?php 
  2. class NLP{  
  3. private static $cmd_path;  
  4. // 不以'/'結尾  
  5. static function set_cmd_path($path){  
  6. self::$cmd_path = $path;  
  7. }  
  8. private function cmd($str){  
  9. $descriptorspec = array(  
  10. 0 => array("pipe""r"),  
  11. 1 => array("pipe""w"),  
  12. );  
  13. $cmd = self::$cmd_path . "/ictclas";  
  14. $process = proc_open($cmd$descriptorspec$pipes);  
  15. if (is_resource($process)) {  
  16. $str = iconv('utf-8''gbk'$str);  
  17. fwrite($pipes[0], $str);  
  18. $output = stream_get_contents($pipes[1]);  
  19. fclose($pipes[0]);  
  20. fclose($pipes[1]);  
  21. $return_value = proc_close($process);  
  22. }  
  23. /*  
  24. $cmd = "printf '$input' | " . self::$cmd_path . "/ictclas";  
  25. exec($cmd, $output, $ret);  
  26. $output = join("n", $output);  
  27. */  
  28. $output = trim($output);  
  29. $output = iconv('gbk''utf-8'$output);  
  30. return $output;  
  31. }  
  32. /**  
  33. * 進行分詞, 返回詞語列表.  
  34. */  
  35. function tokenize($str){  
  36. $tokens = array();  
  37. $output = self::cmd($input);  
  38. if($output){  
  39. $ps教程 = preg_split('/s+/'$output);  
  40. foreach($ps as $p){  
  41. list($seg$tag) = explode('/'$p);  
  42. $item = array(  
  43. 'seg' => $seg,  
  44. 'tag' => $tag,  
  45. ); //開源代碼Vevb.com 
  46. $tokens[] = $item;  
  47. }  
  48. }  
  49. return $tokens;  
  50. }  
  51. }  
  52. NLP::set_cmd_path(dirname(__FILE__));  
  53. ?> 

用起來很簡單,確保 ICTCLAS 編譯后的可執(zhí)行文件和詞典在當前目錄,代碼如下:

  1. <?php  
  2. require_once('NLP.php');  
  3. var_dump(NLP::tokenize('Hello, World!'));  
  4. ?> 

進行中文分詞的 PHP 類就在下面了,用 proc_open() 函數來執(zhí)行分詞程序,并通過管道和其交互, 輸入要進行分詞的文本, 讀取分詞結果.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐源县| 天长市| 武安市| 岳阳市| 兴隆县| 新民市| 兴业县| 若羌县| 南涧| 磐安县| 建瓯市| 郑州市| 宝丰县| 南川市| 永新县| 安溪县| 旌德县| 若尔盖县| 英超| 苏尼特左旗| 鸡西市| 遂平县| 滁州市| 嵩明县| 沙雅县| 阿勒泰市| 五家渠市| 宣武区| 什邡市| 巴里| 博白县| 宣化县| 特克斯县| 北票市| 江都市| 通州市| 石渠县| 个旧市| 宣威市| 若尔盖县| 鄂州市|