1 ! Copyright (C) 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: accessors assocs math kernel shuffle generalizations
\r
4 words quotations arrays combinators sequences math.vectors
\r
5 io.styles prettyprint vocabs sorting io generic
\r
6 math.statistics math.order locals.types
\r
10 : badness ( word -- n )
\r
74 : vsum ( pairs -- pair ) { 0 0 } [ v+ ] reduce ;
\r
76 GENERIC: noise ( obj -- pair )
\r
78 M: word noise badness 1 2array ;
\r
80 M: wrapper noise wrapped>> noise ;
\r
82 M: let noise body>> noise ;
\r
84 M: wlet noise body>> noise ;
\r
86 M: lambda noise body>> noise ;
\r
88 M: object noise drop { 0 0 } ;
\r
90 M: quotation noise [ noise ] map vsum { 1/4 1/2 } v+ ;
\r
92 M: array noise [ noise ] map vsum ;
\r
94 : noise-factor ( x y -- z ) / 100 * >integer ;
\r
96 : quot-noise-factor ( quot -- n )
\r
97 #! For very short words, noise doesn't count so much
\r
98 #! (so dup foo swap bar isn't penalized as badly).
\r
100 { [ over 4 <= ] [ [ drop 0 ] dip ] }
\r
101 { [ over 15 >= ] [ [ 2 * ] dip ] }
\r
105 ! short words are easier to read
\r
106 { [ dup 10 <= ] [ [ 2 / ] dip ] }
\r
107 { [ dup 5 <= ] [ [ 3 / ] dip ] }
\r
108 ! long words are penalized even more
\r
109 { [ dup 25 >= ] [ [ 2 * ] dip 20 max ] }
\r
110 { [ dup 20 >= ] [ [ 5/3 * ] dip ] }
\r
111 { [ dup 15 >= ] [ [ 3/2 * ] dip ] }
\r
113 } cond noise-factor ;
\r
115 GENERIC: word-noise-factor ( word -- factor )
\r
117 M: word word-noise-factor
\r
118 def>> quot-noise-factor ;
\r
120 M: lambda-word word-noise-factor
\r
121 "lambda" word-prop quot-noise-factor ;
\r
123 : flatten-generics ( words -- words' )
\r
125 dup generic? [ "methods" word-prop values ] [ 1array ] if
\r
128 : noisy-words ( -- alist )
\r
129 all-words flatten-generics
\r
130 [ dup word-noise-factor ] { } map>assoc
\r
131 sort-values reverse ;
\r
133 : noise. ( alist -- )
\r
134 standard-table-style [
\r
136 [ [ pprint-cell ] [ pprint-cell ] bi* ] with-row
\r
140 : vocab-noise-factor ( vocab -- factor )
\r
141 words flatten-generics
\r
142 [ word-noise-factor dup 20 < [ drop 0 ] when ] map
\r
144 [ [ sum ] [ length 5 max ] bi /i ]
\r
149 : noisy-vocabs ( -- alist )
\r
150 vocabs [ dup vocab-noise-factor ] { } map>assoc
\r
151 sort-values reverse ;
\r
153 : noise-report ( -- )
\r
154 "NOISY WORDS:" print
\r
155 noisy-words 80 head noise.
\r
157 "NOISY VOCABS:" print
\r
158 noisy-vocabs 80 head noise. ;
\r