Lyrics graph

Idea by Kunal Anand, redone by leonardo maffi
Version 1.0, November 29 2006

[Go back to the index]

Python code + some dot files: lyrics_graph.zip

Taking the idea from this page by Kunal Anand, I have created a small Python script to do something similar, and I have applied it on mostly Italian songs. The program is very simple, it uses my Graph class. It splits the lirics of a song into its lines loaded as unicode, converts them into a simpler lowercose ASCII form and splits the words of each line, then adds directed arcs for each pair of words to a graph. Then it saved in Graphviz format, to be shown with the "dot" program. This is the core of the code:

from graph import Graph

def save_graphviz(lyrics, filepath):
    lyrics_lines = [line for line in lyrics.lower().splitlines() if line.strip()]
    g = Graph()
    for line in lyrics_lines:
        clean_line = "".join((c if c.isalpha() else " ") for c in line)
        clean_line_conv = clean_line.encode('raw_unicode_escape')
        line_words = clean_line_conv.split()
        upairs = zip(line_words, line_words[1:])
        g.addArcs(upairs)
    g.saveDot(fileName=filepath)

And the results:


Click to see full size of "Alla fiera dell est" (Angelo Branduardi).


Click to see full size of "Aqueous transmission" (Incubus).


Click to see full size of "Ballo in Fa diesis minore" (Angelo Branduardi).


Click to see full size of "Bella signora" (Gianni Morandi).


Click to see full size of "Il dono del cervo" (Angelo Branduardi).


Click to see full size of "La serie dei numeri" (Angelo Branduardi).


Click to see full size of "Samarcanda" (Roberto Vecchioni).

The results are interesting. "Alla fiera dell'Est" is really simple, because it repeats itself a lot, with just small parts added at each itreration. I have chosen mostly songs by Branduardi because I like them and because their lyrics are often much more complex than the usual cheap Pop music songs.

Python code + some dot files: lyrics_graph.zip

[Go back to the index]