c#:推荐一款拼音转汉字组件

环境:

  • window10 x64
  • vs2019
  • .net core3.1

组件: ToolGood.Words.Pinyin

源码:https://github.com/toolgood/ToolGood.Words.Pinyin
nuget: Install-Package ToolGood.Words.Pinyin -Version 3.0.1.4

使用方法:

[TestClass]
public class PinYinTests
{
    /// <summary>
    /// 测试首字母
    /// </summary>
    [TestMethod]
    public void GetFirstPinyin_Test()
    {
        //获取首字母
        var str = WordsHelper.GetFirstPinyin("中心").ToUpper();
        Assert.IsTrue(str == "ZX");
        //多音字
        str = WordsHelper.GetFirstPinyin("银行").ToUpper();
        Assert.IsTrue(str == "YH");
        //无声母,带标点
        str = WordsHelper.GetFirstPinyin("啊,嗯");
        Assert.IsTrue(str == "A,E");
        //带英文
        str = WordsHelper.GetFirstPinyin("Jack小明HAODE小红").ToUpper();
        Assert.IsTrue(str == "JACKXMHAODEXH");
    }

    [TestMethod]
    public void GetPinyin_Test()
    {
        //
        var str = WordsHelper.GetPinyin("中心");
        Assert.IsTrue(str == "ZhongXin");
        //多音字
        str = WordsHelper.GetPinyin("银行");
        Assert.IsTrue(str == "YinHang");

        //网络上叫法不统一,语文标准JiShuiPaiShui,但大众及专家叫法GeiPaiShui
        //str = WordsHelper.GetPinyin("给水排水");
        //Assert.IsTrue(str == "JiShuiPaiShui");

        str = WordsHelper.GetPinyin("给予");
        Assert.IsTrue(str == "JiYu");
        str = WordsHelper.GetPinyin("解决");
        Assert.IsTrue(str == "JieJue");
        str = WordsHelper.GetPinyin("解数");
        Assert.IsTrue(str == "XieShu");
        str = WordsHelper.GetPinyin("给谁");
        Assert.IsTrue(str == "GeiShui");
        //人名
        str = WordsHelper.GetPinyin("张三");
        Assert.IsTrue(str == "ZhangSan");
        str = WordsHelper.GetPinyin("王国庆");
        Assert.IsTrue(str == "WangGuoQing");
        //带字母
        str = WordsHelper.GetPinyin("XiaoMing小明ok哈哈");
        Assert.IsTrue(str == "XiaoMingXiaoMingokHaHa");
        //无声母,带标点
        str = WordsHelper.GetPinyin("晓明,啊,呀,嗯&鞥^");
        Assert.IsTrue(str == "XiaoMing,A,Ya,En&Eng^");
    }

    [TestMethod]
    public void HasChinese_Test()
    {
        var b = WordsHelper.HasChinese("xiaomingX交shuji");
        Assert.IsTrue(b);
        b = WordsHelper.HasChinese("xjijsoajdiuah");
        Assert.IsFalse(b);
        //中文标点符号不是算中文汉字
        b = WordsHelper.HasChinese(",");
        Assert.IsFalse(b);
    }

    [TestMethod]
    public void IsAllChinese_Test()
    {
        var b = WordsHelper.IsAllChinese("中文啊");
        Assert.IsTrue(b);
        b = WordsHelper.IsAllChinese("小明x");
        Assert.IsFalse(b);
        //标点符号(中英文)以及阿拉伯数字均不算中文
        b = WordsHelper.IsAllChinese("小红,");
        Assert.IsFalse(b);
        b = WordsHelper.IsAllChinese("小刚,");
        Assert.IsFalse(b);
        b = WordsHelper.IsAllChinese("小红456");
        Assert.IsFalse(b);
    }

    [TestMethod]
    public void HasEnglisg_Test()
    {
        var b = WordsHelper.HasEnglish("小墨迹个x申");
        Assert.IsTrue(b);
        b = WordsHelper.HasEnglish("书记");
        Assert.IsFalse(b);
        //英文标点不算是英文文字
        b = WordsHelper.HasEnglish(",");
        Assert.IsFalse(b);
        b = WordsHelper.HasEnglish(",");
        Assert.IsFalse(b);
    }

    [TestMethod]
    public void IsAllEnglish_Test()
    {
        var b = WordsHelper.IsAllEnglish("hifhaierufhia晓");
        Assert.IsFalse(b);
        b = WordsHelper.IsAllEnglish("tomjack");
        Assert.IsTrue(b);
        //
        b = WordsHelper.IsAllEnglish("fadfa,");
        Assert.IsFalse(b);
        b = WordsHelper.IsAllEnglish("aefad,");
        Assert.IsFalse(b);
        b = WordsHelper.IsAllEnglish("adfas4521");
        Assert.IsFalse(b);
    }

    [TestMethod]
    public void GetPinyinForName_Test()
    {
        var str = WordsHelper.GetPinyinForName("小红");
        Assert.IsTrue(str == "XiaoHong");
        str = WordsHelper.GetPinyinForName("晓铭张三");
        Assert.IsTrue(str == "XiaoMingZhangSan");
        str = WordsHelper.GetPinyinForName("张三");
        Assert.IsTrue(str == "ZhangSan");
        str = WordsHelper.GetPinyinForName("xdjoaj张晓明");
        Assert.IsTrue(str == "xdjoajZhangXiaoMing");
    }

    [TestMethod]
    public void GetPinyinList_Test()
    {
        var list = WordsHelper.GetPinyinList("银行");
        Assert.IsTrue(list.Length == 2);
        Assert.IsTrue(list[0] == "Yin");
        Assert.IsTrue(list[1] == "Hang");

        list = WordsHelper.GetPinyinList("jack tom 小明");
        Assert.IsTrue(list.Length == 11);
        Assert.IsTrue(string.Join("", list) == "jack tom XiaoMing");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值