"""Demo 4: reusable counting and ranking functions."""

import io
import re
from dataclasses import dataclass
from typing import TextIO


@dataclass(frozen=True)
class WordCount:
    word: str
    count: int


def count_words(source: TextIO, fold: bool) -> dict[str, int]:
    """Read source line by line and return word counts."""
    # TODO: Implement the streaming counter.
    raise NotImplementedError


def top_n(counts: dict[str, int], n: int) -> list[WordCount]:
    """Return count-descending, word-ascending results."""
    # TODO: Sort, truncate, and create WordCount objects.
    raise NotImplementedError


source = io.StringIO("the cat and the hat")
for row in top_n(count_words(source, True), 3):
    print(f"{row.count}\t{row.word}")
