Skip to content

Commit

Permalink
Fix match imports and simplify fuzzy threshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
wting committed Jun 23, 2016
1 parent 218d779 commit 7375635
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/autojump
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ from autojump_utils import sanitize
from autojump_utils import take
from autojump_utils import unico

VERSION = '22.3.2'
VERSION = '22.3.3'
FUZZY_MATCH_THRESHOLD = 0.6
TAB_ENTRIES_COUNT = 9
TAB_SEPARATOR = '__'
Expand Down
16 changes: 9 additions & 7 deletions bin/autojump_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# -*- coding: utf-8 -*-
import os
import re
import sys
from difflib import SequenceMatcher

from autojump_utils import is_python3
from autojump_utils import is_windows
from autojump_utils import last

if sys.version_info[0] == 3:

if is_python3():
ifilter = filter
imap = map
os.getcwdu = os.getcwd
Expand Down Expand Up @@ -73,7 +76,7 @@ def match_consecutive(needles, haystack, ignore_case=False):
regex_no_sep_end = regex_no_sep + '$'
regex_one_sep = regex_no_sep + sep + regex_no_sep
# can't use compiled regex because of flags
regex_needle = regex_one_sep.join(map(re.escape, needles)).replace('\\', '\\\\') + regex_no_sep_end # noqa
regex_needle = regex_one_sep.join(imap(re.escape, needles)).replace('\\', '\\\\') + regex_no_sep_end # noqa
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
found = lambda entry: re.search(
regex_needle,
Expand All @@ -82,10 +85,10 @@ def match_consecutive(needles, haystack, ignore_case=False):
return ifilter(found, haystack)


def match_fuzzy(needles, haystack, ignore_case=False):
def match_fuzzy(needles, haystack, ignore_case=False, threshold=0.6):
"""
Performs an approximate match with the last needle against the end of
every path past an acceptable threshold (FUZZY_MATCH_THRESHOLD).
every path past an acceptable threshold.
For example:
needles = ['foo', 'bar']
Expand Down Expand Up @@ -115,6 +118,5 @@ def match_fuzzy(needles, haystack, ignore_case=False):
match_percent = lambda entry: SequenceMatcher(
a=needle,
b=end_dir(entry.path)).ratio()
meets_threshold = lambda entry: match_percent(entry) >= \
FUZZY_MATCH_THRESHOLD
meets_threshold = lambda entry: match_percent(entry) >= threshold
return ifilter(meets_threshold, haystack)

0 comments on commit 7375635

Please sign in to comment.