【问题标题】:Text matcher on javascriptjavascript 上的文本匹配器
【发布时间】:2016-12-13 11:18:51
【问题描述】:

我需要一些JS库,可以通过长文本请求匹配类别。

例如,我有类别ApplesRed ApplesGreen ApplesOranges 和请求Red Juicy Apple 1 Kilo from Spain。在这种情况下,类别应为Red Apples。所以,简单的循环和contains() 是不够的。

我搜索了一些库,如 https://github.com/NaturalNode/natural,但分类不合适,因为在我的情况下,我不知道所有可能的请求并且无法训练它。

也许我需要停止请求,计算请求到类别的单词距离并对其进行排序?

请帮我解决这个反全文搜索。

【问题讨论】:

标签: javascript node.js


【解决方案1】:

一种非常基本的方式,但它符合您提供的单个示例:

var categories = [ "Apples", "Red Apples", "Green Apples", "Oranges" ];
var intputString = "Red Juicy Apple 1 Kilo from Spain";

var words = inputString.split(' ');

for (int wordIndex = 0; wordIndex < words.length; wordIndex++) {
  for (int categoryIndex = 0; categoryIndex < categories.length; categoryIndex++) {
    if (categories[categoryIndex].indexOf(words[wordIndex]) > -1) {
      // The word words[wordIndex] is in the string categories[categoryIndex]
    }
  }
}

【讨论】:

  • 是的,但它仍然是带有包含的循环。我刚刚找到了 lib glench.github.io/fuzzyset.js,希望对我有所帮助。感谢您的回答!
  • @user2298956 是的,你的回答比我的好很多!
【解决方案2】:

我找到了http://glench.github.io/fuzzyset.js/,它似乎有效。

>>f = FuzzySet(['Apples', 'Red Apples', 'Green Apples', 'Oranges'])
>>f.get("Red Juicy Apple 1 Kilo from Spain")

[Array[2]0: 0.3030303030303031: "Red Apples"length: 2__proto__: Array[0]]

希望不只是英语....

【讨论】:

  • 这比我的回答好!
猜你喜欢
  • 1970-01-01
  • 2012-05-01
  • 2022-01-03
  • 2021-08-27
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
相关资源
最近更新 更多