之前有个需求,就是比较2个文本是否相近。

 

最牛逼的方法就是用语义去分析,然后比较结果。可是这个性能是在不敢恭维,于是想起了以前做过的人脸识别,使用特征值去操作。

 

人脸识别当是时把图片转变为一维向量,然后计算特征值。最终发现,如果是文本计算特征值,把文本的字符串输入后,简单的简化为每个char自相乘,再相加 = 向量的模的平方。

 

例如:

 

HI = (char 72)(char 73). 

eigenValue = 72*72+73*73.

 

这样,IH的结果和HI就是一样了。 而且另外一个问题就是char的取值范围是0~255。如果对文本检测,那么很快就会超过了double的长度了。针对这些问题,我自己想到了一个算法。

 

        public static double GetStringEigenValue(string value)
        {
            
if (string.IsNullOrEmpty(value))
                
return 0;

            
double length = value.Length;

            
double eigenvalue = 0;

            
int index = 1;

            
foreach (char str in value)
            {
                
double strnum = (double)str;

                eigenvalue 
+= (strnum * strnum) / 100000 * index++;
            }

            
//计算由于每个位置添加了权重,导致最终值增加的值

            
double weightfactor = (length - 1* 255 * 255 / 100000 + (length - 1* (length - 2* 255 * 255 / 2 / 100000;

            
if (weightfactor == 0)
                weightfactor 
= 1;

            weightfactor 
= Math.Ceiling(Math.Log10(weightfactor));

            
if (weightfactor == 0)
                weightfactor 
= 1;

            eigenvalue 
/= weightfactor;

            eigenvalue 
/= length;

            
return length + eigenvalue;
        }

相关文章:

  • 2021-08-13
  • 2021-11-22
  • 2021-09-07
  • 2021-05-28
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-04-29
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2021-07-18
  • 2021-05-19
  • 2022-12-23
相关资源
相似解决方案