2006-10-06 04:15:34 -04:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-10-06 16:46:35 -04:00
|
|
|
IN: completion
|
2006-10-06 04:15:34 -04:00
|
|
|
USING: kernel arrays sequences math namespaces strings io ;
|
|
|
|
|
|
|
|
|
|
! Simple fuzzy search.
|
|
|
|
|
|
|
|
|
|
: fuzzy ( full short -- indices )
|
|
|
|
|
0 swap >array [ swap pick index* [ 1+ ] keep ] map 2nip
|
|
|
|
|
-1 over member? [ drop f ] when ;
|
|
|
|
|
|
|
|
|
|
: (runs) ( n i seq -- )
|
|
|
|
|
2dup length < [
|
|
|
|
|
3dup nth [
|
|
|
|
|
number= [
|
|
|
|
|
>r >r 1+ r> r>
|
|
|
|
|
] [
|
|
|
|
|
split-next,
|
|
|
|
|
rot drop [ nth 1+ ] 2keep
|
|
|
|
|
] if >r 1+ r>
|
|
|
|
|
] keep split, (runs)
|
|
|
|
|
] [
|
|
|
|
|
3drop
|
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
|
|
: runs ( seq -- seq )
|
|
|
|
|
[
|
|
|
|
|
split-next,
|
|
|
|
|
dup first 0 rot (runs)
|
|
|
|
|
] { } make ;
|
|
|
|
|
|
|
|
|
|
: score-1 ( i full -- n )
|
|
|
|
|
{
|
|
|
|
|
{ [ over zero? ] [ 2drop 10 ] }
|
|
|
|
|
{ [ 2dup length 1- = ] [ 2drop 4 ] }
|
|
|
|
|
{ [ 2dup >r 1- r> nth Letter? not ] [ 2drop 10 ] }
|
|
|
|
|
{ [ 2dup >r 1+ r> nth Letter? not ] [ 2drop 4 ] }
|
|
|
|
|
{ [ t ] [ 2drop 1 ] }
|
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
|
|
: score ( full fuzzy -- n )
|
|
|
|
|
dup [
|
|
|
|
|
[ [ length ] 2apply - 15 swap [-] 3 / ] 2keep
|
|
|
|
|
runs [
|
|
|
|
|
[ swap score-1 ] map-with dup supremum swap length *
|
|
|
|
|
] map-with sum +
|
|
|
|
|
] [
|
|
|
|
|
2drop 0
|
|
|
|
|
] if ;
|
|
|
|
|
|
2006-10-06 16:46:35 -04:00
|
|
|
: rank-completions ( results -- newresults )
|
|
|
|
|
#! Discard results in the low 33%
|
2006-12-07 22:53:50 -05:00
|
|
|
sort-keys <reversed>
|
2006-11-17 01:40:23 -05:00
|
|
|
[ 0 [ first max ] reduce 3 / ] keep
|
|
|
|
|
[ first < ] subset-with
|
|
|
|
|
[ second ] map ;
|
2006-10-06 04:15:34 -04:00
|
|
|
|
2006-11-21 21:27:39 -05:00
|
|
|
: complete ( full short -- score )
|
|
|
|
|
#! Match forwards and backwards, see which one has the
|
|
|
|
|
#! highest score.
|
|
|
|
|
[ dupd fuzzy score ] 2keep
|
|
|
|
|
[ <reversed> ] 2apply
|
|
|
|
|
dupd fuzzy score max ;
|
|
|
|
|
|
2006-10-06 04:15:34 -04:00
|
|
|
: completion ( str quot obj -- pair )
|
2006-10-06 16:46:35 -04:00
|
|
|
#! pair is { obj score }
|
2006-11-21 21:27:39 -05:00
|
|
|
[ swap call swap complete ] keep 2array ; inline
|
2006-10-06 04:15:34 -04:00
|
|
|
|
2006-11-13 00:16:22 -05:00
|
|
|
: completions ( str quot candidates -- seq )
|
2006-11-14 02:22:54 -05:00
|
|
|
pick empty? [
|
2006-11-18 03:51:34 -05:00
|
|
|
dup length 1000 > [
|
|
|
|
|
3drop f
|
|
|
|
|
] [
|
|
|
|
|
2nip
|
|
|
|
|
] if
|
2006-10-06 04:15:34 -04:00
|
|
|
] [
|
|
|
|
|
[ >r 2dup r> completion ] map 2nip rank-completions
|
|
|
|
|
] if ; inline
|
|
|
|
|
|
|
|
|
|
: string-completions ( str strs -- seq )
|
|
|
|
|
f swap completions ;
|