Skip to content

Instantly share code, notes, and snippets.

@kiichi54321
Created October 29, 2013 07:14
Show Gist options
  • Save kiichi54321/7210269 to your computer and use it in GitHub Desktop.
Save kiichi54321/7210269 to your computer and use it in GitHub Desktop.
Yahoo!の形態素解析API用のライブラリ。簡易的に同期版もある。
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 ParseResult ParseSynchronous(string sentence, string filter, string results)
{
bool flag = true;
ParseResult pr = null;
Parse(sentence, filter, results, (n) => { pr = n; flag = false; });
while (flag)
{
System.Threading.Thread.Sleep(10);
}
return 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 });
};
}
/// <summary>
/// Keyphrase取得の同期版
/// </summary>
/// <param name="sentence"></param>
/// <returns></returns>
public List<KeyphraseResult> KeyphraseSynchronous(string sentence)
{
string baseUrl = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract";
var post = "appid=" + YahooAPI.Appid + "&sentence=" + Uri.EscapeUriString(sentence);
bool flag = true;
WebClient wc = new WebClient();
wc.UploadStringAsync(new Uri(baseUrl), post);
List<KeyphraseResult> list = new List<KeyphraseResult>();
wc.UploadStringCompleted += (s, e) =>
{
var root = XElement.Parse(e.Result);
var ns = root.GetDefaultNamespace();
list = root.Descendants(ns + "Result").Select(n => new KeyphraseResult() { Keyphrase = n.Element(ns + "Keyphrase").Value, Score = double.Parse(n.Element(ns + "Score").Value) }).ToList();
flag = false;
};
while (flag)
{
System.Threading.Thread.Sleep(10);
}
return list;
}
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; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment