Created
September 4, 2017 13:46
-
-
Save matze/e096322975fd5b15cd3739d5beae24ff to your computer and use it in GitHub Desktop.
Comparison of search algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import re | |
import difflib | |
store = os.path.abspath('/home/matthias/dev/password-store/') | |
def get_password_names_old(name): | |
names = [] | |
for root, dirs, files in os.walk(store): | |
dir_path = root[len(store) + 1:] | |
if dir_path.startswith('.'): | |
continue | |
for filename in files: | |
path = os.path.join(dir_path, filename) | |
if re.match(r'.*{0}.*\.gpg$'.format(name), path, re.IGNORECASE): | |
names.append(path[:-4]) | |
return names | |
def get_password_names_new(name): | |
def is_junk(x): | |
return x == ' ' | |
matcher = difflib.SequenceMatcher(isjunk=is_junk, b=name, autojunk=False) | |
matches = [] | |
for root, dirs, files in os.walk(store): | |
dir_path = root[len(store) + 1:] | |
if dir_path.startswith('.'): | |
continue | |
for filename in files: | |
path = os.path.join(dir_path, filename) | |
matcher.set_seq1(path[:-4]) | |
score = matcher.ratio() | |
if score >= 0.5: | |
matches.append((score, path[:-4])) | |
return [x[1] for x in sorted(matches, key=lambda x: x[0], reverse=True)] | |
def get_password_names_other(name): | |
matcher = difflib.SequenceMatcher(b=name, autojunk=False) | |
matches = {} | |
for root, dirs, files in os.walk(store): | |
dir_path = root[len(store) + 1:] | |
if dir_path.startswith('.'): | |
continue | |
for filename in files: | |
path = os.path.join(dir_path, filename)[:-4] | |
for name in path.split('/'): | |
matcher.set_seq1(name) | |
score = matcher.ratio() | |
if score >= 0.5 and (path not in matches or score > matches[path]): | |
matches[path] = score | |
return sorted(matches, key=matches.__getitem__, reverse=True) | |
if __name__ == '__main__': | |
print "old =", get_password_names_old(sys.argv[1]) | |
print "new =", get_password_names_new(sys.argv[1]) | |
print "oth =", get_password_names_other(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment