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
82 : vsum ( pairs -- pair ) { 0 0 } [ v+ ] reduce ;
\r
84 GENERIC: noise ( obj -- pair )
\r
86 M: word noise badness 1 2array ;
\r
88 M: wrapper noise wrapped>> noise ;
\r
90 M: let noise body>> noise ;
\r
92 M: wlet noise body>> noise ;
\r
94 M: lambda noise body>> noise ;
\r
96 M: object noise drop { 0 0 } ;
\r
98 M: quotation noise [ noise ] map vsum { 1/4 1/2 } v+ ;
\r
100 M: array noise [ noise ] map vsum ;
\r
102 : noise-factor ( x y -- z ) / 100 * >integer ;
\r
104 : quot-noise-factor ( quot -- n )
\r
105 #! For very short words, noise doesn't count so much
\r
106 #! (so dup foo swap bar isn't penalized as badly).
\r
108 { [ over 4 <= ] [ [ drop 0 ] dip ] }
\r
109 { [ over 15 >= ] [ [ 2 * ] dip ] }
\r
113 ! short words are easier to read
\r
114 { [ dup 10 <= ] [ [ 2 / ] dip ] }
\r
115 { [ dup 5 <= ] [ [ 3 / ] dip ] }
\r
116 ! long words are penalized even more
\r
117 { [ dup 25 >= ] [ [ 2 * ] dip 20 max ] }
\r
118 { [ dup 20 >= ] [ [ 5/3 * ] dip ] }
\r
119 { [ dup 15 >= ] [ [ 3/2 * ] dip ] }
\r
121 } cond noise-factor ;
\r
123 GENERIC: word-noise-factor ( word -- factor )
\r
125 M: word word-noise-factor
\r
126 def>> quot-noise-factor ;
\r
128 M: lambda-word word-noise-factor
\r
129 "lambda" word-prop quot-noise-factor ;
\r
131 : flatten-generics ( words -- words' )
\r
133 dup generic? [ "methods" word-prop values ] [ 1array ] if
\r
136 : noisy-words ( -- alist )
\r
137 all-words flatten-generics
\r
138 [ dup word-noise-factor ] { } map>assoc
\r
139 sort-values reverse ;
\r
141 : noise. ( alist -- )
\r
142 standard-table-style [
\r
144 [ [ pprint-cell ] [ pprint-cell ] bi* ] with-row
\r
148 : vocab-noise-factor ( vocab -- factor )
\r
149 words flatten-generics
\r
150 [ word-noise-factor dup 20 < [ drop 0 ] when ] map
\r
152 [ [ sum ] [ length 5 max ] bi /i ]
\r
157 : noisy-vocabs ( -- alist )
\r
158 vocabs [ dup vocab-noise-factor ] { } map>assoc
\r
159 sort-values reverse ;
\r
161 : noise-report ( -- )
\r
162 "NOISY WORDS:" print
\r
163 noisy-words 80 head noise.
\r
165 "NOISY VOCABS:" print
\r
166 noisy-vocabs 80 head noise. ;
\r