From 09c51f104a93c1b756eb6184292a5ed7f8853ff0 Mon Sep 17 00:00:00 2001 From: tcollins Date: Fri, 6 Oct 2023 15:11:52 -0400 Subject: [PATCH] added multiple typo support for tool searching --- client/src/components/Panels/utilities.test.js | 16 +++++++++++++++- client/src/components/Panels/utilities.ts | 13 ++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/client/src/components/Panels/utilities.test.js b/client/src/components/Panels/utilities.test.js index 33970938dfed..40e9386dac34 100644 --- a/client/src/components/Panels/utilities.test.js +++ b/client/src/components/Panels/utilities.test.js @@ -166,6 +166,7 @@ describe("test helpers in tool searching utilities", () => { it("test tool fuzzy search", async () => { const expectedResults = ["__FILTER_FAILED_DATASETS__", "__FILTER_EMPTY_DATASETS__"]; const keys = { description: 1, name: 2, combined: 0 }; + // Testing if just names work with DL search const filterQueries = ["Fillter", "FILYER", " Fitler", " filtr"]; filterQueries.forEach((q) => { const { results, closestTerm } = searchToolsByKeys( @@ -178,7 +179,20 @@ describe("test helpers in tool searching utilities", () => { expect(results).toEqual(expectedResults); expect(closestTerm).toEqual("filter"); }); - const queries = ["datases from a collection", "from a colleection"]; + // Testing if names and description function with DL search + let queries = ["datases from a collection", "from a colleection", "from a colleection"]; + queries.forEach((q) => { + const { results } = searchToolsByKeys( + Object.values(toolsList.tools), + keys, + q, + "default", + toolsListInPanel.default + ); + expect(results).toEqual(expectedResults); + }); + // Testing if different length queries correctly trigger changes in max DL distance + queries = ["datae", "ppasetsfrom", "datass from a cppollection"]; queries.forEach((q) => { const { results } = searchToolsByKeys( Object.values(toolsList.tools), diff --git a/client/src/components/Panels/utilities.ts b/client/src/components/Panels/utilities.ts index 77c22820eb87..6c2c4713d296 100644 --- a/client/src/components/Panels/utilities.ts +++ b/client/src/components/Panels/utilities.ts @@ -443,22 +443,25 @@ function getPanelSectionsForTool(tool: Tool, panelView: string) { * @returns substring with smallest DL distance, or null */ function closestSubstring(query: string, actualStr: string) { + // Create max distance // Max distance a query and substring can be apart - const maxDistance = 1; + const maxDistance = Math.floor(query.length / 5); // Create an array of all actualStr substrings that are query length, query length -1, and query length + 1 - const substrings = Array.from({ length: actualStr.length - query.length + 1 }, (_, i) => + const substrings = Array.from({ length: actualStr.length - query.length + maxDistance }, (_, i) => actualStr.substr(i, query.length) ); if (query.length > 1) { substrings.push( - ...Array.from({ length: actualStr.length - query.length + 2 }, (_, i) => - actualStr.substr(i, query.length - 1) + ...Array.from({ length: actualStr.length - query.length + maxDistance + 1 }, (_, i) => + actualStr.substr(i, query.length - maxDistance) ) ); } if (actualStr.length > query.length) { substrings.push( - ...Array.from({ length: actualStr.length - query.length }, (_, i) => actualStr.substr(i, query.length + 1)) + ...Array.from({ length: actualStr.length - query.length }, (_, i) => + actualStr.substr(i, query.length + maxDistance) + ) ); } // check to see if any substrings have a levenshtein distance less than the max distance