diff options
| author | Rapptz <[email protected]> | 2019-06-29 02:36:26 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-06-29 02:36:26 -0400 |
| commit | d747f2c88baeb9ba3f1eb9e1421c7a34b3ec99e5 (patch) | |
| tree | ca2857981ca089b9db219131a7fc388bb81fb618 | |
| parent | Proper RTD detection. (diff) | |
| download | discord.py-d747f2c88baeb9ba3f1eb9e1421c7a34b3ec99e5.tar.xz discord.py-d747f2c88baeb9ba3f1eb9e1421c7a34b3ec99e5.zip | |
Use a new scorer to improve search results.
This makes the other pages take less priority than the API results.
It's kind of biased, but hopefully it helps out.
| -rw-r--r-- | docs/_static/scorer.js | 78 | ||||
| -rw-r--r-- | docs/conf.py | 2 |
2 files changed, 79 insertions, 1 deletions
diff --git a/docs/_static/scorer.js b/docs/_static/scorer.js new file mode 100644 index 00000000..7e00f1d9 --- /dev/null +++ b/docs/_static/scorer.js @@ -0,0 +1,78 @@ +var _queryBeingDone = null; +var _pattern = null; +var _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 +var _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'); +} + +var 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: function(result) { + // only inflate the score of things that are actual API reference things + if(_pattern !== null && result[1].startsWith('discord.')) { + let _score = __score(result[1], _pattern); + if(_score === Number.MAX_VALUE) { + return result[4]; + } + if(_beenScored.has(result[1])) { + return 0; + } + _beenScored.add(result[1]); + let newScore = 100 + _queryBeingDone.length - _score; + // console.log(`${result[1]}: ${result[4]} -> ${newScore} (${_score})`); + return newScore; + } + return result[4]; + }, + + + // 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).ready(function() { + let params = $.getQueryParameters(); + if(params.q) { + _queryBeingDone = params.q[0]; + let pattern = Array.from(_queryBeingDone).map(escapeRegex).join('.*?'); + _pattern = new RegExp(pattern, 'i'); + } +}); diff --git a/docs/conf.py b/docs/conf.py index e5054a15..caf49022 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -234,7 +234,7 @@ html_static_path = ['_static'] # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' +html_search_scorer = '_static/scorer.js' # Output file base name for HTML help builder. htmlhelp_basename = 'discord.pydoc' |