5.3 A report of the corpus

Read the tutorial. Build it yourself is the other way to read this part.

just setup

Since the last part, the walk leaves out the function top, its test, and the option --top. This part begins with their code.

The code before this part. The code after it.

5.3 A report, which just setup makes

The most common words of the corpus make a report, which others want to read without running tally. A recipe of the justfile, report, writes the ten most common words to build/top.txt. The recipe has two lines, and just prints each line before it runs it. The second line has two parts.

  • mkdir -p build makes the folder build, and does nothing when the folder is there.
  • uv run tally --top 10 data/corpus.txt counts the corpus, and keeps the ten most common words.
  • > build/top.txt sends what tally prints to the file, in place of the terminal.

A patch to justfile.

The report is an artifact, as the corpus is. git ignores the folder build/, so git holds the recipe, and not the report. just setup makes the report when the file is missing. test -f build/top.txt is true when the file is there, and || runs just report only when it is not.

At the end of the justfile, the new recipe report makes the folder build, and writes the report into it. In the recipe setup, the new last line runs just report when build/top.txt is missing.

A report from an earlier run stays, because git ignores it. So remove the report first, and then just setup makes it again:

rm -f build/top.txt
just setup
cat build/top.txt

The file has ten lines, the most common word first.

The report is the ten most common words of the corpus, the same list that --top 10 prints.

tally is now finished. It counts, it has tests and types, and just setup makes its report.

What changed

  • justfile: the report, and the line in setup that makes it when it is missing. 6 lines added. The file.
    • look at uv run tally --top 10 data/corpus.txt > build/top.txt
    • look at test -f build/top.txt || just report
The diff of justfile
@@ -6,6 +6,7 @@ default:
 setup:
     uv sync
     uv run python scripts/corpus.py data/corpus.txt
+    test -f build/top.txt || just report
 
 # Count the words of a file
 run file:
@@ -14,3 +15,8 @@ run file:
 # Run the tests
 test:
     uv run python -m unittest discover -s tests -q
+
+# Write the ten most common words of the corpus to build/top.txt
+report:
+    mkdir -p build
+    uv run tally --top 10 data/corpus.txt > build/top.txt

This change on GitHub. The code after it.