AI Compass
Compass

Classical language processing

Tokenising, lemmas, TF-IDF, BM25 and entity recognition: methods that predate language models and remain the better choice in many cases.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

Long before language models existed, text was processed by machine: split into words, reduced to base forms, counted and weighted. Those methods are not obsolete; they do part of the work better.

What they are good at

TaskClassicalLanguage model
Finding a case numbervery good, exactunreliable
Validating an IBANperfect, with a checksumunnecessary
Counting frequenciesperfectunsuitable
Searching by keywordvery goodredundant
Grasping meaningweakvery good
Rephrasingunsuitablevery good

The processing chain

  1. 01

    Normalise

    Lower case, unify Unicode, handle diacritics. For German this is also where compound handling is decided.

  2. 02

    Tokenise

    Into words and sentences. Abbreviations ending in a full stop need an exception list, or sentences break at "e.g." and "para.".

  3. 03

    Reduce to base forms

    For German, lemmatisation clearly beats stemming, because inflection is richer.

  4. 04

    Weight

    TF-IDF or BM25, depending on whether you are comparing or searching.

import re

# Patterns a language model does not recognise more reliably than a rule:
PATTERNS = {
    "IBAN":       r"\b[A-Z]{2}\d{2}(?:[ ]?[A-Za-z0-9]{4}){2,7}\b",
    "VAT number": r"\b(?:ATU\d{8}|DE\d{9})\b",
    "date":       r"\b\d{1,2}\.\s?\d{1,2}\.\s?\d{2,4}\b",
    "case ref":   r"\b\d+\s?[A-Za-z]{1,3}\s?\d+/\d{2}\b",
}

def extract(text):
    return {name: re.findall(p, text) for name, p in PATTERNS.items()}

TF-IDF and BM25

TF-IDF

tfidf(t,d) = tf(t,d) · log( N / df(t) )

A term weighs more the more often it occurs in the document and the rarer it is across the collection.

tf(t,d)
frequency of term t in document d
N
number of documents
df(t)
number of documents containing t

BM25

BM25(t,d) = IDF(t) · ( tf·(k₁+1) ) / ( tf + k₁·(1 − b + b·|d|/avgdl) )

Like TF-IDF but with saturating term frequency and a correction for document length.

|d|
document length in words
avgdl
mean document length in the collection
k₁
saturation parameter, usually 1.2 to 2.0
b
length normalisation, usually 0.75

Saturation is the decisive difference: under TF-IDF a document with twenty occurrences is twice as relevant as one with ten. Under BM25 the contribution approaches a ceiling: which matches reality, because the twentieth occurrence carries little new information.

German peculiarities

  • Compounds. "Rechnungsprüfungsverfahren" is not found by a full-text search for "Prüfung". A compound splitter lifts recall considerably.
  • Inflection. "Vertrages", "Verträge", "Vertrag" belong together. Without lemmatisation recall drops noticeably.
  • Umlauts. Both "Prüfung" and "Pruefung" must be found. Normalisation in both directions is needed.
  • Linking-s. Rule-based splitting fails on it routinely; a dictionary approach is more reliable.

The combination

In practice hybrid search almost always wins: BM25 for exact terms, vector search for meaning, fused over the ranks. Measured recall@10 regularly sits 5 to 15 points above either method alone. See Vector databases.

Related courses and sources

CourseFree1500 minEN

Hugging Face NLP course

Tokenisation, transformers, fine-tuning and deployment, with code throughout. Assumes Python, and in exchange you end up working with real models.

For development with language models once it has to go beyond calling an interface.

Hugging FaceGo to offer
ToolFreeEN

spaCy

A library for classical language processing. For recognising names, parts of speech and structure it is often faster, cheaper and more checkable than a language model.

When names, parts of speech or structure are needed: often faster, cheaper and more checkable than a language model.

BookFreeEN

Speech and Language Processing

Jurafsky and Martin, the standard work on language processing, free chapter by chapter. Covers classical methods and language models in one arc.

For anyone learning language processing systematically, classical and modern in one arc.

Was this page helpful?
Classical language processing