From 878892b834c965d1edbecdf6f7a6b547673fe6a2 Mon Sep 17 00:00:00 2001 From: Arkezar Date: Sat, 21 Jul 2018 20:13:49 +0200 Subject: [PATCH 1/3] Sort universe search results with Levenshtein's function --- app/main/controller/api/universe.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/main/controller/api/universe.php b/app/main/controller/api/universe.php index e59e580b..4651463c 100644 --- a/app/main/controller/api/universe.php +++ b/app/main/controller/api/universe.php @@ -32,6 +32,16 @@ class Universe extends Controller\AccessController { !empty($categories) ){ $universeNameData = Ccp\Universe::searchUniverseNameData($categories, $search); + + foreach($categories as &$category) { + if(!empty($universeNameData[$category])){ + usort($universeNameData[$category], function($a, $b) use ($search) { + $levA = levenshtein($search, strtolower($a["name"])); + $levB = levenshtein($search, strtolower($b["name"])); + return $levA == $levB ? 0 : ($levA > $levB ? 1 : -1); + }); + } + } } echo json_encode($universeNameData); From cdd1b8d0995064c7dc42241e8a74409c279fecca Mon Sep 17 00:00:00 2001 From: Arkezar Date: Sun, 22 Jul 2018 10:25:13 +0200 Subject: [PATCH 2/3] Revert "Sort universe search results with Levenshtein's function" This reverts commit 98a523c941956f8be32a3cb4b027dddd49d1f3b7. --- app/main/controller/api/universe.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/main/controller/api/universe.php b/app/main/controller/api/universe.php index 4651463c..e59e580b 100644 --- a/app/main/controller/api/universe.php +++ b/app/main/controller/api/universe.php @@ -32,16 +32,6 @@ class Universe extends Controller\AccessController { !empty($categories) ){ $universeNameData = Ccp\Universe::searchUniverseNameData($categories, $search); - - foreach($categories as &$category) { - if(!empty($universeNameData[$category])){ - usort($universeNameData[$category], function($a, $b) use ($search) { - $levA = levenshtein($search, strtolower($a["name"])); - $levB = levenshtein($search, strtolower($b["name"])); - return $levA == $levB ? 0 : ($levA > $levB ? 1 : -1); - }); - } - } } echo json_encode($universeNameData); From 6297144a1a7d84c4cdfeba50217b74fb3d499194 Mon Sep 17 00:00:00 2001 From: Arkezar Date: Sun, 22 Jul 2018 13:21:40 +0200 Subject: [PATCH 3/3] Sort universe search results with Levenshtein's algorithm (client-side) --- js/app/ui/form_element.js | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/js/app/ui/form_element.js b/js/app/ui/form_element.js index d0226bf7..c7cb43ad 100644 --- a/js/app/ui/form_element.js +++ b/js/app/ui/form_element.js @@ -334,6 +334,46 @@ define([ return markup; } + /** + * sort universe data + * @param data universe search result array + * @param term search term + */ + function sortResultData (data, term){ + let levenshtein = function (a,b){ + let matrix = new Array(a.length+1); + for(let i = 0; i < matrix.length; i++){ + matrix[i] = new Array(b.length+1).fill(0); + } + + for(let ai = 1; ai <= a.length; ai++){ + matrix[ai][0] = ai; + } + + for(let bi = 1; bi <= b.length; bi++){ + matrix[0][bi] = bi; + } + + for(let bi = 1; bi <= b.length; bi++){ + for(let ai = 1; ai <= a.length; ai++){ + matrix[ai][bi] = Math.min( + matrix[ai-1][bi]+1, + matrix[ai][bi-1]+1, + matrix[ai-1][bi-1]+(a[ai-1] == b[bi-1] ? 0 : 1) + ); + } + } + + return matrix[a.length][b.length]; + }; + + data.sort(function(a,b){ + let levA = levenshtein(term, a.name.toLowerCase()); + let levB = levenshtein(term, b.name.toLowerCase()); + return levA === levB ? 0 : (levA > levB ? 1 : -1); + }); + } + return this.each(function() { let selectElement = $(this); @@ -370,6 +410,7 @@ define([ for(let category in result){ // skip custom functions in case result = [] (array functions) if(result.hasOwnProperty(category)){ + sortResultData(result[category], page.term); data.results.push({ text: category, children: result[category].map(mapChildren, category)