国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 运城市| 汤原县| 鄂托克旗| 汶川县| 乌鲁木齐县| 游戏| 枣庄市| 灯塔市| 丘北县| 常宁市| 柞水县| 浮山县| 林西县| 上饶县| 辽阳县| 红原县| 岑溪市| 梁河县| 延吉市| 英德市| 资中县| 淮安市| 博客| 新郑市| 福泉市| 呼伦贝尔市| 唐河县| 宾川县| 沐川县| 巫溪县| 天柱县| 山阴县| 涟水县| 高陵县| 玉门市| 武冈市| 宁远县| 营口市| 新密市| 凤台县| 普陀区|