星期三, 5月 13, 2009

[.Net]如何在 .Net 調用Yahoo!搜索 斷章取義 API

無心工作,剛好又看到有人問怎麼用,所以就牛刀小試一下。基本上用 WebClient 就可以搞定:


//
// Yahoo!搜尋『斷章取義』API http://tw.developer.yahoo.com/cas/
// Yahoo!搜尋『斷章取義』API 技術文件 http://tw.developer.yahoo.com/cas/api.php
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Net;

namespace Yahoo
{
public class CAS
{
private string _GetString( byte[] bytes )
{
return Encoding.UTF8.GetString(bytes);
}

private WebClient GetWebClient()
{
WebClient client = new WebClient();
// client.Proxy = new WebProxy("localhost", 8000);
client.Encoding = Encoding.UTF8;
return client;
}

private NameValueCollection GetParameters()
{
NameValueCollection data = new NameValueCollection();

// TODO: Place your appid here.
data.Add("appid", "APgdNPnV34E7WQlhpBYaQvaRWPjwvd8exe094Q_r_7GWEOBFh9UDQY6vqNgZVwhc");
return data;
}

public string WordSegmentation(string content)
{
NameValueCollection data = GetParameters();
data.Add("content", content);

WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues("http://asia.search.yahooapis.com/cas/v1/ws", "POST", data);
return _GetString(responseArray);
}

public string Authenticate()
{
NameValueCollection data = GetParameters();

WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues("http://asia.search.yahooapis.com/cas/v1/AuthBootUp.php", "POST", data);
return _GetString(responseArray);
}

public string KeywordExtraction( string content, int threshold, int maxnum )
{
NameValueCollection data = GetParameters();
data.Add("content", content);
data.Add("threshold", threshold.ToString());
data.Add("maxnum", maxnum.ToString());

WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues("http://asia.search.yahooapis.com/cas/v1/ke", "POST", data);
return _GetString(responseArray);
}

public static void Main()
{
Yahoo.CAS cas = new Yahoo.CAS();

// 如果你重新申請 appid 的話,要先作 Authenticate
// Console.WriteLine( cas.Authenticate() );
Console.WriteLine( cas.WordSegmentation( "your_text" ));
Console.WriteLine( cas.KeywordExtraction( "your_text", 30, 10));

// 得到的字串是 XML,要轉為 DataSet 的話,可以這樣作
/*
string text = cas.WordSegmentation( "your_text" );
TextReader stringReader = new StringReader(text);
DataSet dataSet1 = new DataSet();
dataSet1.ReadXml(stringReader);
// 轉好以後應會有兩個 DataTable,資料都在第二個 Table 裡,也就是 dataSet1.Tables[1]
*/
}
}
}

沒有留言: