Skip to content

Commit

Permalink
:SearchNotes completion now sorts by Levenshtein distance
Browse files Browse the repository at this point in the history
TODO Document this: sudo apt-get install python-levenshtein
  • Loading branch information
xolox committed Nov 22, 2011
1 parent 6993951 commit 6723f90
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
6 changes: 3 additions & 3 deletions autoload/xolox/notes.vim
@@ -1,12 +1,12 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: November 22, 2011
" Last Change: November 23, 2011
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.

let g:xolox#notes#version = '0.14.1'
let g:xolox#notes#version = '0.14.2'

function! xolox#notes#shortcut() " {{{1
" The "note:" pseudo protocol is just a shortcut for the :Note command.
Expand Down Expand Up @@ -551,7 +551,7 @@ function! s:run_scanner(keywords, matches) " {{{2
endfunction

function! xolox#notes#keyword_complete(arglead, cmdline, cursorpos) " {{{2
return s:python_command('--list=' . a:arglead)
return split(s:python_command('--list=' . a:arglead), '\n')
endfunction

function! s:python_command(...) " {{{2
Expand Down
38 changes: 28 additions & 10 deletions misc/notes/search-notes.py
Expand Up @@ -20,6 +20,12 @@
import re
import sys

try:
import Levenshtein
levenshtein_supported = True
except ImportError:
levenshtein_supported = False

class NotesIndex:

def __init__(self):
Expand All @@ -33,7 +39,7 @@ def __init__(self):
self.list_keywords(self.keyword_filter)
else:
matches = self.search_index(keywords)
print '\n'.join(sorted(matches))
print self.encode('\n'.join(sorted(matches)))

def parse_args(self):
''' Parse the command line arguments. '''
Expand Down Expand Up @@ -64,6 +70,8 @@ def parse_args(self):
sys.exit(0)
else:
assert False, "Unhandled option"
if self.keyword_filter is not None:
self.keyword_filter = self.decode(self.keyword_filter)
# Canonicalize pathnames, check validity.
self.database_file = self.munge_path(self.database_file)
self.user_directory = self.munge_path(self.user_directory)
Expand Down Expand Up @@ -147,27 +155,37 @@ def search_index(self, keywords):
matches &= set(filenames)
return list(matches) if matches else []

def list_keywords(self, substring, limit=100):
def list_keywords(self, substring, limit=25):
''' Print all (matching) keywords to standard output. '''
i = 0
for kw in self.index['keywords']:
decorated = []
for kw, filenames in self.index['keywords'].iteritems():
if substring in kw.lower():
print kw
if i < limit:
i += 1
if levenshtein_supported:
decorated.append((Levenshtein.distance(kw.lower(), substring), -len(filenames), kw))
else:
break
decorated.append((-len(filenames), kw))
decorated.sort()
selection = [d[-1] for d in decorated[:limit]]
print self.encode('\n'.join(selection))

def tokenize(self, text):
''' Tokenize a string into a list of normalized, unique keywords. '''
words = set()
text = text.decode(self.character_encoding, 'ignore')
for word in re.findall(r'\w+', text.lower(), re.UNICODE):
text = self.decode(text).lower()
for word in re.findall(r'\w+', text, re.UNICODE):
word = word.strip()
if word != '' and not word.isspace():
words.add(word)
return words

def encode(self, text):
''' Encode a string in the user's preferred character encoding. '''
return text.encode(self.character_encoding, 'ignore')

def decode(self, text):
''' Decode a string in the user's preferred character encoding. '''
return text.decode(self.character_encoding, 'ignore')

def munge_path(self, path):
''' Canonicalize user-defined path, making it absolute. '''
return os.path.abspath(os.path.expanduser(path))
Expand Down
4 changes: 2 additions & 2 deletions plugin/notes.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: November 22, 2011
" Last Change: November 23, 2011
" URL: http://peterodding.com/code/vim/notes/

" Support for automatic update using the GLVS plug-in.
Expand Down Expand Up @@ -59,7 +59,7 @@ endif
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete DeleteNote call xolox#notes#delete(<q-bang>, <q-args>)
command! -bang -nargs=? -complete=custom,xolox#notes#keyword_complete SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bang -nargs=? -complete=customlist,xolox#notes#keyword_complete SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
command! -bar -bang -nargs=? RecentNotes call xolox#notes#recent(<q-bang>, <q-args>)
command! -bar -count=1 ShowTaggedNotes call xolox#notes#tags#show_tags(<count>)
Expand Down

0 comments on commit 6723f90

Please sign in to comment.