File: scorer.js

package info (click to toggle)
python-discord 2.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,180 kB
  • sloc: python: 46,013; javascript: 363; makefile: 154
file content (83 lines) | stat: -rw-r--r-- 2,506 bytes parent folder | download | duplicates (3)
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
'use-strict';

let queryBeingDone = null;
let pattern = null;

const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
function escapeRegex(e) {
    return e.replace(escapedRegex, '\\$&');
}

// for some reason Sphinx shows some entries twice
// if something has been scored already I'd rather sort it to the bottom
const beenScored = new Set();

function __score(haystack, regex) {
    let match = regex.exec(haystack);
    if (match == null) {
        return Number.MAX_VALUE;
    }
    let subLength = match[0].length;
    let start = match.index;
    return (subLength * 1000 + start) / 1000.0;
}

// unused for now
function __cleanNamespaces(query) {
    return query.replace(/(discord\.(ext\.)?)?(.+)/, '$3');
}

Scorer = {

    // Implement the following function to further tweak the score for each result
    // The function takes a result array [filename, title, anchor, descr, score]
    // and returns the new score.
    score: (result) => {
        // only inflate the score of things that are actual API reference things
        const [, title, , , score] = result;

        if (pattern !== null && title.startsWith('discord.')) {
            let _score = __score(title, pattern);
            if (_score === Number.MAX_VALUE) {
                return score;
            }
            if (beenScored.has(title)) {
                return 0;
            }
            beenScored.add(title);
            let newScore = 100 + queryBeingDone.length - _score;
            // console.log(`${title}: ${score} -> ${newScore} (${_score})`);
            return newScore;
        }
        return score;
    },

    // query matches the full name of an object
    objNameMatch: 15,
    // or matches in the last dotted part of the object name
    objPartialMatch: 11,
    // Additive scores depending on the priority of the object
    objPrio: {
        0: 15,  // used to be importantResults
        1: 7,   // used to be objectResults
        2: -5   // used to be unimportantResults
    },
    //  Used when the priority is not in the mapping.
    objPrioDefault: 0,

    // query found in title
    title: 15,
    partialTitle: 7,
    // query found in terms
    term: 5,
    partialTerm: 2
};

document.addEventListener('DOMContentLoaded', () => {
    const params = new URLSearchParams(window.location.search);
    queryBeingDone = params.get('q');
    if (queryBeingDone) {
        let pattern = Array.from(queryBeingDone).map(escapeRegex).join('.*?');
        pattern = new RegExp(pattern, 'i');
    }
});