Contents Projects Code Course home

Chapter 28Project 1: The Shape of Language

What you will build

This project is a corpus study with one rule: you write a prediction down before you compute the number that tests it. You will run a preprocessing grid, fit Zipf’s law and its Mandelbrot variant, extrapolate a vocabulary with Heaps’ law, compare two genres, and find the most characteristic words of one document. None of it needs new code if you use the pipeline of Chapter 3. What it tests is whether you understand what the numbers mean.

Allow eight to ten hours. Read Chapters 2, 3 and 4 first.

What you need

A corpus of at least a million tokens, split into documents. The Brown corpus is the obvious choice, because it has about 1.161.16 million tokens and Chapter 3 reports its numbers, so you can compare. Use all of Brown for stages 1 to 3 and 5. For stage 4 the contrast is between two of its genres, such as news and learned prose, or between Brown and a second corpus of your own. The script empirical_laws.py in the companion code implements the fitting, and you may use it.

Write your predictions in a file and date it before you run anything that tests them. The point of predicting is that you cannot revise the prediction afterwards.

Stages

  1. The preprocessing grid. Define six configurations that switch three options on and off, as in the table. Run each on your corpus and record the token count, the vocabulary size and the type-token ratio. That is eighteen numbers.

    case folding stop words removed numbers stripped
    C1 off off off
    C2 on off off
    C3 on on off
    C4 on on on
    C5 off on off
    C6 off off on

    Then measure what each switch does on its own, against C1. The effect of case folding is the vocabulary of C1 minus that of C2, of stop words C1 minus C5, and of numbers C1 minus C6. Do not compare configurations in sequence, because C3 minus C2 is the effect of stop words after case folding, which is a different quantity.

    Check. Compare your token and type shares with Chapter 3. On Brown, removing stop words takes about 4444 per cent of the tokens and 0.20.2 per cent of the types. Explain why one switch deletes a large share of the text and barely touches the vocabulary, while another deletes no tokens at all and cuts the vocabulary hardest.

  2. Zipf against Mandelbrot. On configuration C2 fit Zipf’s law and report α\alpha. Fit the Mandelbrot variant and report α\alpha and β\beta. Report the root mean square error in log space of both fits in three rank bands: ranks 11 to 1010, ranks 1111 to 10001000, and the rest.

    Predict first. Will the Mandelbrot fit have a smaller or larger error than plain Zipf on the head? Many people expect smaller, because the offset is sold as the fix for the head. Chapter 3 shows β\beta coming out near zero when only the head is fitted and in the hundreds when every rank is. Explain your result with that table.

  3. The Heaps prediction. Fit Heaps’ law V=KNbV = K N^{b} on the first half of your corpus and report KK and bb, in the notation of Chapter 3. Predict the vocabulary of the full corpus. Then measure it.

    Check. Say which way you missed: you overpredicted because growth slowed, you underpredicted because new words kept arriving, or you landed within ten per cent. Chapter 3 shows an extrapolation over one decade that overshoots by 6262 per cent on Brown. What counts at this stage is the explanation, not the closeness. A prediction that missed badly and is correctly diagnosed is worth more than one that landed by luck.

  4. Genre transfer. Before you fit anything, predict whether α\alpha is higher or lower in your contrast genre, and write down why. Then fit α\alpha and compute the type-token ratio on the contrast corpus with C2, using samples of equal length so that the ratios are comparable. Say what property of the genre explains the direction.

  5. TF-IDF forensics. Choose one document. With configuration C4 compute its ten highest TF-IDF terms against the rest of the corpus. The most frequent content word in the document is often absent from the list. Explain why, using the definition of inverse document frequency.

Extensions

  • Repeat the Heaps extrapolation from several prefix lengths and plot how the error depends on the fraction of the corpus used.

  • Fit Zipf’s law by least squares in log space and by nonlinear least squares in linear space, and say which part of the curve each one listens to.

  • Run the study on a corpus in a language with rich morphology, and predict before you look what happens to the vocabulary and the type-token ratio.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.