【asp.net】字符相似度


        /// <summary>
        /// 判断两个字符相似度
        /// 公式:l = q/(q+r+s);
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="isOff">是否关闭 (true: 将不计算,返回-1)</param>
        /// <returns></returns>
        public static double Similarity(string a, string b,bool isOff)
        {
            if (isOff)
            {
                return -1;
            }
            var a1 = "";
            var b1 = "";

            b = b.ToUpper(); //公司名称均是大写
            //去除影响相似度的词
            a = a.Trim();
            a = a.Replace("  ", " ");
            a = a.Replace("   ", " ");
            a = a.Replace("    ", " ");
            a = a.Replace("CO LTD", "");
            a = a.Replace("CC LTD", "");
            a = a.Replace("INC", "");
            a = a.Trim();

            //消除重复
            for (var i = 0; i < a.Length; i++)
            {
                var emtpy = a.Substring(i, 1);
                if (a1.Length == 0)
                {
                    a1 = emtpy;
                }
                if (a1.IndexOf(emtpy, 0, StringComparison.OrdinalIgnoreCase) < 0)
                {
                    a1 += emtpy;
                }
            }
            //消除重复
            for (var i = 0; i < b.Length; i++)
            {
                var emtpy = b.Substring(i, 1);
                if (b1.Length == 0)
                {
                    b1 = emtpy;
                }
                if (b1.IndexOf(emtpy, 0,StringComparison.OrdinalIgnoreCase) < 0)
                {
                    b1 += emtpy;
                }
            }

            //计算相似度
            if (a1.Length >= b1.Length)
            {
                var q = 0;
                for (var i = 0; i < b1.Length; i++)
                {
                    for (var j = 0; j < a1.Length; j++)
                    {
                        if (b1.Substring(i, 1) == a1.Substring(j, 1))
                        {
                            q += 1;
                        }

                    }
                }
                var provider = new NumberFormatInfo
                {
                    PercentDecimalDigits = 2, 
                    PercentPositivePattern = 1
                };
                //小数点保留几位数.
                //百分号出现在何处.
                var result = (double)q / (a1.Length + b1.Length - q);//一定要用double类型.
                return result;
            }
            else
            {
                var q = 0;
                for (var i = 0; i < a1.Length; i++)
                {
                    for (var j = 0; j < b1.Length; j++)
                    {
                        if (a1.Substring(i, 1) == b1.Substring(j, 1))
                        {
                            q += 1;
                        }
                    }
                }
                var provider = new NumberFormatInfo
                {
                    PercentDecimalDigits = 2, 
                    PercentPositivePattern = 1
                };
                //小数点保留几位数.
                //百分号出现在何处.
                var result = (double)q / (a1.Length + b1.Length - q);//一定要用double类型.
                return result;
            }
        }

 

posted @ 2016-11-29 16:41  游子善心  阅读(2)  评论(0编辑  收藏  举报  来源