sourcelibs/Strings/demos/word-count.xtl
1⍝!/usr/bin/env xetal
2⍝# Word count: the words of a text, how often each occurs, the most
3⍝# frequent first. Run with "just demo Strings word-count".
4
5ᵗ⁼u̲se< "Strings"
6text ← "The cat sat on the mat. The dog sat on the log; the cat ran."
7
8⍝ Lower case, punctuation to spaces, then the words.
9clean ← ᵗl̲ower "." " " ᵗr̲eplace ";" " " ᵗr̲eplace text
10words ← ᵗw̲ords clean
11t̲ally words
12
13⍝ Each distinct word and its count, most frequent first.
14distinct ← u̲nique words
15counts ← '{ t̲ally w̲here words = ⍵ } e̲ach distinct
16order ← g̲rade n̲eg counts ⍝ ties keep their first-seen order
17"\n" ᵗj̲oin '{ i → (10 ᵗp̲adRight d̲isclose i s̲elect distinct) c̲at f̲ormat i s̲elect counts } m̲ap 5 t̲ake order