您的位置:首页 > 其它

使用Microsoft Visual Studio International Pack获得中文字符串的所有拼音组合(处理多音字)

2012-02-09 17:24 736 查看
最近一个项目中客户需要做人名的拼音检索,就像QQ好友那样,输入拼音就提示符合的联系人。上网找了一下,发现微软已经发布了相关的支持库,不仅可以处理拼音,还能处理笔画、简繁体转换等。csdn上有一篇博客介绍了基本的使用方法,微软发布的库也包含了比较全面的文档。但实际使用过程中发现了几个问题:

多音字的处理,当人名中含有多音字时,处理会变得相对复杂。

汉字拼音边界的处理,比如人名为“小明”,拼音为"XIAOMING",当输入"AOM"时,客户可能不希望看到“小明”这个结果。

本文分析第一个问题。此问题的目标是对于特定的字符串,生成其对应拼音列表。比如“重”有"CHONG2"、"ZHONG4"两个音,“量”有"LIANG2"、"LIANG4"、"LIANG5"三个音(5代表轻声)。最后生成的列表应该是"CHONGLIANG"、"ZHONGLIANG"。由于使用微软的国际化库ChineseChar类的Pinyins属性得到的事一个元素个数固定为8的ReadOnlyCollection<string>类型,其中元素包含读音,而且集合会包含null元素,所以必须先做一步处理,取得无音标的拼音。为ChineseChar添加一个扩展方法如下:



public static class ChineseCharExtension

{

/// <summary>

/// Get the distinct pinyins of a <see cref="ChineseChar"/> without the sound indicator.<para />

/// The result strings are upper case.

/// For example, "重" will get a result of "ZHONG" and "CHONG",

/// while "量" will only get "LIANG".

/// </summary>

/// <param name="chineseChar"></param>

/// <returns></returns>

public static IEnumerable<string> PinyinsWithoutSound (this ChineseChar chineseChar)

{

return chineseChar.Pinyins.Where (p => p != null).Select (p => p.Substring (0, p.Length - 1)).Distinct ();

}

}

然后是取得整个字符串的拼音组合,这本质上就是个回溯算法,我用了递归来实现,为了方便使用,直接做成了string的扩展方法。



public static class ChineseString

{

/// <summary>

/// Get pinyins of a string containing chinese characters.

/// </summary>

/// <param name="chinese">The input string.</param>

/// <returns>The pinyin combinations.</returns>

public static IEnumerable<string> GetPinyins (this string chinese)

{

Char[] chineseChars = chinese.ToCharArray ();

return chineseChars.GetPinyins (0);

}

private static IEnumerable<string> GetPinyins (this char[] chinese, int startIndex)

{

//If the start index is beyond the string length, just yield return an empty string.

if (startIndex == chinese.Length)

{

yield return string.Empty;

}

else

{

//If this char is a chinese char.

if (ChineseChar.IsValidChar (chinese[startIndex]))

{

foreach (var charPinyin in (new ChineseChar (chinese[startIndex])).PinyinsWithoutSound ())

{

foreach (var remainingPinyins in chinese.GetPinyins (startIndex + 1))

{

yield return charPinyin + remainingPinyins;

}

}

}

else

{

//Is not chinese, just concat it with the remaining pinyins.

foreach (var remainingPinyins in chinese.GetPinyins (startIndex + 1))

{

yield return chinese[startIndex] + remainingPinyins;

}

}

}

}

}
这样,要判断用户输入的字符串是不是中文字符串"name"的拼音的一部分,只要用

name.GetPinyins().Any(p => p.ToUpper ().Contains (inputStr.ToUpper ()))
就行了。

Microsoft Visual Studio International Pack 下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyID=44cac7f0-633b-477d-aed2-99aee642fc10&DisplayLang=zh-cn

注意下面有个2.0的下载,但那个只是个Feature Pack,主要功能还在1.0中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐