Skip to content

Instantly share code, notes, and snippets.

@BlueFlo0d
Last active Dec 18, 2021
Embed
What would you like to do?
simple flex score function for nyxt
(in-package :prompter)
(defun score-suggestion-string (input suggestion-string)
(let ((i 0)
(score 0.0))
(declare (optimize (speed 3) (safety 0))
(type single-float score) (type fixnum i)
(type (simple-array character) input suggestion-string))
(loop for c across input do
(let ((next (position c suggestion-string :start i :test 'eq)))
(if next
(progn
(incf score (/ 1.0 (1+ next)))
(setq i next))
(return))))
score))
(in-package :prompter)
(defvar word-separator '(#\ #\Tab))
(defun score-suggestion-string (input suggestion-string)
(let ((i 0)
(score 0.0))
(declare (optimize (speed 3) (safety 0))
(type single-float score) (type fixnum i)
(type (simple-array character) input suggestion-string))
(loop for c across input do
(if (member c word-separator :test 'eq)
(setq i 0)
(let ((next (position c suggestion-string :start i :test 'eq)))
(if next
(progn
(incf score (/ 1.0 (1+ next)))
(setq i next))
(return)))))
(let ((next (position-if (lambda (c) (member c word-separator :test 'eq))
suggestion-string :start i)))
(when next
(incf score (/ 1.0 (1+ next)))))
score))
(in-package :prompter)
(defvar word-separator '(#\ #\Tab))
(defun score-suggestion-string (input suggestion-string)
(let ((i 0)
(score 0.0)
(lastchar #\ ))
(declare (optimize (speed 3) (safety 0))
(type single-float score) (type fixnum i) (type character lastchar)
(type (simple-array character) input suggestion-string))
(flet ((word-end ()
;; bonus for word ending early (shorter words at the beginning wins)
(let ((next (position-if (lambda (c) (member c word-separator :test 'eq))
suggestion-string :start i)))
(unless next (setq next (length suggestion-string)))
(incf score (/ 1.0 (1+ next)))
(setq i 0))))
;; flex match, with higher weight for the beginning
(loop for c across input do
(if (member c word-separator :test 'eq)
(word-end)
(let ((next (position c suggestion-string :start i :test 'eq)))
(if next
(progn
;; bonus for continuous match
(when (and (> next 0)
(eq (aref suggestion-string (1- next)) lastchar))
(incf score 0.1))
(incf score (/ 1.0 (1+ next)))
(setq i next lastchar c))
(return)))))
(when (> i 0) (word-end)))
score))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment