home

Meet Loquax (and Problems with CLTK)

Table of Contents

During lockdown, I was committing (portions of) the Aeneid to memory, and the exercise proved to be a lot fiddlier than expected: firstly because verse sticks better when you can say it out loud, and secondly, if you are a perfectionist the very thought of saying it out loud in a way that would have made the Latins giggle is anathema.

So you're not a native speaker (nobody is), and if you're not an expert either, the very first (and touted) line is already a bit of a grind to use the parlance of our times:

Arma virumque canō, Trōiae quī prīmus ab ōrīs

Every one of these has an answer, and every answer is a rule (at least in theory). I found these two pretty solid projects:

  1. CLTK, the Classical Language Toolkit, is the big one: a Python package for NLP on corpora of classical languages.
  2. Open IPA is younger and narrower: French and Latin transliteration, in the browser. Its rules live in a custom database that's meant to be easy to edit (as per the docs) but is annoyingly opaque for fellow programmers.

This post is about CLTK. I don't mean to be overly critical with the below, and think CLTK is a great project - in the right direction, that certainly captured my imagination.

Heavier APIs (no bueno)

To begin, using CLTK was a pain. Before transcribing Latin, we need a corpus: a git clone into ~/cltk_data that nothing mentions until something breaks.

THEN the transcriber's constructor builds a macronizer, whether you asked for macrons or not:

self.macronizer = m.Macronizer("tag_ngram_123_backoff")

To get its vowel lengths, that macronizer finds a macrons.py under ~/cltk_data and runs it. And import cltk writes a function into Python's builtins, which twenty-three modules then call without importing.

The cherry on top, importing anything from cltk pulls in PyTorch, even the syllabifier, a couple hundred lines of plain if​s. When we run our pipeline and it stops to ask, on stdin, whether it may download models (eyeroll).

Three syllabifiers, no syllable

There are three syllabifiers in all: in phonology, in prosody, and inside the transcriber. They share no code and no idea of what a syllable is, or of when an i is a consonant. One leaves a note:

# Note: what about compounds like 'adiungere'

Good question.

Take ui. The first syllabifier calls it a diphthong only in huius, cuius and a few friends. The second strikes it on purpose. The prose scanner, built on the second, puts it back to decide which syllables are long, then patches qui by hand. Within one call, ui both is and isn't a diphthong.

The first also keeps sixty-odd prefixes ("counter" and "outr" among them) that nothing reads. What it uses is in, ex and ob, lopped off any word that starts with them. So obolus, a Greek coin, comes out ob.o.lus.

Phonology as string processing

TL;DR the CLTK situation is very scripty. And none of the afore are really a separate bug. They all come from one decision: in CLTK, phonology is string processing. A string goes in, a string comes out, one word at a time. This is the pipeline's whole transcription step:

for word in output_doc.words:
    word.phonetic_transcription = transcriber(
        word.string.lower()
    )

There's no shared idea of a sound, a syllable or a rule, so every module invents its own, and they drift apart. That's ui. A rule is a procedure, so a new dialect means a fork. And nothing is bigger than a word, so there's no elision.

The right idea, one directory over

The odd thing is that CLTK knows better. Here's n turning velar before g or k, in Old English:

n >> Place.velar | ANY - g // k

And the same sound change in Latin:

def _n_place_assimilation(self):
    out_phones = self.phones
    target = Phone("ŋ")
    for n in range(len(self.phones)):
        p = self.phones[n]
        if p.ipa == "n̪" and p.right.vel:
            out_phones[n] = target
    self.phones = out_phones
    self._refresh()

The first is a rule you can read, compare and swap out. The second is a loop that happens to apply one. Latin has fourteen of them, run in a fixed order, and exactly one way to set them up: Classical, after Allen. Want Ecclesiastical? Write more methods. Old English and Old Norse share a feature system and four languages share a generic syllabifier. Latin uses neither. The toolkit is organised by contributor, not by idea … which is a shame because this is a really cool project in my opinion.

Loquax

I've been building Loquax for the past year. Each of its ideas answers one of the problems above:

  • No dependencies - No PyTorch, no models to download, nothing on disk.
  • One Syllable - Every part of the library uses the same one, so nothing drifts apart.
  • A language is a value made of rules - A new dialect is a few rules swapped, not a fork.
  • Rules can see their neighbours - That's MOST of what Latin needs. E.g: long by position, the consonantal i.

The essence of the library is very FP in the sense you declaratively build a language with whatever regional or dialectic tweaks you want - then syllabification and scansion are merely pure functions.

from loquax import Document
from loquax.languages import Latin

text = "Quoūsque tandem abutēre, Catilīna, patientiā nostrā?"
doc = Document(text, Latin)
print(doc.to_string(ipa=True, scansion=True))
kʷɔ.uːs.kʷɛ    tan.dɛm    a.bʊ.teː.rɛ
 u   -   u      -   u     u u   -  u

ka.tɪ.liː.na    pa.tɪ.ɛn.tɪ.aː    nɔs.traː
u  u   -  u     u  u  u  u  -      u   -

Text in, sounds out. Isn't that nice?

What it doesn't know

Loquax's Latin is the syllable page of Wheelock's, written down as rules. A syllable per vowel or diphthong. One consonant goes right, two split.

For now at least, this is also the limitation. Loquax knows sounds, not words. Every long vowel is already marked, and the algorithm takes this into account when we parse the utf-8. Because a vowel's length often lives in the grammar, and the same goes for syllable quantity in scansion. Currently loquax has no declensions, no conjugations and no dictionary. It can't tell a prefix from a stem either, so adiungere is still a good question.

Quōusque tandem, indeed… but hopefully we will get there.