Remove some code duplication between apropos and help search

slava 2006-10-06 20:46:35 +00:00
parent fab5ec98f2
commit c87a34b164
11 changed files with 42 additions and 31 deletions

View File

@ -12,6 +12,11 @@
- minibuffer should show a title - minibuffer should show a title
- clean up listener's minibuffer-related code - clean up listener's minibuffer-related code
- help search looks funny - help search looks funny
- vocab completer
- vocab operations:
- browse
- insert IN: -- or just 'become in'
- insert USE: -- 'use'
+ ui: + ui:

View File

@ -36,12 +36,6 @@ SYMBOL: term-index
drop drop
] if* ; ] if* ;
: discard-irrelevant ( results -- newresults )
#! Discard results in the low 33%
dup 0 [ second max ] reduce
swap [ first2 rot / 2array ] map-with
[ second 1/3 > ] subset ;
: count-occurrences ( seq -- hash ) : count-occurrences ( seq -- hash )
[ [
dup [ [ drop off ] hash-each ] each dup [ [ drop off ] hash-each ] each
@ -50,9 +44,7 @@ SYMBOL: term-index
: search-help ( phrase -- assoc ) : search-help ( phrase -- assoc )
tokenize [ term-index get hash ] map [ ] subset tokenize [ term-index get hash ] map [ ] subset
count-occurrences hash>alist count-occurrences hash>alist rank-completions ;
[ first2 2array ] map
[ [ second ] 2apply swap - ] sort discard-irrelevant ;
: index-help ( -- ) : index-help ( -- )
term-index get [ term-index get [
@ -83,9 +75,6 @@ SYMBOL: term-index
over >r "help" set-word-prop r> over >r "help" set-word-prop r>
dup xref-article index-article ; dup xref-article index-article ;
: search-help. ( phrase -- )
search-help [ first ] map help-outliner ;
! Definition protocol ! Definition protocol
M: link forget link-name remove-article ; M: link forget link-name remove-article ;

View File

@ -36,7 +36,7 @@ HELP: search-help
{ $description "Performs a full-text search in the term index for help topics relating to " { $snippet "phrase" } ". The result is an association list of topic names paired with scores, sorted by decreasing score." } ; { $description "Performs a full-text search in the term index for help topics relating to " { $snippet "phrase" } ". The result is an association list of topic names paired with scores, sorted by decreasing score." } ;
HELP: index-help HELP: index-help
{ $description "Updates the full-text search term index for use by " { $link search-help } " and " { $link search-help. } "." } ; { $description "Updates the full-text search term index for use by " { $link search-help } "." } ;
HELP: search-help. HELP: search-help.
{ $values { "phrase" "a string" } } { $values { "phrase" "a string" } }

View File

@ -94,7 +94,7 @@ PROVIDE: library {
"tools/memory.factor" "tools/memory.factor"
"tools/listener.factor" "tools/listener.factor"
"tools/inspector.factor" "tools/inspector.factor"
"tools/fuzzy.factor" "tools/completion.factor"
"tools/word-tools.factor" "tools/word-tools.factor"
"tools/test.factor" "tools/test.factor"
@ -206,6 +206,7 @@ PROVIDE: library {
"test/generic.factor" "test/generic.factor"
"test/help/porter-stemmer.factor" "test/help/porter-stemmer.factor"
"test/help/topics.factor" "test/help/topics.factor"
"test/help/search.factor"
"test/inference.factor" "test/inference.factor"
"test/init.factor" "test/init.factor"
"test/inspector.factor" "test/inspector.factor"

View File

@ -0,0 +1,6 @@
IN: temporary
USING: help sequences math test ;
[ t ]
[ "variables" search-help [ second number? ] all? ]
unit-test

View File

@ -2,3 +2,4 @@ IN: temporary
USING: tools ; USING: tools ;
[ ] [ "" apropos ] unit-test [ ] [ "" apropos ] unit-test
[ ] [ "swp" apropos ] unit-test

View File

@ -1,6 +1,6 @@
! Copyright (C) 2005, 2006 Slava Pestov. ! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
IN: tools IN: completion
USING: kernel arrays sequences math namespaces strings io ; USING: kernel arrays sequences math namespaces strings io ;
! Simple fuzzy search. ! Simple fuzzy search.
@ -48,14 +48,15 @@ USING: kernel arrays sequences math namespaces strings io ;
2drop 0 2drop 0
] if ; ] if ;
: rank-completions ( seq -- seq ) : rank-completions ( results -- newresults )
[ first zero? not ] subset #! Discard results in the low 33%
[ [ first ] 2apply swap - ] sort [ [ second ] 2apply swap - ] sort
dup length 20 min head ; [ 0 [ second max ] reduce ] keep
[ second swap > ] subset-with ;
: completion ( str quot obj -- pair ) : completion ( str quot obj -- pair )
#! pair is { score obj } #! pair is { obj score }
[ swap call dup rot fuzzy score ] keep 2array ; inline [ swap call dup rot fuzzy score ] keep swap 2array ; inline
: completions ( str candidates quot -- seq ) : completions ( str candidates quot -- seq )
pick empty? [ pick empty? [
@ -64,9 +65,8 @@ USING: kernel arrays sequences math namespaces strings io ;
[ >r 2dup r> completion ] map 2nip rank-completions [ >r 2dup r> completion ] map 2nip rank-completions
] if ; inline ] if ; inline
: completion. ( score str obj -- ) : completion>string ( score str -- )
>r [ % " (score: " % >fixnum # ")" % ] "" make r> [ % " (score: " % >fixnum # ")" % ] "" make ;
write-object terpri ; inline
: string-completions ( str strs -- seq ) : string-completions ( str strs -- seq )
f swap completions ; f swap completions ;

View File

@ -3,7 +3,7 @@
IN: tools IN: tools
USING: arrays definitions hashtables help tools io kernel USING: arrays definitions hashtables help tools io kernel
math namespaces prettyprint sequences strings styles words math namespaces prettyprint sequences strings styles words
generic ; generic completion ;
: word-outliner ( seq -- ) : word-outliner ( seq -- )
natural-sort [ natural-sort [
@ -48,10 +48,11 @@ generic ;
] annotate ; ] annotate ;
: word-completion. ( pair -- ) : word-completion. ( pair -- )
first2 [ summary ] keep completion. ; first2 over summary completion>string swap write-object ;
: word-completions ( str words -- seq ) : word-completions ( str words -- seq )
[ word-name ] swap completions ; [ word-name ] swap completions ;
: apropos ( str -- ) : apropos ( str -- )
all-words word-completions [ word-completion. ] each ; all-words word-completions
[ word-completion. terpri ] each ;

View File

@ -54,5 +54,6 @@ PROVIDE: library/ui {
"test/commands.factor" "test/commands.factor"
"test/panes.factor" "test/panes.factor"
"test/editor.factor" "test/editor.factor"
"test/search.factor"
"test/tracks.factor" "test/tracks.factor"
} ; } ;

View File

@ -0,0 +1,6 @@
IN: temporary
USING: gadgets-search io test ;
[ "hey man (score: 123)" ]
[ [ { "hey man" 123 } file-completion. ] string-out ]
unit-test

View File

@ -4,7 +4,8 @@ IN: gadgets-search
USING: arrays gadgets gadgets-frames gadgets-labels USING: arrays gadgets gadgets-frames gadgets-labels
gadgets-panes gadgets-scrolling gadgets-text gadgets-theme gadgets-panes gadgets-scrolling gadgets-text gadgets-theme
generic help tools kernel models sequences words generic help tools kernel models sequences words
gadgets-borders gadgets-lists namespaces parser hashtables io ; gadgets-borders gadgets-lists namespaces parser hashtables io
completion ;
TUPLE: live-search field list model producer action presenter ; TUPLE: live-search field list model producer action presenter ;
@ -59,7 +60,7 @@ C: live-search ( string action producer presenter -- gadget )
M: live-search focusable-child* live-search-field ; M: live-search focusable-child* live-search-field ;
: <word-search> ( string action -- gadget ) : <word-search> ( string action -- gadget )
\ second add* \ first add*
all-words all-words
[ word-completions ] curry [ word-completions ] curry
[ [ word-completion. ] make-pane ] [ [ word-completion. ] make-pane ]
@ -72,10 +73,10 @@ M: live-search focusable-child* live-search-field ;
<live-search> ; <live-search> ;
: file-completion. ( pair -- ) : file-completion. ( pair -- )
first2 dup <pathname> completion. ; first2 over completion>string swap <pathname> write-object ;
: <source-files-search> ( string action -- gadget ) : <source-files-search> ( string action -- gadget )
\ second add* \ first add*
source-files get hash-keys natural-sort source-files get hash-keys natural-sort
[ string-completions ] curry [ string-completions ] curry
[ [ file-completion. ] make-pane ] [ [ file-completion. ] make-pane ]