Skip to content

Commit 5c482ee

Browse files
author
Bas van den Heuvel
committed
Added fuzzy matcher to hints
1 parent 18cbce8 commit 5c482ee

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

common/content/hints.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,23 @@ const Hints = Module("hints", {
833833
};
834834
} //}}}
835835

836+
function fuzzyMatcher(hintString) {
837+
expression = '';
838+
839+
for (var i = 0; i < hintString.length; i ++) {
840+
var char = hintString[i];
841+
expression += '[^' + char + ']*' + char;
842+
}
843+
844+
var re = new RegExp(expression, 'i');
845+
846+
return function(linkText) {
847+
var found = linkText.search(re) != -1;
848+
849+
return found;
850+
}
851+
}
852+
836853
let indexOf = String.indexOf;
837854
if (options.get("hintmatching").has("transliterated"))
838855
indexOf = Hints.indexOf;
@@ -841,6 +858,7 @@ const Hints = Module("hints", {
841858
case "contains" : return containsMatcher(hintString);
842859
case "wordstartswith": return wordStartsWithMatcher(hintString, /*allowWordOverleaping=*/ true);
843860
case "firstletters" : return wordStartsWithMatcher(hintString, /*allowWordOverleaping=*/ false);
861+
case "fuzzy" : return fuzzyMatcher(hintString);
844862
case "custom" : return liberator.plugins.customHintMatcher(hintString);
845863
default : liberator.echoerr("Invalid hintmatching type: " + hintMatching);
846864
}
@@ -1268,6 +1286,7 @@ const Hints = Module("hints", {
12681286
["contains", "The typed characters are split on whitespace. The resulting groups must all appear in the hint."],
12691287
["wordstartswith", "The typed characters are split on whitespace. The resulting groups must all match the beginings of words, in order."],
12701288
["firstletters", "Behaves like wordstartswith, but all groups much match a sequence of words."],
1289+
["fuzzy", "Hints are matched according to the fuzzy search algorithm."],
12711290
["custom", "Delegate to a custom function: liberator.plugins.customHintMatcher(hintString)"],
12721291
["transliterated", "When true, special latin characters are translated to their ascii equivalent (e.g., \u00e9 -> e)"]
12731292
]

common/locale/en-US/options.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@
596596
<dt>contains</dt> <dd>The typed characters are split on whitespace, and these character groups have to match anywhere inside the text of the link.</dd>
597597
<dt>wordstartswith</dt> <dd>The typed characters are matched with the beginning of the first word (see <o>wordseparators</o>) in the link as long as possible. If no matches occur in the current word, then the matching is continued at the beginning of the next word. The words are worked through in the order they appear in the link. If the typed characters contain spaces, then the characters are split on whitespace. These character groups are then matched with the beginning of the words, beginning at the first one and continuing with the following words in the order they appear in the link.</dd>
598598
<dt>firstletters</dt> <dd>Behaves like wordstartswith, but non-matching words aren't overleaped.</dd>
599+
<dt>fuzzy</dt> <dd>Hints are matched according to the fuzzy search algorithm.</dd>
599600
<dt>custom</dt> <dd>Delegate to a custom function: liberator.plugins.customHintMatcher(hintString)</dd>
600601
</dl>
601602
</description>

0 commit comments

Comments
 (0)