YahooのWEBAPIを使い、形態素解析を行います。
YahooAPIを使うメリットは、Mecab等のインストールの手間を省けるということでしょうか。
とはいえ、一日あたり5万件のAPI制限が壁ですかねぇ・・・。
使い方は、http://developer.yahoo.co.jp/ でAppidを取得。
それをYahooAPI.Appid に入力して、それぞれのメソッドを実行してください。
これは、非同期処理ということを一応頭に入れといてください。
単語だけを抽出したいなら、形態素解析よりもキーフレーズ解析の方が便利そうです。
API自体の細かい使い方は、
http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html
http://developer.yahoo.co.jp/webapi/jlp/keyphrase/v1/extract.html
を読んでください。
filename
using System;
using System.Net;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace MyLib.WebAPI.Yahoo
{
public static class YahooAPI
{
public static string Appid { get; set; }
}
/// <summary>
/// http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html 参照
/// </summary>
public class KeitaisoAPI
{
public void Parse(string sentence, string filter, string results, Action<ParseResult> endAction)
{
string baseUrl = "http://jlp.yahooapis.jp/MAService/V1/parse";
var post = "appid=" + YahooAPI.Appid + "&sentence=" + Uri.EscapeUriString(sentence) + "&filter=" + filter + "&result=" + results;
WebClient wc = new WebClient();
wc.UploadStringAsync(new Uri(baseUrl), post);
wc.UploadStringCompleted += (s, e) =>
{
var root = XElement.Parse(e.Result);
var ns = root.GetDefaultNamespace();
var list = root.Descendants(ns + "word").Select(n => new Word() { Surface = n.Element(ns + "surface").Value, Pos = n.Element(ns + "pos").Value, Reading = n.Element(ns + "reading").Value });
ParseResult pr = new ParseResult() { Text = sentence, Words = list.ToList() };
endAction(pr);
};
}
public void Keyphrase(string sentence, Action<KeyphraseResults> endAction, object tag)
{
string baseUrl = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract";
var post = "appid=" + YahooAPI.Appid + "&sentence=" + Uri.EscapeUriString(sentence);
WebClient wc = new WebClient();
wc.UploadStringAsync(new Uri(baseUrl), post);
wc.UploadStringCompleted += (s, e) =>
{
var root = XElement.Parse(e.Result);
var ns = root.GetDefaultNamespace();
var list = root.Descendants(ns + "Result").Select(n => new KeyphraseResult() { Keyphrase = n.Element(ns + "Keyphrase").Value, Score = double.Parse(n.Element(ns + "Score").Value) });
endAction(new KeyphraseResults() { Results = list.ToList(), Tag = tag });
};
}
public class KeyphraseResult
{
public string Keyphrase { get; set; }
public double Score { get; set; }
}
public class KeyphraseResults
{
public List<KeyphraseResult> Results { get; set; }
public object Tag { get; set; }
}
public class Word
{
public string Surface { get; set; }
public string Reading { get; set; }
public string Pos { get; set; }
}
public class ParseResult
{
public string Text { get; set; }
public List<Word> Words { get; set; }
}
}
}
^^^