在自然語言處理的研究領域中,「Word Segmentation(斷詞)」是一個相當重要的技術,舉個英文的例子來說:
I like to play baseball.
斷詞後的結果:
「I」、「like」、「to」、「play」、「baseball」
因為英文字句中的各個單字之間都有著「空隔」來區隔,但中文呢?卻毫無如此的規則可循~
所以利用詞庫的方式是一種解決方案~ 這裡介紹利用「中研院-CKIP中文斷詞系統」來協助我們進行中文的斷詞工作~
雖然你也可以在「中研院-CKIP中文斷詞系統」申請使用~ 不過需要自行撰寫相關的Socket程式並剖析XML資料~
所以本文提供使用Web Scraping的方式來達成~
CKIP.php
<?php /** * Date: 2008/06/06 * Shen(http://blog.ring.idv.tw) */ require_once "HttpClient.php"; class CKIP { private $param = ""; private $body = ""; function __construct(){} public function setParam($param) { $this->param = $param; } public function doQuery() { if($this->param != "") { $uri = "http://mt.iis.sinica.edu.tw/cgi-bin/text.cgi"; $data = array('query' => $this->param); $body = HttpClient::quickPost($uri, $data); $regex = '/URL=\'(.*)\'\">/Us'; preg_match($regex,$body,$match); $redirect = "http://mt.iis.sinica.edu.tw/" . $match[1]; $body = HttpClient::quickGet($redirect); $regex = '/HREF=(.*)>/Us'; preg_match($regex,$body,$match); $filename = str_replace(".txt", ".tag.txt", $match[1]); $redirect = "http://mt.iis.sinica.edu.tw/uwextract/pool/" . $filename; $this->body = HttpClient::quickGet($redirect); return true; } return false; } public function getBody() { return $this->body; } } ?>
Demo
<?php require_once "CKIP.php"; $ckip = new CKIP(); $ckip->setParam("這個東西不錯吃!"); $ckip->doQuery(); $body = $ckip->getBody(); $body = iconv("Big5","UTF-8",$body); echo $body; ?>
您好
我最近也在研究斷詞這一塊
我有Run過您所放上的這支程式
可是發現沒有HttpClient.php所以無法跑這支程式
可以請您放上來嗎?
謝謝您
2011-10-25 21:39:51
HttpClient程式你Google一下就有許多類似的程式了,純粹只是下個Http Request而已~
2011-10-25 22:07:37
謝謝您, 已解決
2011-10-28 11:48:46