【问题标题】:Check similarity between strings in Javascript [closed]检查Javascript中字符串之间的相似性[关闭]
【发布时间】:2021-04-18 06:02:30
【问题描述】:

如何比较两个文本字符串是否相似,例如:

var a = "Hello Blue World";
var b = "Hello Blut World?";

if(a similar b)
{
    console.log(true);
}

【问题讨论】:

  • 您对相似的确切定义是什么?就像你想检查两个字符串是否有一些共同的子字符串,但是它们应该有多少和多长。相似没有确切的含义
  • 不是那么简单的任务,尤其是如果您对相似性的定义很灵活...medium.com/@sumn2u/…
  • 也许看看 Levenshtein 距离。虽然正如其他人所说,它不清楚你所说的相似到底是什么意思。

标签: javascript string-comparison


【解决方案1】:

您可以使用string-similarity 库。

根据 Dice 系数查找字符串之间的相似度,该系数大多优于 Levenshtein 距离。

var a = "Hello Blue World";
var b = "Hello Blut World?";

var stringSimilarity = require("string-similarity");
var similarityCoef = stringSimilarity.compareTwoStrings(a, b);

if (similarityCoef > 0.8) { console.log(true); }

请注意,similarityCoef 在字符串匹配率为 80% (0.8) 时打印为 true。您可以根据需要调整此值。

【讨论】:

    【解决方案2】:
    if 'n' is zero then strings are equal
    

      var str1 = "ab";
      var str2 = "cd";
      var n = str1.localeCompare(str2);
      console.log(n);

    【讨论】:

      【解决方案3】:

      这很棘手。因为您必须以某种方式以百分比来说明相似性对您意味着什么。这种方法怎么样?

      您逐个字符串比较它们并计算匹配项。我知道一旦其中一个字符串很早就有一个额外的字符,这就会失败。但一开始就足够了。

      var a = "Hello Blue World";
      var b = "Hello Blut World?";
      
       // only compare both strings with their mutual length, because of the loop we use
      const mutualLength = (a.length > b.length) ? b.length : a.length;
      const similarityAt = 90; // percent
      let matchCount = 0;
      
      // with each match increase matchCount by 1
      for (let pointer = 0; pointer < mutualLength; pointer++) {
          if (a.substring(pointer, 1) === (b.substring(pointer, 1) {
              matchCount++;
          }
      }
      
       // compute similarity in percent
      const similarity = (matchCount * 100) / mutualLength;
      
      console.log('Similarity given: ' + (similarity >= similarityAt));
      

      【讨论】:

        【解决方案4】:

        B"H

        取决于相似度,但如果你想要一个百分比来告诉你什么匹配,你可以简单地循环遍历最短的,并跟踪每个字符与最长字符串的后续索引匹配的次数(或将空格添加到最短的字符串,但这可能会弄乱一些计算),然后将匹配的总数除以(最短)字符​​串的长度以获得相等的百分比

        
        var str1 = "Hello blue world"
        var str2 = "Hello blut world?!"
        
        var shortest = str2.length >= str1.length?str1:str2
        var longest = str2.length < str1.length?str1:str2
        
        var matches= 0
        
        shortest
        .split("")
        //Just check if index of shortest
        //Matches index of longest, and if so (&& means do next
        //Expression) add the total number of matches by one
        .forEach (
        (x,k)=>
        ((x==longest[k]) && (matches++) )
        )
        
        //Final result, divide matches by total length
        var similarity = matches / shortest.length
        

        【讨论】:

          猜你喜欢
          • 2014-08-13
          • 2019-04-18
          • 1970-01-01
          • 1970-01-01
          • 2021-03-20
          • 2019-01-26
          • 2014-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多