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
77 : vsum ( pairs -- pair ) { 0 0 } [ v+ ] reduce ;
\r
79 GENERIC: noise ( obj -- pair )
\r
81 M: word noise badness 1 2array ;
\r
83 M: wrapper noise wrapped>> noise ;
\r
85 M: let noise body>> noise ;
\r
87 M: wlet noise body>> noise ;
\r
89 M: lambda noise body>> noise ;
\r
91 M: object noise drop { 0 0 } ;
\r
93 M: quotation noise [ noise ] map vsum { 1/4 1/2 } v+ ;
\r
95 M: array noise [ noise ] map vsum ;
\r
97 : noise-factor ( x y -- z ) / 100 * >integer ;
\r
99 : quot-noise-factor ( quot -- n )
\r
100 #! For very short words, noise doesn't count so much
\r
101 #! (so dup foo swap bar isn't penalized as badly).
\r
103 { [ over 4 <= ] [ [ drop 0 ] dip ] }
\r
104 { [ over 15 >= ] [ [ 2 * ] dip ] }
\r
108 ! short words are easier to read
\r
109 { [ dup 10 <= ] [ [ 2 / ] dip ] }
\r
110 { [ dup 5 <= ] [ [ 3 / ] dip ] }
\r
111 ! long words are penalized even more
\r
112 { [ dup 25 >= ] [ [ 2 * ] dip 20 max ] }
\r
113 { [ dup 20 >= ] [ [ 5/3 * ] dip ] }
\r
114 { [ dup 15 >= ] [ [ 3/2 * ] dip ] }
\r
116 } cond noise-factor ;
\r
118 GENERIC: word-noise-factor ( word -- factor )
\r
120 M: word word-noise-factor
\r
121 def>> quot-noise-factor ;
\r
123 M: lambda-word word-noise-factor
\r
124 "lambda" word-prop quot-noise-factor ;
\r
126 : flatten-generics ( words -- words' )
\r
128 dup generic? [ "methods" word-prop values ] [ 1array ] if
\r
131 : noisy-words ( -- alist )
\r
132 all-words flatten-generics
\r
133 [ dup word-noise-factor ] { } map>assoc
\r
134 sort-values reverse ;
\r
136 : noise. ( alist -- )
\r
137 standard-table-style [
\r
139 [ [ pprint-cell ] [ pprint-cell ] bi* ] with-row
\r
143 : vocab-noise-factor ( vocab -- factor )
\r
144 words flatten-generics
\r
145 [ word-noise-factor dup 20 < [ drop 0 ] when ] map
\r
147 [ [ sum ] [ length 5 max ] bi /i ]
\r
152 : noisy-vocabs ( -- alist )
\r
153 vocabs [ dup vocab-noise-factor ] { } map>assoc
\r
154 sort-values reverse ;
\r
156 : noise-report ( -- )
\r
157 "NOISY WORDS:" print
\r
158 noisy-words 80 head noise.
\r
160 "NOISY VOCABS:" print
\r
161 noisy-vocabs 80 head noise. ;
\r