Files
number_zj/20220330_Vote/RoadFlow.Pinyin/Pinyin4Name.cs
2022-03-30 17:54:33 +08:00

73 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using RoadFlow.Pinyin.data;
using RoadFlow.Pinyin.exception;
using System.Linq;
using RoadFlow.Pinyin.format;
namespace RoadFlow.Pinyin
{
/// <summary>
/// 处理姓名专用
/// </summary>
public static class Pinyin4Name
{
#region // 获取单字拼音
/// <summary>
/// 获取姓的拼音,如果是复姓则由空格分隔
/// </summary>
/// <param name="firstName">要查询拼音的姓</param>
/// <returns>返回姓的拼音若未找到姓则返回null</returns>
/// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
public static string GetPinyin(string firstName)
{
if (firstName.All(PinyinUtil.IsHanzi))
{
return NameDB.Instance.GetPinyin(firstName);
}
// 不是汉字
throw new UnsupportedUnicodeException("不支持的字符: 请输入汉字字符");
}
/// <summary>
/// 获取姓的首字母,如果是复姓则由空格分隔首字母
/// </summary>
/// <param name="firstName">要查询拼音的姓</param>
/// <returns>返回姓的拼音首字母若未找到姓则返回null</returns>
/// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
public static string GetFirstLetter(string firstName)
{
var pinyin = GetPinyin(firstName);
if (pinyin == null)
{
return null;
}
return string.Join(" ", pinyin.Split(' ').Select(py => py[0]));
}
/// <summary>
/// 获取格式化后的拼音
/// </summary>
/// <param name="firstName">要查询拼音的姓</param>
/// <param name="format">输出拼音格式化参数</param>
/// <see cref="PinyinOutputFormat"/>
/// <seealso cref="PinyinFormatter"/>
/// <returns>返回格式化后的拼音若未找到姓则返回null</returns>
/// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
public static string GetPinyinWithFormat(string firstName, PinyinOutputFormat format)
{
return string.Join(" ", GetPinyin(firstName).Split(' ').Select(item => PinyinFormatter.Format(item, format)));
}
#endregion
/// <summary>
/// 根据拼音查询匹配的姓
/// </summary>
/// <param name="pinyin"></param>
/// <param name="matchAll">是否全部匹配为true时匹配整个拼音否则匹配开头字符此参数用于告知传入的拼音是完整拼音还是仅仅是声母</param>
/// <returns>匹配的姓数组</returns>
public static string[] GetHanzi(string pinyin, bool matchAll)
{
return NameDB.Instance.GetHanzi(pinyin.ToLower(), matchAll);
}
}
}