Nick: improved map ranking algorithm
This commit is contained in:
@@ -2,6 +2,7 @@ import { Response } from "express";
|
|||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import {
|
import {
|
||||||
legacyCrawlerOptions,
|
legacyCrawlerOptions,
|
||||||
|
LinkInfo,
|
||||||
mapRequestSchema,
|
mapRequestSchema,
|
||||||
RequestWithAuth,
|
RequestWithAuth,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
@@ -109,6 +110,10 @@ export async function mapController(
|
|||||||
mapResults = mapResults.slice(0, minumumCutoff);
|
mapResults = mapResults.slice(0, minumumCutoff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let linkInfos: LinkInfo[] = [];
|
||||||
|
|
||||||
if (mapResults.length > 0) {
|
if (mapResults.length > 0) {
|
||||||
if (req.body.search) {
|
if (req.body.search) {
|
||||||
// Ensure all map results are first, maintaining their order
|
// Ensure all map results are first, maintaining their order
|
||||||
@@ -117,6 +122,12 @@ export async function mapController(
|
|||||||
...mapResults.slice(1).map((x) => x.url),
|
...mapResults.slice(1).map((x) => x.url),
|
||||||
...links,
|
...links,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
linkInfos = [
|
||||||
|
mapResults[0],
|
||||||
|
...mapResults.slice(1),
|
||||||
|
...links.map((x) => ({ url: x })),
|
||||||
|
]
|
||||||
} else {
|
} else {
|
||||||
mapResults.map((x) => {
|
mapResults.map((x) => {
|
||||||
links.push(x.url);
|
links.push(x.url);
|
||||||
@@ -128,7 +139,7 @@ export async function mapController(
|
|||||||
if (req.body.search) {
|
if (req.body.search) {
|
||||||
const searchQuery = req.body.search.toLowerCase();
|
const searchQuery = req.body.search.toLowerCase();
|
||||||
|
|
||||||
links = performCosineSimilarity(links, searchQuery);
|
links = performCosineSimilarity(linkInfos, searchQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
links = links
|
links = links
|
||||||
|
|||||||
@@ -478,3 +478,11 @@ export function legacyDocumentConverter(doc: any): Document {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export interface LinkInfo {
|
||||||
|
url: string;
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Logger } from "./logger";
|
import { Logger } from "./logger";
|
||||||
|
import { LinkInfo } from "../controllers/v1/types";
|
||||||
|
|
||||||
export function performCosineSimilarity(links: string[], searchQuery: string) {
|
export function performCosineSimilarity(links: LinkInfo[], searchQuery: string) {
|
||||||
try {
|
try {
|
||||||
// Function to calculate cosine similarity
|
// Function to calculate cosine similarity
|
||||||
const cosineSimilarity = (vec1: number[], vec2: number[]): number => {
|
const cosineSimilarity = (vec1: number[], vec2: number[]): number => {
|
||||||
@@ -27,20 +28,20 @@ export function performCosineSimilarity(links: string[], searchQuery: string) {
|
|||||||
|
|
||||||
// Calculate similarity scores
|
// Calculate similarity scores
|
||||||
const similarityScores = links.map((link) => {
|
const similarityScores = links.map((link) => {
|
||||||
const linkVector = textToVector(link);
|
const linkText = `${link.url} ${link.title || ''} ${link.description || ''}`.trim();
|
||||||
|
const linkVector = textToVector(linkText);
|
||||||
const searchVector = textToVector(searchQuery);
|
const searchVector = textToVector(searchQuery);
|
||||||
return cosineSimilarity(linkVector, searchVector);
|
return cosineSimilarity(linkVector, searchVector);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort links based on similarity scores and print scores
|
// Sort links based on similarity scores
|
||||||
const a = links
|
const sortedLinks = links
|
||||||
.map((link, index) => ({ link, score: similarityScores[index] }))
|
.map((link, index) => ({ link, score: similarityScores[index] }))
|
||||||
.sort((a, b) => b.score - a.score);
|
.sort((a, b) => b.score - a.score);
|
||||||
|
|
||||||
links = a.map((item) => item.link);
|
return sortedLinks.map((item) => item.link.url);
|
||||||
return links;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(`Error performing cosine similarity: ${error}`);
|
Logger.error(`Error performing cosine similarity: ${error}`);
|
||||||
return links;
|
return links.map(link => link.url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user