Explorar o código

ref!: remove fuzzy filtering

Sv443 hai 5 meses
pai
achega
9ed17dcecc
Modificáronse 5 ficheiros con 4 adicións e 121 borrados
  1. 1 1
      CHANGELOG.md
  2. 1 20
      README.md
  3. 2 8
      src/routes/search.ts
  4. 0 90
      src/songData.ts
  5. 0 2
      src/types.ts

+ 1 - 1
CHANGELOG.md

@@ -2,7 +2,7 @@
 **Features:**
 - Added `?redirect` parameter for automatic HTTP redirection instead of returning a JSON response ([#22](https://github.com/Sv443/geniURL/issues/22))
 **Changes:**
-- Removed fuzzy filtering and `?disableFuzzy` parameter altogether (to maybe be added back in the future as an opt-in feature) ([#24](https://github.com/Sv443/geniURL/issues/24))
+- Removed fuzzy filtering and `?disableFuzzy` and `?threshold` parameters altogether (to maybe be added back in the future as an opt-in feature) ([#24](https://github.com/Sv443/geniURL/issues/24))
 - Reduced ratelimit budget from 25 requests every 30 seconds to 20 requests
 
 <br>

+ 1 - 20
README.md

@@ -1,7 +1,6 @@
 # geniURL
 Simple JSON and XML REST API to search for song metadata, the lyrics URL and lyrics translations on [genius.com](https://genius.com/)  
-Authorization is not required and geniURL implements a fuzzy search that will greatly improve search results over the genius.com API.  
-Obtaining actual lyrics sadly isn't possible.  
+Authorization is not required. Obtaining actual lyrics sadly isn't possible.  
   
 Disclaimer: this project is not affiliated with or endorsed by Genius.  
   
@@ -82,15 +81,6 @@ Make sure these parameters are [percent/URL-encoded.](https://en.wikipedia.org/w
 `?format=json/xml`  
 Use this optional parameter to change the response format from the default (`json`) to `xml`  
 The structure of the XML data is similar to the shown JSON data.
-  
-`?threshold=0.65`  
-This optional parameter can be used to change the fuzzy search threshold from the default of 0.65  
-It has to be between 0.0 and 1.0; the lower the number, the less results you'll get but the more accurate the top results will be.  
-0.65 is a good middle ground but depending on your use-case you might want to play around with this.
-  
-`?disableFuzzy`  
-This optional and value-less parameter can be used to disable the fuzzy search entirely.  
-Because the current fuzzy search implementation is not as accurate as it should be, your application might need the search results as they come from the genius API.
 
 <br>
 
@@ -199,15 +189,6 @@ Make sure these parameters are [percent/URL-encoded.](https://en.wikipedia.org/w
 `?format=json/xml`  
 Use this optional parameter to change the response format from the default (`json`) to `xml`  
 The structure of the XML data is similar to the shown JSON data.
-  
-`?threshold=0.65`  
-This optional parameter can be used to change the fuzzy search threshold from the default of 0.65  
-It has to be between 0.0 and 1.0; the lower the number, the less results you'll get but the more accurate the top results will be.  
-0.65 is a good middle ground but depending on your use-case you might want to play around with this.
-  
-`?disableFuzzy`  
-This optional and value-less parameter can be used to disable the fuzzy search entirely.  
-Because the current fuzzy search implementation is not as accurate as it should be, your application might need the search results as they come from the genius API.
 
 <br>
 

+ 2 - 8
src/routes/search.ts

@@ -5,10 +5,9 @@ import { getMeta } from "../songData";
 export function initSearchRoutes(router: Router) {
   router.get("/search", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr, disableFuzzy } = req.query;
+      const { q, artist, song, format: fmt } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
-      const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
@@ -18,8 +17,6 @@ export function initSearchRoutes(router: Router) {
             artist: String(artist),
             song: String(song),
           }),
-          threshold,
-          disableFuzzy: typeof disableFuzzy === "string",
         });
 
         if(!meta || meta.all.length < 1)
@@ -40,10 +37,9 @@ export function initSearchRoutes(router: Router) {
 
   router.get("/search/top", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr, disableFuzzy } = req.query;
+      const { q, artist, song, format: fmt } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
-      const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
@@ -53,8 +49,6 @@ export function initSearchRoutes(router: Router) {
             artist: String(artist),
             song: String(song),
           }),
-          threshold,
-          disableFuzzy: typeof disableFuzzy === "string",
         });
 
         if(!meta || !meta.top)

+ 0 - 90
src/songData.ts

@@ -1,15 +1,7 @@
-/* eslint-disable no-control-regex */
-
-import Fuse from "fuse.js";
-import { nanoid } from "nanoid";
-import { clamp } from "svcorelib";
-
 import { axios, baseAxiosOpts } from "./axios";
 import { charReplacements } from "./constants";
 import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, MetaSearchHit, SongMeta, SongTranslation } from "./types";
 
-const defaultFuzzyThreshold = 0.65;
-
 /**
  * Returns meta information about the top results of a search using the genius API
  * @param param0 URL parameters - needs either a `q` prop or the props `artist` and `song`
@@ -18,8 +10,6 @@ export async function getMeta({
   q,
   artist,
   song,
-  threshold,
-  disableFuzzy,
 }: GetMetaArgs): Promise<GetMetaResult | null>
 {
   const query = q ? q : `${artist} ${song}`;
@@ -32,11 +22,6 @@ export async function getMeta({
     baseAxiosOpts()
   );
 
-  if(threshold === undefined || isNaN(threshold))
-    threshold = defaultFuzzyThreshold;
-  else
-    threshold = clamp(threshold, 0.0, 1.0);
-
   if(status >= 200 && status < 300 && Array.isArray(response?.hits)) {
     if(response.hits.length === 0)
       return null;
@@ -74,80 +59,6 @@ export async function getMeta({
         id: result.id ?? null,
       }));
 
-    if(disableFuzzy === true) {
-      if(hits.length === 0)
-        return null;
-
-      return {
-        top: hits[0]!,
-        all: hits.slice(0, 10),
-      };
-    }
-
-    const scoreMap: Record<string, number> = {};
-
-    hits = hits.map(h => {
-      h.uuid = nanoid();
-      return h;
-    }) as (SongMeta & { uuid: string })[];
-
-    const fuseOpts: Fuse.IFuseOptions<MetaSearchHit> = {
-      includeScore: true,
-      threshold,
-    };
-
-    // TODO:FIXME: this entire thing is unreliable af
-    const addScores = (searchRes: Fuse.FuseResult<SongMeta & { uuid?: string; }>[]) =>
-      searchRes.forEach(({ item, score }) => {
-        if(!item.uuid || !score)
-          return;
-
-        if(!scoreMap[item.uuid])
-          scoreMap[item.uuid] = score;
-        else if(typeof scoreMap[item.uuid] === "number") // @ts-ignore
-          scoreMap[item.uuid] += score;
-      });
-
-    if(song && artist) {
-      const titleFuse = new Fuse(hits, { ...fuseOpts, keys: [ "meta.title" ] });
-      const artistFuse = new Fuse(hits, { ...fuseOpts, keys: [ "meta.primaryArtist.name" ] });
-
-      addScores(titleFuse.search(song));
-      addScores(artistFuse.search(artist));
-    }
-    else {
-      const queryFuse = new Fuse(hits, {
-        ...fuseOpts,
-        ignoreLocation: true,
-        keys: [ "meta.title", "meta.primaryArtist.name" ],
-      });
-
-      let queryParts = [query];
-      if(query.match(/\s-\s/))
-        queryParts = query.split(/\s-\s/);
-
-      queryParts = queryParts.slice(0, 5);
-      for(const part of queryParts)
-        addScores(queryFuse.search(part.trim()));
-    }
-
-    // TODO: reduce the amount of remapping cause it takes long
-
-    const bestMatches = Object.entries(scoreMap)
-      .sort(([, valA], [, valB]) => valA > valB ? 1 : -1)
-      .map(e => e[0]);
-
-    const oldHits = [...hits];
-
-    hits = bestMatches
-      .map(uuid => oldHits.find(h => h.uuid === uuid))
-      .map(hit => {
-        if(!hit) return undefined;
-        delete hit.uuid;
-        return hit;
-      })
-      .filter(h => h !== undefined) as MetaSearchHit[];
-
     if(hits.length === 0)
       return null;
 
@@ -156,7 +67,6 @@ export async function getMeta({
       all: hits.slice(0, 10),
     };
   }
-
   return null;
 }
 

+ 0 - 2
src/types.ts

@@ -52,8 +52,6 @@ export interface GetMetaArgs {
   q?: string;
   artist?: string;
   song?: string;
-  threshold?: number;
-  disableFuzzy?: boolean;
 }
 
 export type SearchFilterArgs = GetMetaArgs;