]> gitweb.factorcode.org Git - factor.git/blob - extra/did-you-mean/did-you-mean.factor
mason: update table style
[factor.git] / extra / did-you-mean / did-you-mean.factor
1 ! Copyright (C) 2022 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays assocs continuations formatting kernel
5 math math.statistics parser sequences sorting spelling vocabs
6 vocabs.parser ;
7
8 IN: did-you-mean
9
10 : did-you-mean-restarts ( possibilities -- restarts )
11     natural-sort
12     [ [ [ vocabulary>> ] [ name>> ] bi "Use %s:%s" sprintf ] keep ]
13     { } map>assoc ;
14
15 : did-you-mean-restarts-with-defer ( name possibilities -- restarts )
16     did-you-mean-restarts "Defer word in current vocabulary"
17     rot 2array suffix ;
18
19 : <did-you-mean> ( name possibilities -- error restarts )
20     [ drop \ no-word-error boa ]
21     [ did-you-mean-restarts-with-defer ] 2bi ;
22
23 : did-you-mean-words ( name -- possibilities )
24     all-words [ [ name>> ] histogram-by corrections ] keep
25     [ name>> swap member? ] with filter ;
26
27 : did-you-mean ( name -- word )
28     dup did-you-mean-words <did-you-mean>
29     throw-restarts no-word-restarted ;