-1

I need some JS library, which could match category by long text request.

For example, I have categories Apples, Red Apples, Green Apples, Oranges and request Red Juicy Apple 1 Kilo from Spain. In this case the category should be Red Apples. So, simple loop and contains() is not enough.

I've searched some libs like https://github.com/NaturalNode/natural, but classification not suitable because in my case I do not know all possible requests and could not train it.

Maybe I need to stem request, calculate words distances request to categories and sort it?

Please help me with this anti-full-text-search.

user2298956
  • 75
  • 2
  • 10

2 Answers2

1

A very rudimentary way, but it meets your single provided example:

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]
    }
  }
}
Nick Bull
  • 9,518
  • 6
  • 36
  • 58
1

I have found http://glench.github.io/fuzzyset.js/ and it seems to work.

>>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]]

Hope it suppose not only English....

user2298956
  • 75
  • 2
  • 10