factor/library/tools/word-tools.factor

107 lines
2.7 KiB
Factor
Raw Normal View History

2006-03-25 01:06:52 -05:00
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: tools
USING: arrays definitions hashtables help tools io kernel
math namespaces prettyprint sequences strings styles words ;
2005-03-10 17:57:22 -05:00
: word-outliner ( word quot -- )
swap natural-sort [
2006-08-25 21:29:23 -04:00
dup rot curry >r [ summary ] keep r>
write-outliner terpri
] each-with ;
: usage. ( word -- )
usage [ usage. ] word-outliner ;
2006-08-15 16:29:35 -04:00
: annotate ( word quot -- )
2005-08-23 15:50:32 -04:00
over >r >r dup word-def r> call r> swap define-compound ;
2005-04-01 12:42:14 -05:00
inline
2005-03-10 17:57:22 -05:00
2006-03-18 01:57:57 -05:00
: watch-msg ( word prefix -- ) write word-name print .s flush ;
2005-09-14 00:37:50 -04:00
2005-04-14 19:37:13 -04:00
: (watch) ( word def -- def )
2005-08-08 15:21:14 -04:00
[
2005-09-14 00:37:50 -04:00
swap literalize
dup , "===> Entering: " , \ watch-msg ,
swap %
, "===> Leaving: " , \ watch-msg ,
2005-08-25 15:27:38 -04:00
] [ ] make ;
2005-03-10 17:57:22 -05:00
2006-03-25 01:06:52 -05:00
: watch ( word -- ) [ (watch) ] annotate ;
2005-03-10 17:57:22 -05:00
2005-08-24 21:52:10 -04:00
: profile ( word -- )
2006-08-12 16:57:49 -04:00
[
swap [ global [ inc ] bind ] curry swap append
] annotate ;
: 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 ] }
2006-08-15 14:56:18 -04:00
{ [ 2dup >r 1- r> nth Letter? not ] [ 2drop 10 ] }
{ [ 2dup >r 1+ r> nth Letter? not ] [ 2drop 4 ] }
2006-08-12 16:57:49 -04:00
{ [ 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-08-18 00:56:31 -04:00
: completion ( str word -- triple )
#! triple is { score indices word }
2006-08-12 16:57:49 -04:00
[
word-name [ swap fuzzy ] keep swap [ score ] keep
] keep
3array ;
2006-08-24 00:45:58 -04:00
: completions ( str words -- seq )
[ completion ] map-with [ first zero? not ] subset
2006-08-12 17:13:36 -04:00
[ [ first ] 2apply swap - ] sort dup length 20 min head ;
2006-08-12 16:57:49 -04:00
: fuzzy. ( fuzzy full -- )
dup length [
pick member?
[ hilite-style >r ch>string r> format ] [ write1 ] if
] 2each drop ;
2006-08-24 00:45:58 -04:00
: (apropos) ( str words -- )
2006-08-12 16:57:49 -04:00
completions [
first3 dup presented associate [
2006-08-12 17:02:14 -04:00
dup word-vocabulary write bl word-name fuzzy.
" (score: " swap >fixnum number>string ")" append3
write
2006-08-12 16:57:49 -04:00
] with-nesting terpri
] each ;
2006-08-24 00:45:58 -04:00
: apropos ( str -- ) all-words (apropos) ;