soma.cell

Word frequency counting with a Map<String, Int> — the canonical hash-map histogram

Demonstrates: map bracket read/write, the ?? null-coalescing guard for first-seen keys, .has / .get / .set on a memory slot, and finding the most frequent word.

A Soma program from the algorithms part of the verified corpus: 54 lines, cells WordCount, WordCountTests. It passes soma check with soma 3.2.4.

How it is built

The cell WordCount declares 3 signals in its face: count(text: String) -> Map, most_common(text: String) -> String and distinct_words(text: String) -> Int. Handlers: count (line 16), most_common (line 26) and distinct_words (line 39).

The file also holds 1 test cell (WordCountTests) with 6 assertions, run by soma test.

Generated from soma describe on this file.

The source

word_count.cell

// Word frequency counting with a Map<String, Int> — the canonical hash-map
// histogram. Demonstrates: map bracket read/write, the ?? null-coalescing
// guard for first-seen keys, .has / .get / .set on a memory slot, and finding
// the most frequent word.
//
//   soma run  examples/corpus/algorithms/word_count.cell count "the cat sat on the mat the cat"
//   soma test examples/corpus/algorithms/word_count.cell

cell WordCount {
    face {
        signal count(text: String) -> Map
        signal most_common(text: String) -> String
        signal distinct_words(text: String) -> Int
    }

    on count(text: String) {
        let counts = map()
        for w in split(trim(text), " ") {
            if w != "" {
                counts[w] = (counts[w] ?? 0) + 1
            }
        }
        return counts
    }

    on most_common(text: String) {
        let counts = count(text)
        let best = ""
        let best_n = 0
        for w in counts.keys {
            if counts[w] > best_n {
                best_n = counts[w]
                best = w
            }
        }
        return best
    }

    on distinct_words(text: String) {
        return len(count(text).keys)
    }
}

cell test WordCountTests {
    rules {
        assert count("the cat sat on the mat the cat")["the"] == 3
        assert count("the cat sat on the mat the cat")["cat"] == 2
        assert count("the cat sat on the mat the cat")["mat"] == 1
        assert most_common("the cat sat on the mat the cat") == "the"
        assert distinct_words("a b c a b a") == 3
        assert distinct_words("") == 0
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check word_count.cell
✓ All checks passed.
$ soma test word_count.cell
test WordCountTests ...
  ✓ assert count("the cat sat on the mat the cat")["the"] == 3
  ✓ assert count("the cat sat on the mat the cat")["cat"] == 2
  ✓ assert count("the cat sat on the mat the cat")["mat"] == 1
  ✓ assert most_common("the cat sat on the mat the cat") == "the"
  ✓ assert distinct_words("a b c a b a") == 3
  ✓ assert distinct_words("") == 0

6 tests: 6 passed, 0 failed
$ soma verify word_count.cell
Verifying word_count.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)
$ soma run word_count.cell count "the cat sat on the mat the cat"
{"the": 3, "cat": 2, "sat": 1, "on": 1, "mat": 1}

Builtins used

len, map, split, trim

Source file: /corpus/algorithms/word_count.cell · examples/corpus/algorithms/word_count.cell