Skip to content

Commit

Permalink
added multiple typo support for tool searching
Browse files Browse the repository at this point in the history
  • Loading branch information
tcollins2011 authored and ahmedhamidawan committed Oct 9, 2023
1 parent 5c1af51 commit 09c51f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 15 additions & 1 deletion client/src/components/Panels/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down
13 changes: 8 additions & 5 deletions client/src/components/Panels/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09c51f1

Please sign in to comment.