环境:

  • 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");
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐