word_freq.st en Smalltalk
Una tabla de frecuencias con un Bag, un bloque de orden y relleno.
"Count word frequencies in a passage and chart the leaders."
| passage counts ranked |
passage := 'the rain in maine falls mainly on the plain and ',
'the rain stays on the plain'.
"A Bag tallies as it collects."
counts := Bag new.
passage substrings do: [:word | counts add: word].
"Rank the distinct words: count descending, ties alphabetical."
ranked := counts asSet asSortedCollection: [:a :b |
(counts occurrencesOf: a) = (counts occurrencesOf: b)
ifTrue: [a < b]
ifFalse: [(counts occurrencesOf: a)
> (counts occurrencesOf: b)]].
Transcript show: ranked size printString, ' distinct words'; cr.
Transcript show: 'word ct bar'; cr.
ranked do: [:word |
| n |
n := counts occurrencesOf: word.
Transcript
show: (word padRightTo: 7);
show: (n printString padLeftTo: 3);
show: ' ';
show: (String new: n withAll: $#);
cr].
Cómo funciona
- Un
Bagcuenta las palabras según llegan. - El bloque de orden clasifica por conteo con empates alfabéticos.
String new: n withAll: $#dibuja cada barra.
Palabras clave y builtins usados aquí
new
El intento, en números
- Líneas
- 27
- Caracteres a escribir
- 827
- Tokens
- 135
- Ritmo de tres estrellas
- 55 tpm
Al ritmo de tres estrellas de 55 tokens por minuto, este intento toma unos 147 segundos.
Paso 1 de 3 en Bis; paso 29 de 31 en Fundamentos del lenguaje.