【问题标题】:Creating an algorithm to define matching probability of strings in Javascript创建一个算法来定义 Javascript 中字符串的匹配概率
【发布时间】:2019-07-20 06:09:53
【问题描述】:

我正在研究谷歌地图并试图找到指定的地方。但是,有时我会得到更多指定地点的结果(因为我试图在整个城市中搜索具有其名称的地点)。因此,我将输出限制为前三个匹配的建议,并排除了除此之外的所有结果。但问题是,前 3 个匹配的建议仍有可能在同一个地方,我只想显示他们的一个建议以使建议准确。 例如:

  • 我搜索过“必胜客”
  • 我得到的前三个结果如下:
    1. Pizza Hut, MI Road, Jaipur --- 第一个建议
    2. Pizza Hut, MI Road, Ajmeri Gate, Jaipur --- 第二个建议
    3. 必胜客,Malviya Nagar,斋浦尔 --- 第三个建议

第 1 条和第 2 条建议表示同一地点。

现在我想应用预测方法(使用概率)来发现第一个和第二个建议是相同的地方,而第三个是不同的地方。

我的方法是什么:-

var p = [], ap = []; //p -- places & ap -- array of splitted strings
    p[0] = "Pizza Hut, MI Road, Jaipur";
    p[1] = "Pizza Hut, MI Road, Ajmeri Gate, Jaipur";
    p[2] = "Pizza Hut, Malviya Nagar, Jaipur";

    //split all places
    ap[0] = p[0].split(",");
    ap[1] = p[1].split(",");
    ap[2] = p[2].split(",");

    /*
     --- Theoretically ---
     ### Splitting strings into symbols ###
     string_symbols_1 = a1, a2, a3;  --- a1 = "Pizza Hut", a2 = "MI Road", a3="Jaipur"; 
     string_symbols_2 = b1, b2, b3, b4; --- b1 = "Pizza Hut", b2 = "MI Road", b3 = "Ajmeri 
     Gate", b4 = "Jaipur"
     string_symbols_3 = c1, c2, c3; --- c1 = "Pizza Hut", c2 = "Malviya Nagar", c3 = 
     "Jaipur"

     ### On Prediction Basis ###
     I am trying to evaluate that if 60% of the symbols match with 
     another string symbols then there is probability that both strings are 
     same. 
     From above case I am considering if I am able to find >40% unique 
     symbols in both strings (that is being compared) then there is 
     probability that both strings are unique. (It will reduce the 60% 
     comparison to 40% comparison in best cases).
     Once found the unique strings return their indexes;
    */

    //pseudo implementation
    function findUniquePlaces(ap){
       //stuck here..
       //now match the splitted string arrays to find the unique places 
       //what should be the logic
       return index(0 and 2)
    }

我知道如何实现这一点。但我想知道实现它的最佳方法是什么。我想确保此任务不能是计算密集型任务。我听说过 map reduce 技术。我应该使用 map reduce 技术还是其他一些计算成本更低的技术。

【问题讨论】:

  • 试图找到指定的地方 - 怎么做?与其比较字符串,不如比较地点ID?
  • 因为 ID 已经不同但地点相同但街道名称不同。
  • 请提供Minimal, Reproducible Example,在您进行问题中提到的搜索时证明该问题。
  • 我想我已经提供了关于我想问什么的足够信息。因为我必须根据概率找到字符串匹配,但我无法做到这一点。
  • IMO 你没有。我尝试时没有重复。我们不知道您使用的是什么 API 以及如何使用。真的取决于你。

标签: javascript probability string-comparison


【解决方案1】:

这背后的一般理论是字符串度量 https://en.wikipedia.org/wiki/String_metric 计算字符串之间的距离,然后只使用现成的解决方案之一:

https://www.npmjs.com/package/fast-levenshtein

https://www.npmjs.com/package/js-levenshtein

https://www.npmjs.com/package/string-similarity

或在 google npm string distance 中查找其他任何内容

这些速度非常快,因此您的问题可能更多在于大小而不是速度。

或者你可以使用模糊搜索https://glench.github.io/fuzzyset.js/

另外,你得到的这个列表很可能已经在下面使用了这些算法,所以先拿吧?

【讨论】:

  • 正如我所说,输出仅限于前 3 个结果,因此不会消耗太多大小。但是,我有很多磁盘空间,所以不用担心。但我不想应用比较字符串的幼稚方法,因为我知道在大多数情况下,超过 0.6 的概率位置是相似的。
  • 感谢您推荐这些可用的方法。我去看看。
猜你喜欢
  • 2013-06-26
  • 2012-06-19
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2015-04-04
  • 2012-03-25
  • 1970-01-01
相关资源
最近更新 更多