using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
///
/// RubyWebAPI の概要の説明です
///
public class RubyWebAPI
{
#region "定数定義"
//YahooAPIアクセス用
private static string FURIGANA_SERVISE_URL = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana";
private static string FURIGANA_SERVISE_APPID = "※ ここにYahooのアプリケーションID ※";
private static int FURIGANA_SERVISE_GRADE = 1;
//フィルタオプション
public static int CONVERT_ZENKAKU = 1;
public static int CONVERT_ALPHA = 2;
public static int CONVERT_KIGOU = 4;
public static int CONVERT_KATAKANA = 8;
#endregion
#region "メソッド"
///
/// 漢字文字列を読み仮名に変換する
///
///
/// 漢字文字列
///
/// 読み仮名
// 正常に取得できなかった場合、string.emptyが返る
public static string getRubyString(string strKanjiText){
string strYomiText = string.Empty;
if (strKanjiText == "") return "";
string url = RubyWebAPI.FURIGANA_SERVISE_URL;
url += "?grade=" + RubyWebAPI.FURIGANA_SERVISE_GRADE.ToString();
url += "&appid=" + RubyWebAPI.FURIGANA_SERVISE_APPID;
url += "&sentence=" + HttpUtility.UrlEncode(strKanjiText);
try {
//Yahooから読み仮名XMLを取得
HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse wrs = (HttpWebResponse) wrq.GetResponse();
Stream st = wrs.GetResponseStream();
StreamReader sr = new StreamReader(st);
XmlDocument yomi = new XmlDocument();
yomi.LoadXml(sr.ReadToEnd());
//XMLをパースして読み仮名文字列を作成する
XmlNodeList xnResults = yomi.DocumentElement.GetElementsByTagName("Word");
XmlElement xe = null;
XmlNodeList xnYomi = null;
XmlElement xeMoto = null;
string str = string.Empty;
for (int i = 0; i < xnResults.Count; i++)
{
xe = (XmlElement)xnResults.Item(i);
xnYomi = xe.GetElementsByTagName("Furigana");
if (xnYomi.Count == 0)
{
xeMoto = (XmlElement)xe.GetElementsByTagName("Surface").Item(0);
str += xe.InnerText;
}
else
{
str += xnYomi.Item(0).InnerText;
}
}
strYomiText = str;
}
catch (Exception ex)
{
}
return strYomiText;
}
///
/// オプション動作させる場合のオーバーロード
///
///
///
///
public static string getRubyString(string strKanjiText, int intOption) {
string strYomiText = RubyWebAPI.getRubyString(strKanjiText);
if ((intOption & RubyWebAPI.CONVERT_ZENKAKU) > 0)
strYomiText = Microsoft.VisualBasic.Strings.StrConv(strYomiText, Microsoft.VisualBasic.VbStrConv.Wide, 0);
if ((intOption & RubyWebAPI.CONVERT_ALPHA) > 0)
strYomiText = RubyWebAPI.ConvertAlphabetToKana(strYomiText);
if ((intOption & RubyWebAPI.CONVERT_KIGOU) > 0)
strYomiText = RubyWebAPI.ConvertMarkSymbolToKana(strYomiText);
if ((intOption & RubyWebAPI.CONVERT_KATAKANA) > 0)
strYomiText = Microsoft.VisualBasic.Strings.StrConv(strYomiText, Microsoft.VisualBasic.VbStrConv.Katakana, 0);
return strYomiText;
}
#region "フィルタ"
///
/// 汎用変換処理
///
///
/// 変換元テキスト
///
///
/// 変換パターン
/// key:変換結果
/// value:keyに1対多で対応する変換元文字列として変換するstring[]であること
/// string[]でない場合例外
///
private static string ConvertMultiKeyword(string strBase, Hashtable htPattern)
{
string strReplace;
string[] strPattern;
int i;
foreach (DictionaryEntry de in htPattern)
{
if (de.Value.GetType() != Type.GetType("System.String[]")) throw new Exception("type must be 'System.String[]'");
strReplace = (string)de.Key;
strPattern = (string[])de.Value;
//短い変換候補が長い変換候補の内容を含んでいると問題になるので文字列長で並べ替える
IComparer ic = new StrLenComparer();
Array.Sort(strPattern, ic);
for (i = 0; i < strPattern.Length; i++)
{
strBase = strBase.Replace(strPattern[i], strReplace);
}
}
return strBase;
}
//アルファベット変換
private static string ConvertAlphabetToKana(string str)
{
Hashtable ht = new Hashtable();
ht.Add("えー", new string[] { "a", "A", "a", "A" });
ht.Add("びー", new string[] { "b", "B", "b", "B" });
ht.Add("しー", new string[] { "c", "C", "c", "C" });
ht.Add("でぃー", new string[] { "d", "D", "d", "D" });
ht.Add("いー", new string[] { "e", "E", "e", "E" });
ht.Add("えふ", new string[] { "f", "F", "f", "F" });
ht.Add("じー", new string[] { "g", "G", "g", "G" });
ht.Add("えいち", new string[] { "h", "H", "h", "H" });
ht.Add("あい", new string[] { "i", "I", "i", "I" });
ht.Add("じぇー", new string[] { "j", "J", "j", "J" });
ht.Add("けー", new string[] { "k", "K", "k", "K" });
ht.Add("える", new string[] { "l", "L", "l", "L" });
ht.Add("えむ", new string[] { "m", "M", "m", "M" });
ht.Add("えぬ", new string[] { "n", "N", "n", "N" });
ht.Add("おー", new string[] { "o", "O", "o", "O" });
ht.Add("ぴー", new string[] { "p", "P", "p", "P" });
ht.Add("きゅー", new string[] { "q", "Q", "q", "Q" });
ht.Add("あーる", new string[] { "r", "R", "r", "R" });
ht.Add("えす", new string[] { "s", "S", "s", "S" });
ht.Add("てぃー", new string[] { "t", "T", "t", "T" });
ht.Add("ゆー", new string[] { "u", "U", "u", "U" });
ht.Add("ぶい", new string[] { "v", "V", "v", "V" });
ht.Add("だぶりゅー", new string[] { "w", "W", "w", "W" });
ht.Add("えっくす", new string[] { "x", "X", "x", "X" });
ht.Add("わい", new string[] { "y", "Y", "y", "Y" });
ht.Add("ぜっと", new string[] { "z", "Z", "z", "Z" });
return RubyWebAPI.ConvertMultiKeyword(str, ht);
}
//記号変換
private static string ConvertMarkSymbolToKana(string str)
{
Hashtable ht = new Hashtable();
ht.Add("あんど", new string[] { "&", "&" });
ht.Add("ぷらす", new string[] { "+", "+" });
ht.Add("まいなす", new string[] { "-", "-" });
ht.Add("すらっしゅ", new string[] { "/", "/" });
ht.Add("どっと", new string[] { ".", ".", "。" });
ht.Add("えん", new string[] { "\\", "¥" });
ht.Add("はてな", new string[] { "?", "?" });
ht.Add("どる", new string[] { "$", "$" });
ht.Add("しゃーぷ", new string[] { "#", "#" });
ht.Add("ぱーせんと", new string[] { "%", "%" });
return RubyWebAPI.ConvertMultiKeyword(str, ht);
}
#endregion
#endregion
#region "並べ替え用のインナークラス"
//文字列の長さで並べ替えを行なう
//【参考】http://www.atmarkit.co.jp/fdotnet/dotnettips/215arraysort/arraysort.html
private class StrLenComparer : IComparer
{
public int Compare(object x, object y)
{
string str1 = (string)x;
string str2 = (string)y;
return str2.Length - str1.Length;
}
}
#endregion
}