similar notes tweaks

This commit is contained in:
zadam 2020-09-16 11:24:29 +02:00
parent d345c4850f
commit 5738f2a5b3

View file

@ -18,13 +18,18 @@ function gatherRewards(rewardMap, text) {
function computeScore(candidateNote, ancestorNoteIds, rewardMap, dates) { function computeScore(candidateNote, ancestorNoteIds, rewardMap, dates) {
let score = let score =
gatherRewards(rewardMap, candidateNote.title) gatherRewards(rewardMap, candidateNote.type);
+ gatherRewards(rewardMap, candidateNote.type);
+ gatherRewards(rewardMap, trimMime(candidateNote.mime)); + gatherRewards(rewardMap, trimMime(candidateNote.mime));
if (candidateNote.isDecrypted) {
score += gatherRewards(rewardMap, candidateNote.title);
}
for (const ancestorNote of candidateNote.ancestors) { for (const ancestorNote of candidateNote.ancestors) {
if (!ancestorNoteIds.includes(ancestorNote.noteId)) { if (!ancestorNoteIds.has(ancestorNote.noteId)) {
score += gatherRewards(rewardMap, ancestorNote.title); if (ancestorNote.isDecrypted) {
score += gatherRewards(rewardMap, ancestorNote.title);
}
for (const branch of ancestorNote.parentBranches) { for (const branch of ancestorNote.parentBranches) {
score += gatherRewards(rewardMap, branch.prefix); score += gatherRewards(rewardMap, branch.prefix);
@ -81,16 +86,6 @@ function evaluateSimilarity(sourceNote, candidateNote, ancestorNoteIds, rewardMa
} }
} }
/**
* Point of this is to break up long running sync process to avoid blocking
* see https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/
*/
function setImmediatePromise() {
return new Promise((resolve) => {
setTimeout(() => resolve(), 0);
});
}
const IGNORED_ATTR_NAMES = [ const IGNORED_ATTR_NAMES = [
"includenotelink", "includenotelink",
"internallink", "internallink",
@ -105,7 +100,9 @@ function buildRewardMap(note) {
const map = {}; const map = {};
for (const ancestorNote of note.ancestors) { for (const ancestorNote of note.ancestors) {
addToRewardMap(map, ancestorNote.title, 0.4); if (ancestorNote.isDecrypted) {
addToRewardMap(map, ancestorNote.title, 0.4);
}
for (const branch of ancestorNote.parentBranches) { for (const branch of ancestorNote.parentBranches) {
addToRewardMap(map, branch.prefix, 0.4); addToRewardMap(map, branch.prefix, 0.4);
@ -115,7 +112,9 @@ function buildRewardMap(note) {
addToRewardMap(map, note.type, 0.2); addToRewardMap(map, note.type, 0.2);
addToRewardMap(map, trimMime(note.mime), 0.3); addToRewardMap(map, trimMime(note.mime), 0.3);
addToRewardMap(map, note.title, 1); if (note.isDecrypted) {
addToRewardMap(map, note.title, 1);
}
for (const branch of note.parentBranches) { for (const branch of note.parentBranches) {
addToRewardMap(map, branch.prefix, 1); addToRewardMap(map, branch.prefix, 1);
@ -170,7 +169,7 @@ function addToRewardMap(map, text, baseReward) {
} }
} }
async function findSimilarNotes(noteId) { function findSimilarNotes(noteId) {const start = Date.now();
const results = []; const results = [];
let i = 0; let i = 0;
@ -190,7 +189,7 @@ async function findSimilarNotes(noteId) {
}; };
const rewardMap = buildRewardMap(baseNote); const rewardMap = buildRewardMap(baseNote);
const ancestorNoteIds = baseNote.ancestors.map(note => note.noteId); const ancestorNoteIds = new Set(baseNote.ancestors.map(note => note.noteId));
for (const candidateNote of Object.values(noteCache.notes)) { for (const candidateNote of Object.values(noteCache.notes)) {
if (candidateNote.noteId === baseNote.noteId) { if (candidateNote.noteId === baseNote.noteId) {
@ -202,15 +201,25 @@ async function findSimilarNotes(noteId) {
i++; i++;
if (i % 1000 === 0) { if (i % 1000 === 0) {
await setImmediatePromise(); //await setImmediatePromise();
} }
} }
results.sort((a, b) => a.score > b.score ? -1 : 1); results.sort((a, b) => a.score > b.score ? -1 : 1);
console.log("Similarity search took", Date.now() - start, "ms");
return results.length > 200 ? results.slice(0, 200) : results; return results.length > 200 ? results.slice(0, 200) : results;
} }
/**
* Point of this is to break up long running sync process to avoid blocking
* see https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/
*/
function setImmediatePromise() {
return new Promise((resolve) => {
setTimeout(() => resolve(), 0);
});
}
module.exports = { module.exports = {
findSimilarNotes findSimilarNotes
}; };