Sky-blue intelligence: how an n-gram model writes a soccer chant
Tokenizers, lemmatizers, part-of-speech taggers and 4-grams: the natural language processing tools that generate an AI’s first batch of lyrics.
Before an AI can write songs, it has to learn how sentences are put together. Héctor Cotelo explains the classic natural language processing tools and how a 4-gram model, trained on songs about Uruguay’s national soccer team, wrote a chant for the 2018 World Cup in Russia.
TABLA DE CONTENIDOS
Series «Songwriting with artificial intelligence», part 3 of 3. Part 1 · Part 2 · Part 3
In the first article in this series I introduced an AI that could write songs, and in the second I went into the genetic algorithm that evolved an initial population of songs to improve them until it produced a good one. All that’s left to explain is how I created that initial population (generation 0): with natural language processing (NLP) tools and a 4-gram model trained, this time, on soccer songs about Uruguay’s national team. This is 2018 work, predating large language models such as ChatGPT (released in November 2022), and it walks through classic NLP techniques with a concrete example. The lyrics are in Spanish, so the examples below keep the original text.
NLP tools for teaching a machine to write
To generate this initial population we used NLP text generation techniques, a subfield of artificial intelligence.
For the AI to learn to write songs, you need to build a model and feed it examples (existing songs) so it can somehow absorb words, sentences and phrases. Many tools make this hard job easier:
- Lexical analyzers (lexer, tokenizer, scanner): take text (a sequence of characters) as input and output tokens, words, lexemes or symbols (all names for lexical components).
- Parsers: analyze a list of tokens according to the rules of a formal grammar and turn it into a parse tree. It’s one of the first steps in analyzing natural language sentences.
- Stemmers: trim a word down to its root, or stem. In Spanish, “perro”, “perra”, “perrito”, “perrazo” and “perrera” (dog, female dog, puppy, big dog, kennel) would all become “perr”.
- Lemmatizers (morphological taggers): separate a word’s root (lexeme) from its inflectional morphemes and tag it with its canonical form, or lemma. Both stemmers and lemmatizers reduce words to a common base, but stemming uses crude heuristics that chop off word endings hoping to get it right most of the time, while lemmatization usually does it more properly, with a vocabulary and morphological analysis.
- Part-of-speech (POS) taggers: label words by grammatical category (noun, pronoun, verb, adjective, adverb, preposition, conjunction, interjection, date, number, punctuation), as well as gender, number, verb tense and so on.
- Dependency parsers: perform basic syntactic analysis that tries to define the relationship between each element of a sentence.
- N-grams: a probabilistic model that statistically predicts the next item in a sequence from the previous ones; for example, given three words, predicting the fourth. After “Ahora golpeas la” (“Now you hit the”), the next word might be wall (27%), door (18%), table (22%) or ball (33%) (illustrative probabilities).
Several of these tools rely on machine learning: instead of being programmed to cover endless cases (which would be impossible), they are trained on examples labeled by hand (a corpus). That way the system learns how often certain word combinations appear in a specific type of song.
Why human language is hard to analyze
Parsing sentences isn’t easy, because human language is highly ambiguous. A Spanish example uses the same word, “bajo”, four ways:
Cada noche que bajo (1) a la calle, el músico bajo (2) toca el bajo (3) bajo (4) la luna.
(1) verb, “I go down” · (2) adjective, “short” · (3) noun, “bass” · (4) preposition, “under”
A sentence isn’t a random list of words. There are formal rules and constraints that can be learned by analyzing example sentences whose words are tagged by part of speech. For instance, a basic sentence has a subject and a predicate: the subject can be an optional determiner followed by a noun or pronoun, and the predicate a verb (agreeing with the subject in person and number) and an optional adjective.
How a 4-gram model writes the first generation
The solution combines several of these methods to generate a sequence of words based on the model’s probabilities. That ensures some grammatical correctness (allowing for the artistic license of a song) and textual consistency, since all the songs it was fed share the same theme.
In the previous articles, the model had been trained on cumbia songs; for this example it used soccer songs about Uruguay’s national team. Many soccer songs, like cumbias, repeat the same structures: some phrases come up again and again. In both cases those patterns were used to train the AI.
The word sequence is built with 4-grams: the model predicts the probability of a word given the three before it. Part of the trick is to use that probability distribution to pick the word at random rather than always choosing the most likely candidate, so the algorithm isn’t deterministic and produces a different song on every run. There’s also a bit of secret sauce to keep the song as different as possible from existing ones: if the verse being built already has three words from the same song, the algorithm lowers the weight of that song’s words so the next one comes from another.
In short, the AI is fed a set of songs and counts how often each word appears (and how it is tagged) after each set of three previous words. With that, the model predicts one word after another until the song reaches the desired length.
“Inteligencia celeste”: the song the algorithm wrote
This is one of the results, which I published in June 2018, during the World Cup in Russia. I titled it “Inteligencia celeste” (Sky-blue intelligence, after the color of Uruguay’s jersey):
Llegó la hora
de la batalla
11 hombres defienden
la linda bandera
de 9 rayasVan a brillar
los gladiadores
como lo hace el sol
del cielo celeste
cuando hacen golesCon mucha garra
y corazón
vamos por todo
somos tres millones
con mucha ilusiónDejar lo más alto
la hermosa celeste
sin ser favoritos
buscando la hazaña
cueste lo que cuesteCon tremendo orgullo
y dientes apretados
vamo’ la celeste
con la frente en alto
están los uruguayosCuando la cosa
no está en calma
más transpiramos
la camiseta
celeste del almaVamo’ a demostrar
que esto es Uruguay
pase lo que pase
no le tememos
a ningún rivalVamo’ campeones
con sed de gloria
no hay más honor
para la aldea
que la victoria
To be sung murga-style, the Uruguayan carnival chorus, to a “marcha camión” beat.
The songs and jingles used as the corpus were: A diario vivimos el país (Diario El País); A grito de gol (Walter Zeinal); Al fondo de la red (Mauricio Ubal); Cada vez te quiero más (Buitres); Cielo de un solo color (No Te Va Gustar); Cuando juega Uruguay (Jaime Roos); Descolgando el cielo (Edu “Pitufo” Lombardo); El tipo de la radio (Tabaré Cardozo); Esto es Uruguay, papá (The Party Band); Gaucho Power (Cuarteto de Nos); Gloriosa celeste (Canario Luna); Himno Nacional (Francisco Acuña de Figueroa); José sabía (La Vela Puerca); La canción del Mundial (Edison Campiglia, Rafael Cotelo DelSol); La copa de la vida (Ricky Martin); Locura celeste (Tabaré y Yamandú Cardozo); Marcha mi bandera (Nicolás Bonomi y Luis Suárez); Mi país (Rubén Rada); Murguero oriental (Tabaré Cardozo); Nací celeste (Snake); Negro jefe (Canario Luna); Nuestra bandera (Daniel Viglietti); Orgullo de ser uruguayo (Canarias); Schneck, bien nuestro (Frigorífico Schneck); Todos goleando (Pájaro Canzani); Un dulce sentimiento (Daniel Deniesse); Un grito celeste (La Tuerca); United by love (Natalia Oreiro); Uruguayos campeones de América y del mundo (Canario Luna); Vamos la celeste (Partido Independiente); Vayan pelando las chauchas (Canario Luna); Waka Waka (Shakira); Waving Flag, versión en español (K’naan y David Bisbal). Texts from tribute and motivational videos about the national team were also transcribed.
Héctor Cotelo
Business Analytics & Information Management Consultant