Skip to content

Build your personal knowledge base with Trilium Notes

similar notes algorithm tweaks (WIP)

zadam056c40c0d080
diff --git a/src/public/app/widgets/similar_notes.js b/src/public/app/widgets/similar_notes.js
index 06eb31fca9..4ef0a548d0 100644
--- a/src/public/app/widgets/similar_notes.js
+++ b/src/public/app/widgets/similar_notes.js
@@ -101,9 +101,9 @@ export default class SimilarNotesWidget extends TabAwareWidget {
}, 1000);
}
- async refreshWithNote(note) {
+ async refresh() {
// remember which title was when we found the similar notes
- this.title = note.title;
+ this.title = this.note.title;
const similarNotes = await server.get('similar-notes/' + this.noteId);
diff --git a/src/routes/api/similar_notes.js b/src/routes/api/similar_notes.js
index c71e18f5e8..dbdfa944fb 100644
--- a/src/routes/api/similar_notes.js
+++ b/src/routes/api/similar_notes.js
@@ -3,7 +3,7 @@
const noteCacheService = require('../../services/note_cache/note_cache_service');
const repository = require('../../services/repository');
-function getSimilarNotes(req) {
+async function getSimilarNotes(req) {
const noteId = req.params.noteId;
const note = repository.getNote(noteId);
@@ -12,7 +12,7 @@ function getSimilarNotes(req) {
return [404, `Note ${noteId} not found.`];
}
- const results = noteCacheService.findSimilarNotes(noteId);
+ const results = await noteCacheService.findSimilarNotes(noteId);
return results
.filter(note => note.noteId !== noteId);
diff --git a/src/services/note_cache/entities/note.js b/src/services/note_cache/entities/note.js
index a33236b3c7..7c39dd4ffd 100644
--- a/src/services/note_cache/entities/note.js
+++ b/src/services/note_cache/entities/note.js
@@ -157,7 +157,7 @@ class Note {
*/
get flatText() {
if (!this.flatTextCache) {
- this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime + ' ' + this.dateCreated.substr(0, 16) + ' ';
+ this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime + ' ';
for (const branch of this.parentBranches) {
if (branch.prefix) {
diff --git a/src/services/note_cache/note_cache_service.js b/src/services/note_cache/note_cache_service.js
index 8b563827bf..a7b1491834 100644
--- a/src/services/note_cache/note_cache_service.js
+++ b/src/services/note_cache/note_cache_service.js
@@ -5,6 +5,7 @@ const hoistedNoteService = require('../hoisted_note');
const protectedSessionService = require('../protected_session');
const stringSimilarity = require('string-similarity');
const log = require('../log');
+const dateUtils = require('../date_utils');
function isNotePathArchived(notePath) {
const noteId = notePath[notePath.length - 1];
@@ -174,8 +175,22 @@ function getNotePath(noteId) {
}
}
-function evaluateSimilarity(sourceNote, candidateNote, results) {
+function evaluateSimilarity(sourceNote, candidateNote, dates, results) {
let coeff = stringSimilarity.compareTwoStrings(sourceNote.flatText, candidateNote.flatText);
+ const {utcDateCreated} = candidateNote;
+
+ /**
+ * We want to improve standing of notes which have been created in similar time to each other since
+ * there's a good chance they are related.
+ *
+ * But there's an exception - if they were created really close to each other (withing few seconds) then
+ * they are probably part of the import and not created by hand - these OTOH should not benefit.
+ */
+ if (utcDateCreated >= dates.minDate && utcDateCreated <= dates.maxDate
+ && utcDateCreated < dates.minExcludedDate && utcDateCreated > dates.maxExcludedDate) {
+
+ coeff += 0.3;
+ }
if (coeff > 0.5) {
const notePath = getSomePath(candidateNote);
@@ -203,7 +218,7 @@ function setImmediatePromise() {
});
}
-function findSimilarNotes(noteId) {
+async function findSimilarNotes(noteId) {
const results = [];
let i = 0;
@@ -213,17 +228,28 @@ function findSimilarNotes(noteId) {
return [];
}
+ const dateCreatedTs = dateUtils.parseDateTime(origNote.utcDateCreated);
+
+ const dates = {
+ minDate: dateUtils.utcDateStr(new Date(dateCreatedTs - 1800)),
+ minExcludedDate: dateUtils.utcDateStr(new Date(dateCreatedTs - 5)),
+ maxExcludedDate: dateUtils.utcDateStr(new Date(dateCreatedTs + 5)),
+ maxDate: dateUtils.utcDateStr(new Date(dateCreatedTs + 1800)),
+ };
+
+ console.log("ORIG:", origNote.flatText);
+
for (const note of Object.values(noteCache.notes)) {
if (note.noteId === origNote.noteId) {
continue;
}
- evaluateSimilarity(origNote, note, results);
+ evaluateSimilarity(origNote, note, dates, results);
i++;
if (i % 200 === 0) {
- setImmediatePromise();
+ await setImmediatePromise();
}
}