/// <summary>
/// 存放排好序的汉字-拼音对照字典,格式如下
/// 汉han
/// 字zi
/// 既汉字作为字典的Key,拼音作为value
/// </summary>
private SortedDictionary<Char, string> mSortDiction = new SortedDictionary<Char, string>() ;
private static SpellLib _Instance ;
/// <summary>
/// 取汉字拼音类的实例
/// </summary>
/// <returns></returns>
public static SpellLib Instance()
{
if (_Instance == null)
_Instance = new SpellLib();
return _Instance;
}
/// <summary>
/// 构造函数
/// </summary>
private SpellLib()
{
BuildSortDiction();
}
#region 提供的方法
/// <summary>
/// 把给定的字转化为拼音
/// </summary>
/// <param name="word">要转化的字</param>
/// <returns>字的拼音</returns>
/// <example>
/// string strCode = SpellLib.Instance().GetCodesByWord('字');
/// </example>
/// <remarks>如果该字不存在拼音,就返回该字本身</remarks>
public string GetCodeByWord(Char word)
{
if (mSortDiction.ContainsKey(word))
return mSortDiction[word];
else
return word.ToString();
}
/// <summary>
/// 取得字符串第一个字的拼音
/// </summary>
/// <param name="word">要转化的字符串</param>
/// <returns>字的拼音</returns>
/// <example>
/// string strCode = SpellLib.Instance().GetCodesByWord("汉字");
/// </example>
/// <remarks>如果第一个字不存在拼音,就返回该字本身</remarks>
public string GetCodeByWord(string word)
{
if (word == null)
throw new ArgumentNullException("word");
if (word.Length > 0)
return GetCodeByWord(word[0]);
else
return null;
}
/// <summary>
/// 取得一个字符串中所有字的拼音
/// </summary>
/// <param name="sentence"></param>
/// <returns></returns>
public string GetCodesByWord(string sentence)
{
if (sentence == null)
throw new ArgumentNullException("sentence");
bool blAddSpace = false;
StringBuilder sbResult = new StringBuilder();
foreach (Char chr in sentence)
{
string strCodes = GetCodeByWord(chr) ;
if (strCodes.Length > 1)
{
if (blAddSpace == false)
{
sbResult.Append(" ");
}
sbResult.Append(strCodes);
sbResult.Append(" ");
blAddSpace = true;
}
else
{
sbResult.Append(strCodes);
blAddSpace = false;
}
}
return sbResult.ToString();
}
/// <summary>
/// 构造排序字典对象
/// </summary>
private void BuildSortDiction()
{
// mSortDiction.
for (int i = 0; i < this.WordList.Length; i++)
{
string value = this.CodeList[i];
string strWord = WordList[i];
foreach (char key in strWord)
{
if (mSortDiction.ContainsKey(key))
{
continue;
}
else
{
mSortDiction.Add(key, value);
}
}//end foreach
}//end for
}
/// <summary>
/// 取得给定拼音的声母
/// </summary>
/// <param name="spell">拼音</param>
/// <returns>声母</returns>
/// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>
public static string GetVoicePart(string spell)
{
string strVoicePart = null;
if (spell.Length == 1)
{
strVoicePart = spell;
}
else
{
char firstChr = spell[0];
char SecondChr = spell[1];
strVoicePart = firstChr.ToString();
if (SecondChr.Equals('h') || SecondChr.Equals('H'))
strVoicePart += "h";
switch (strVoicePart.ToLower(System.Globalization.CultureInfo.CurrentCulture))
{
case "b":
case "p":
case "m":
case "f":
case "d":
case "t":
case "n":
case "l":
case "g":
case "k":
case "h":
case "j":
case "q":
case "x":
case "r":
case "z":
case "c":
case "s":
case "zh":
case "ch":
case "sh":
break;
default:
strVoicePart = firstChr.ToString();
break;
}//end switch
}
return strVoicePart;
}
/// <summary>
/// 取得给定汉字的声母部分
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
/// <example>
/// string strVoicePart = SpellLib.Instance().GetCodeOfVoicePart("字");
/// </example>
/// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>
public string GetCodeOfVoicePart(string word)
{
return GetVoicePart(this.GetCodeByWord(word));
}
/// <summary>
/// 取得给定汉字的声母部分
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
/// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>
public string GetCodeOfVoicePart(Char word)
{
return GetVoicePart(this.GetCodeByWord(word));
}
#endregion
}//end class
}//end namespace