mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	similar notes algorithm tweaks (WIP)
This commit is contained in:
		| @@ -101,9 +101,9 @@ export default class SimilarNotesWidget extends TabAwareWidget { | |||||||
|         }, 1000); |         }, 1000); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async refreshWithNote(note) { |     async refresh() { | ||||||
|         // remember which title was when we found the similar notes |         // 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); |         const similarNotes = await server.get('similar-notes/' + this.noteId); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| const noteCacheService = require('../../services/note_cache/note_cache_service'); | const noteCacheService = require('../../services/note_cache/note_cache_service'); | ||||||
| const repository = require('../../services/repository'); | const repository = require('../../services/repository'); | ||||||
|  |  | ||||||
| function getSimilarNotes(req) { | async function getSimilarNotes(req) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|     const note = repository.getNote(noteId); |     const note = repository.getNote(noteId); | ||||||
| @@ -12,7 +12,7 @@ function getSimilarNotes(req) { | |||||||
|         return [404, `Note ${noteId} not found.`]; |         return [404, `Note ${noteId} not found.`]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const results = noteCacheService.findSimilarNotes(noteId); |     const results = await noteCacheService.findSimilarNotes(noteId); | ||||||
|  |  | ||||||
|     return results |     return results | ||||||
|         .filter(note => note.noteId !== noteId); |         .filter(note => note.noteId !== noteId); | ||||||
|   | |||||||
| @@ -157,7 +157,7 @@ class Note { | |||||||
|      */ |      */ | ||||||
|     get flatText() { |     get flatText() { | ||||||
|         if (!this.flatTextCache) { |         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) { |             for (const branch of this.parentBranches) { | ||||||
|                 if (branch.prefix) { |                 if (branch.prefix) { | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ const hoistedNoteService = require('../hoisted_note'); | |||||||
| const protectedSessionService = require('../protected_session'); | const protectedSessionService = require('../protected_session'); | ||||||
| const stringSimilarity = require('string-similarity'); | const stringSimilarity = require('string-similarity'); | ||||||
| const log = require('../log'); | const log = require('../log'); | ||||||
|  | const dateUtils = require('../date_utils'); | ||||||
|  |  | ||||||
| function isNotePathArchived(notePath) { | function isNotePathArchived(notePath) { | ||||||
|     const noteId = notePath[notePath.length - 1]; |     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); |     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) { |     if (coeff > 0.5) { | ||||||
|         const notePath = getSomePath(candidateNote); |         const notePath = getSomePath(candidateNote); | ||||||
| @@ -203,7 +218,7 @@ function setImmediatePromise() { | |||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function findSimilarNotes(noteId) { | async function findSimilarNotes(noteId) { | ||||||
|     const results = []; |     const results = []; | ||||||
|     let i = 0; |     let i = 0; | ||||||
|  |  | ||||||
| @@ -213,17 +228,28 @@ function findSimilarNotes(noteId) { | |||||||
|         return []; |         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)) { |     for (const note of Object.values(noteCache.notes)) { | ||||||
|         if (note.noteId === origNote.noteId) { |         if (note.noteId === origNote.noteId) { | ||||||
|             continue; |             continue; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         evaluateSimilarity(origNote, note, results); |         evaluateSimilarity(origNote, note, dates, results); | ||||||
|  |  | ||||||
|         i++; |         i++; | ||||||
|  |  | ||||||
|         if (i % 200 === 0) { |         if (i % 200 === 0) { | ||||||
|             setImmediatePromise(); |             await setImmediatePromise(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user