server-ts: Port services/search/search_result

This commit is contained in:
Elian Doran 2024-02-18 00:30:16 +02:00
parent fbf77f3382
commit 80e6ced5db
No known key found for this signature in database
2 changed files with 15 additions and 7 deletions

View File

@ -1,10 +1,14 @@
"use strict"; "use strict";
const beccaService = require('../../becca/becca_service'); import beccaService = require('../../becca/becca_service');
const becca = require('../../becca/becca'); import becca = require('../../becca/becca');
class SearchResult { class SearchResult {
constructor(notePathArray) { private notePathArray: string[];
private notePathTitle: string;
private score?: number;
constructor(notePathArray: string[]) {
this.notePathArray = notePathArray; this.notePathArray = notePathArray;
this.notePathTitle = beccaService.getNoteTitleForPath(notePathArray); this.notePathTitle = beccaService.getNoteTitleForPath(notePathArray);
} }
@ -17,7 +21,7 @@ class SearchResult {
return this.notePathArray[this.notePathArray.length - 1]; return this.notePathArray[this.notePathArray.length - 1];
} }
computeScore(fulltextQuery, tokens) { computeScore(fulltextQuery: string, tokens: string[]) {
this.score = 0; this.score = 0;
const note = becca.notes[this.noteId]; const note = becca.notes[this.noteId];
@ -42,9 +46,13 @@ class SearchResult {
} }
} }
addScoreForStrings(tokens, str, factor) { addScoreForStrings(tokens: string[], str: string, factor: number) {
const chunks = str.toLowerCase().split(" "); const chunks = str.toLowerCase().split(" ");
if (!this.score) {
this.score = 0;
}
for (const chunk of chunks) { for (const chunk of chunks) {
for (const token of tokens) { for (const token of tokens) {
if (chunk === token) { if (chunk === token) {
@ -59,4 +67,4 @@ class SearchResult {
} }
} }
module.exports = SearchResult; export = SearchResult;

View File

@ -4,7 +4,7 @@ const normalizeString = require("normalize-strings");
const lex = require('./lex.js'); const lex = require('./lex.js');
const handleParens = require('./handle_parens.js'); const handleParens = require('./handle_parens.js');
const parse = require('./parse.js'); const parse = require('./parse.js');
const SearchResult = require('../search_result.js'); const SearchResult = require('../search_result');
const SearchContext = require('../search_context'); const SearchContext = require('../search_context');
const becca = require('../../../becca/becca'); const becca = require('../../../becca/becca');
const beccaService = require('../../../becca/becca_service'); const beccaService = require('../../../becca/becca_service');