判断两个字符相似度

        public static double SimilarityNoContains(string a, string b)
        {
            string a1 = "";
            string b1 = "";
            if (a==b)
            {
                return 1;
            }
           
          
            //消除重复
            for (int i = 0; i < a.Length; i++)
            {
                string emtpy = a.Substring(i, 1).ToString();
                if (a1.Length == 0)
                {
                    a1 = emtpy;
                }
                if (a1.IndexOf(emtpy, 0) < 0)
                {
                    a1 += emtpy;
                }
            }
            //消除重复
            for (int i = 0; i < b.Length; i++)
            {
                string emtpy = b.Substring(i, 1).ToString();
                if (b1.Length == 0)
                {
                    b1 = emtpy;
                }
                if (b1.IndexOf(emtpy, 0) < 0)
                {
                    b1 += emtpy;
                }
            }
            //计算相似度
            if (a1.Length >= b1.Length)
            {
                int q = 0;
                for (int i = 0; i < b1.Length; i++)
                {
                    for (int j = 0; j < a1.Length; j++)
                    {
                        if (b1.Substring(i, 1).ToString() == a1.Substring(j, 1).ToString())
                        {
                            q += 1;
                        }
                    }
                }
                System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
                provider.PercentDecimalDigits = 2;//小数点保留几位数.
                provider.PercentPositivePattern = 1;//百分号出现在何处.
                double result = (double)q / (a1.Length + b1.Length - q);//一定要用double类型.
                return result;
            }
            else
            {
                int q = 0;
                for (int i = 0; i < a1.Length; i++)
                {
                    for (int j = 0; j < b1.Length; j++)
                    {
                        if (a1.Substring(i, 1).ToString() == b1.Substring(j, 1).ToString())
                        {
                            q += 1;
                        }
                    }
                }
                System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
                provider.PercentDecimalDigits = 2;//小数点保留几位数.
                provider.PercentPositivePattern = 1;//百分号出现在何处.
                double result = (double)q / (a1.Length + b1.Length - q);//一定要用double类型.
                return result;

            }


        }

  

转载于:https://www.cnblogs.com/Aamir-Ye/p/4561210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值