目前,CKIP Client 開放原始碼專案已實作 Java、PHP 兩種版本,可以用來簡化 CKIP 斷詞服務用戶端應用程式的開發,而不需自行撰寫 Socket 程式碼及處理 XML 資料。而本文的 CKIP Client for .NET 則是自己重新以 C# 實作 CLR 非同步程式撰寫模型(Asynchronous Programming Model)所設計的斷詞服務用戶端 APIs。
使用範例
下列範例會使用 CkipClient 在同步封鎖模式進行連接、傳送文本,並接收傳回結果,其中 username 及 password 為用戶端所申請之帳號及密碼。
using (CkipClient client = new CkipClient("username", "password"))
{
client.Send("菩提本無樹,明鏡亦非臺;本來無一物,何處惹塵埃?");
SegmentationResult result = client.GetResult();
if (result.StatusCode == StatusCode.Success)
{
StringBuilder sb = new StringBuilder();
foreach (Term term in result.GetTerms())
{
sb.Append(term.ToString() + " ");
}
Console.WriteLine(sb.ToString());
}
else
{
Console.WriteLine(string.Format("Error: {0}", result.StatusDescription));
}
}
輸出結果如下:
菩提(N) 本無樹(N) ,(COMMACATEGORY) 明鏡(N) 亦(ADV) 非(Vt) 臺(N) ;(SEMICOLONCATEGORY) 本來(ADV) 無(Vt) 一(DET) 物(N) ,(COMMACATEGORY) 何處(N) 惹(Vt) 塵埃(N) ?(QUESTIONCATEGORY)
非同步 API 使用範例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using CKIP;
namespace AsyncCkipClient
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void uxSend_Click(object sender, EventArgs e)
{
CkipClient client = new CkipClient();
client.BeginConnect(uxUsername.Text, uxPassword.Text,
new AsyncCallback(EndConnectCallback), client);
DisplayStatus("Connecting to server ...");
}
private void EndConnectCallback(IAsyncResult ar)
{
CkipClient client = (CkipClient)ar.AsyncState;
try
{
client.EndConnect(ar);
if (client.Connected)
{
client.BeginSend(uxRawText.Text,
new AsyncCallback(EndSendCallback), client);
DisplayStatus("Sending data to server ...");
}
else
{
DisplayStatus(string.Format("Ready (last error: {0})", "Connect Failed!"));
}
}
catch (Exception ex)
{
client.Close();
DisplayStatus(string.Format("Ready (last error: {0})", ex.Message));
}
}
private void EndSendCallback(IAsyncResult ar)
{
CkipClient client = (CkipClient)ar.AsyncState;
try
{
client.EndSend(ar);
client.BeginGetResult(new AsyncCallback(EndGetResultCallback), client);
DisplayStatus("Reading server response ...");
}
catch (Exception ex)
{
client.Close();
DisplayStatus(string.Format("Ready (last error: {0})", ex.Message));
}
}
private void EndGetResultCallback(IAsyncResult ar)
{
CkipClient client = (CkipClient)ar.AsyncState;
try
{
SegmentationResult result = client.EndGetResult(ar);
if (result.StatusCode == StatusCode.Success)
{
StringBuilder sb = new StringBuilder();
foreach (Term term in result.GetTerms())
{
sb.Append(term.ToString() + " ");
}
DisplayResults(sb.ToString());
}
else
{
DisplayStatus(string.Format("Ready (last error: {0})",
result.StatusDescription));
}
}
catch (Exception ex)
{
DisplayStatus(string.Format("Ready (last error: {0})", ex.Message));
}
finally
{
client.Close();
}
}
public void DisplayResults(string text)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(DisplayResults), text);
return;
}
uxResult.Text = text;
DisplayStatus("Done");
}
public void DisplayStatus(string text)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(DisplayStatus), text);
return;
}
uxStatus.Text = text;
}
}
}
參考資料:
實作 CLR 非同步程式撰寫模型 (Programming Model)