From 1c93e12ada27c592e3a4e4e7c46bb54f18a94d49 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Fri, 25 Oct 2024 05:37:54 -0600 Subject: [PATCH] improve search to be next word predictive --- lib/debugprint.js | 6 ++++ lib/search.js | 70 +++++++++++++++++++++++++++++++++++++++++-- lib/searchalikes.json | 4 ++- server.js | 5 ++-- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/lib/debugprint.js b/lib/debugprint.js index 2e62965..af920ba 100644 --- a/lib/debugprint.js +++ b/lib/debugprint.js @@ -2,4 +2,10 @@ export default function debugPrint(string){ if(process.env.DEBUG == "1"){ console.log(string) } +} + +export function debugPrintDir(string){ + if(process.env.DEBUG == "1"){ + console.dir(string) + } } \ No newline at end of file diff --git a/lib/search.js b/lib/search.js index b17b304..5fb100c 100644 --- a/lib/search.js +++ b/lib/search.js @@ -123,7 +123,73 @@ export default class Searcher{ getIndexSize(){ return this.miniSearch._storedFields.size } - getSuggestions(query){ - return this.miniSearch.autoSuggest(query) + async getSuggestions(query, options){ + options.fields = ['filename', 'category'] //reduce field search + let matches = await this.findAllMatches(query, options) + let results = matches.items + console.log(results) + let suggestions = [] + for(let result = 0; result < results.length; result++){ + let currentResult = results[result] + let fileString = String(currentResult.filename).toLowerCase() + let categoryString = String(currentResult.category).toLowerCase() + let fileSplit = fileString.split(query) + let categorySplit = categoryString.split(query) + console.log(fileSplit) + console.log(categorySplit) + if(fileSplit.length > 1){ + let wordSplit = String(fileSplit[1]).split(' ') + if(!wordSplit[0]){ + wordSplit.shift() + } + console.log(wordSplit) + let prediction = '' + let prefixMatch = String(fileSplit[1]).substring(0,1) != ' ' + let prefixSpace = prefixMatch ? '' : ' ' + if(wordSplit.length > 1){ + prediction = `${prefixSpace}${wordSplit[0]} ${wordSplit[1]}` + } + else if (wordSplit.length == 1){ + prediction = `${prefixSpace}${wordSplit[0]}` + } + else { + //bad result discard + continue + } + suggestions.push(`${query}${prediction}`) + continue + } + if(categorySplit.length > 1){ + let wordSplit = String(categorySplit[1]).split(' ') + if(!wordSplit[0]){ + wordSplit.shift() + } + console.log(wordSplit) + let prediction = '' + let prefixMatch = String(categorySplit[1]).substring(0,1) != ' ' + let prefixSpace = prefixMatch ? '' : ' ' + if(wordSplit.length > 1){ + prediction = `${prefixSpace}${wordSplit[0]} ${wordSplit[1]}` + } + else if (wordSplit.length == 1){ + prediction = `${prefixSpace}${wordSplit[0]}` + } + else { + //bad result discard + continue + } + suggestions.push(`${query}${prediction}`) + continue + } + } + let dedupe = [...new Set(suggestions)] + let dedupeLimit = dedupe.length >= 10 ? 10 : dedupe.length + let arr = [] + for(let x = 0; x < dedupeLimit; x++){ + arr.push({ + suggestion: dedupe[x] + }) + } + return arr } } \ No newline at end of file diff --git a/lib/searchalikes.json b/lib/searchalikes.json index e499139..f83d3d8 100644 --- a/lib/searchalikes.json +++ b/lib/searchalikes.json @@ -32,6 +32,8 @@ ["pc88", "pc-88", "pc 88"], ["dvd", "digital video disc", "digital versatile disc"], ["mastersystem", "ms", "master system"], - ["wii", "revolution"] + ["wii", "revolution"], + ["bros", "brothers", "bros."], + ["bros.", "brothers", "bros"] ] } \ No newline at end of file diff --git a/server.js b/server.js index 004f1b8..bfca882 100644 --- a/server.js +++ b/server.js @@ -196,14 +196,15 @@ app.get("/settings", function (req, res) { res.render(indexPage, options); }); -app.post("/suggest", function(req, res){ +app.post("/suggest", async function(req, res){ if(!req.body){ return } if(typeof req.body.query == 'undefined'){ return } - let suggestions = search.getSuggestions(req.body.query) + let suggestions = await search.getSuggestions(req.body.query, defaultSettings) + debugPrint(suggestions) res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ suggestions })); })