初始提交
This commit is contained in:
100
Services/Hncore.Pass.BaseInfo/Utils/PinYinHelper.cs
Normal file
100
Services/Hncore.Pass.BaseInfo/Utils/PinYinHelper.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Microsoft.International.Converters.PinYinConverter;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Common
|
||||
{
|
||||
public class PinYinHelper
|
||||
{
|
||||
/*
|
||||
两种方法共用://优先使用NPinyin,转换失败则用 Pinyinconverter
|
||||
NPinyin 错误集:洺(不识别)
|
||||
Pinyinconverter 多音字错误集:广a,区o,强j。都是多音字
|
||||
*/
|
||||
/// <summary>
|
||||
/// 汉字转全拼
|
||||
/// </summary>
|
||||
/// <param name="strChinese"></param>
|
||||
/// <returns></returns>
|
||||
public static string ConvertToAllSpell(string strChinese)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (strChinese.Length != 0)
|
||||
{
|
||||
StringBuilder fullSpell = new StringBuilder();
|
||||
for (int i = 0; i < strChinese.Length; i++)
|
||||
{
|
||||
var chr = strChinese[i];
|
||||
fullSpell.Append(GetSpell(chr));
|
||||
}
|
||||
|
||||
return fullSpell.ToString().ToUpper();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 汉字转首字母
|
||||
/// </summary>
|
||||
/// <param name="strChinese"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFirstSpell(string strChinese)
|
||||
{
|
||||
//NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
|
||||
//return NPinyin.Pinyin.GetInitials(strChinese);
|
||||
|
||||
try
|
||||
{
|
||||
if (strChinese.Length != 0)
|
||||
{
|
||||
StringBuilder fullSpell = new StringBuilder();
|
||||
for (int i = 0; i < strChinese.Length; i++)
|
||||
{
|
||||
var chr = strChinese[i];
|
||||
fullSpell.Append(GetSpell(chr)[0]);
|
||||
}
|
||||
|
||||
return fullSpell.ToString().ToUpper();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 汉字转拼音
|
||||
/// </summary>
|
||||
/// <param name="chr"></param>
|
||||
/// <returns></returns>
|
||||
private static string GetSpell(char chr)
|
||||
{
|
||||
var coverchr = NPinyin.Pinyin.GetPinyin(chr);
|
||||
//优先使用NPinyin,转换失败则用 Pinyinconverter
|
||||
bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
|
||||
if (isChineses)
|
||||
{
|
||||
ChineseChar chineseChar = new ChineseChar(coverchr[0]);
|
||||
foreach (string value in chineseChar.Pinyins)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
return value.Remove(value.Length - 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return coverchr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user