File size: 2,767 Bytes
f2bee8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Copyright (C) 2021 Thomas Weber
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

const normalize = text => text
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .toLowerCase()
    .replace(/['"()\-+,./[\]]/g, ' ')
    .trim();

const splitToWords = text => normalize(text)
    .split(' ')
    .filter(i => i);

const parseTexts = texts => {
    const result = [];
    for (const {score, text} of texts) {
        result.push({
            score,
            words: splitToWords(text)
        });
    }
    return result;
};

class Search {
    constructor (texts) {
        this.items = texts.map(parseTexts);
    }

    search (query) {
        const terms = splitToWords(query);
        const result = [];
        const processItem = item => {
            let totalScore = 0;
            for (const term of terms) {
                let highestScoreForTerm = 0;
                for (const group of item) {
                    for (const word of group.words) {
                        const wordIndex = word.indexOf(term);
                        if (wordIndex !== -1) {
                            let multiplier;
                            if (wordIndex === 0) {
                                multiplier = 1.5;
                            } else {
                                multiplier = 1;
                            }
                            const itemScore = group.score * multiplier;
                            if (itemScore > highestScoreForTerm) {
                                highestScoreForTerm = itemScore;
                            }
                        }
                    }
                }
                if (highestScoreForTerm === 0) {
                    return;
                }
                totalScore += highestScoreForTerm;
            }
            return totalScore;
        };
        for (let i = 0; i < this.items.length; i++) {
            const score = processItem(this.items[i]);
            if (score > 0) {
                result.push({
                    index: i,
                    score
                });
            }
        }
        result.sort((a, b) => b.score - a.score);
        return result;
    }
}

export default Search;