programlibs/Strings/demos/word-count.xtl

Word count: the words of a text, how often each occurs, the most frequent first. Run with "just demo Strings word-count".

source · imports t: libs/Strings/src/Strings.xtl

text : Char

value · line 6
text ← "The cat sat on the mat. The dog sat on the log; the cat ran."
Used in: clean

clean : Char

value · line 9
clean ← ᵗl̲ower "." " " ᵗr̲eplace ";" " " ᵗr̲eplace text
Used in: words

words : Box Char

value · line 10
words ← ᵗw̲ords clean

distinct : Box Char

value · line 14
distinct ← u̲nique words

counts : Int

value · line 15
counts ← '{ t̲ally w̲here words = ⍵ } e̲ach distinct

order : Int

value · line 16
order ← g̲rade n̲eg counts                     ⍝ ties keep their first-seen order