-
Notifications
You must be signed in to change notification settings - Fork 1
/
findBestMatches.js
81 lines (58 loc) · 2.3 KB
/
findBestMatches.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var stringSimilarity = require('string-similarity');
var sortBy = require('sort-array')
var findBestMatches = function(foodName, menuObj){
var allTypes = ['pizza', 'wings', 'sandwich', 'salad', 'pasta', 'bread'];
var foodType = '';
var menuNameArr = [];
for(var item in menuObj){
menuNameArr.push(menuObj[item].Name);
}
var allGuesses = [];
var bestGuesses = [];
var isInType = false;
var foodComponent = foodName.split(' ');
var subMenuNameArr = [];
var subMenuObj = {};
for(var i = 0; i < foodComponent.length; i++){
for(var j = 0; j < allTypes.length; j++){
if(allTypes[j].toLocaleLowerCase() === foodComponent[i].toLocaleLowerCase()){
isInType = true;
for(var item in menuObj){
if(menuObj[item].ProductType.toLocaleLowerCase() === allTypes[j].toLocaleLowerCase()){
subMenuObj[item] = menuObj[item];
subMenuNameArr.push(item);
}else if(menuObj[item].ProductType.toLocaleLowerCase() === 'gsalad' && allTypes[j].toLocaleLowerCase() === 'salad'){
subMenuObj[item] = menuObj[item];
subMenuNameArr.push(item);
}
}
}
}
}
if(isInType){
var matches = stringSimilarity.findBestMatch(foodName, subMenuNameArr);
allGuesses = matches.ratings;
sortBy(allGuesses, 'rating')
bestGuesses = allGuesses.slice(allGuesses.length-4);
var bestGuessesArr = [];
for(var i = 0; i < bestGuesses.length; i++){
bestGuessesArr.push(menuObj[bestGuesses[i].target]);
}
bestGuessesArr = bestGuessesArr.reverse();
return bestGuessesArr;
}else{
var matches = stringSimilarity.findBestMatch(foodName, MenuNameArr);
allGuesses = matches.ratings;
sortBy(allGuesses, 'rating')
bestGuesses = allGuesses.slice(allGuesses.length-5);
var bestGuessesArr = [];
for(var i = 0; i < bestGuesses.length; i++){
bestGuessesArr.push(menuObj[bestGuesses[i].target]);
}
bestGuessesArr = bestGuessesArr.reverse();
return bestGuessesArr;
}
}
module.exports = {
'findBestMatches': findBestMatches
}