|
| 1 | +from __future__ import unicode_literals |
| 2 | +import re |
| 3 | + |
| 4 | +from collections import namedtuple |
| 5 | +from six import string_types |
| 6 | + |
| 7 | +from prompt_toolkit.completion import Completer, Completion |
| 8 | + |
| 9 | +__all__ = [ |
| 10 | + 'FuzzyWordCompleter', |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | +class FuzzyWordCompleter(Completer): |
| 15 | + """ |
| 16 | + Fuzzy completion on a list of words. |
| 17 | +
|
| 18 | + If the list of words is: ["leopard" , "gorilla", "dinosaur", "cat", "bee"] |
| 19 | + Then trying to complete "oar" would yield "leopard" and "dinosaur", but not |
| 20 | + the others, because they match the regular expression 'o.*a.*r'. |
| 21 | +
|
| 22 | + The results are sorted by relevance, which is defined as the start position |
| 23 | + of the match and then the proportion of the word span that is covered. As a |
| 24 | + user, if you want to get leopard, it's better to type 'ld' (first + last |
| 25 | + letter) because this covers 100% of the word. |
| 26 | +
|
| 27 | + See: https://blog.amjith.com/fuzzyfinder-in-10-lines-of-python |
| 28 | +
|
| 29 | + :param words: List of words or callable that returns a list of words. |
| 30 | + :param meta_dict: Optional dict mapping words to their meta-information. |
| 31 | + :param WORD: When True, use WORD characters. |
| 32 | + :param sort_results: Boolean to determine whether to sort the results (default: True). |
| 33 | +
|
| 34 | + Fuzzy algorithm is based on this post: https://blog.amjith.com/fuzzyfinder-in-10-lines-of-python |
| 35 | + """ |
| 36 | + def __init__(self, words, meta_dict=None, WORD=False, sort_results=True): |
| 37 | + assert callable(words) or all(isinstance(w, string_types) for w in words) |
| 38 | + |
| 39 | + self.words = words |
| 40 | + self.meta_dict = meta_dict or {} |
| 41 | + self.sort_results = sort_results |
| 42 | + self.WORD = WORD |
| 43 | + |
| 44 | + def get_completions(self, document, complete_event): |
| 45 | + # Get list of words. |
| 46 | + words = self.words |
| 47 | + if callable(words): |
| 48 | + words = words() |
| 49 | + |
| 50 | + word_before_cursor = document.get_word_before_cursor(WORD=self.WORD) |
| 51 | + |
| 52 | + fuzzy_matches = [] |
| 53 | + pat = '.*?'.join(map(re.escape, word_before_cursor)) |
| 54 | + pat = '(?=({0}))'.format(pat) # lookahead regex to manage overlapping matches |
| 55 | + regex = re.compile(pat, re.IGNORECASE) |
| 56 | + for word in words: |
| 57 | + matches = list(regex.finditer(word)) |
| 58 | + if matches: |
| 59 | + best = min(matches, key=lambda x: len(x.group(1))) # find shortest match |
| 60 | + fuzzy_matches.append(_FuzzyMatch(len(best.group(1)), best.start(), word)) |
| 61 | + |
| 62 | + def sort_key(fuzzy_match): |
| 63 | + " Sort by start position, then by proportion of word that is covered. " |
| 64 | + return ( |
| 65 | + fuzzy_match.start_pos, |
| 66 | + float(fuzzy_match.match_length) / len(fuzzy_match.word) |
| 67 | + ) |
| 68 | + |
| 69 | + fuzzy_matches = sorted(fuzzy_matches, key=sort_key) |
| 70 | + |
| 71 | + for match in fuzzy_matches: |
| 72 | + display_meta = self.meta_dict.get(match.word, '') |
| 73 | + |
| 74 | + yield Completion( |
| 75 | + match.word, |
| 76 | + -len(word_before_cursor), |
| 77 | + display_meta=display_meta) |
| 78 | + |
| 79 | + |
| 80 | +_FuzzyMatch = namedtuple('_FuzzyMatch', 'match_length start_pos word') |
0 commit comments