Word Adjacency Graph of Rolling Down to Rio

Art
Music
Word Adjacency
Author

Galen Seilis

Published

February 19, 2025

Here are the lyrics of Rolling Down to Rio with punctuation and capitalization ignored:

"""Lyrics of *Rolling Down to Rio* as a list of words."""

lyrics = """i have never sailed the amazon i have never reached brazil but the don and the magdalena they can go there when they will ah yes weekly from southampton great steams white and gold go rolling down to rio roll down roll down to rio and i would like to roll to rio some day before i am old to roll i would like to roll to rio some day before i am old i have never seen a jaguar nor yet an armadillo dillowing in his armour and i suppose i never will ah unless i go to rio these wonders to behold go rolling down to rio roll really down to rio oh i would like to roll to rio some day before i am old to roll i would love to roll to rio some day before i am old"""
lyrics = lyrics.split(" ")
"""Plot the word adjacency graph for *Rolling Down to Rio*."""

import os

import matplotlib.pyplot as plt
import networkx as nx

from .lyrics import lyrics


def create_graph(strings):
    # Initialize a directed graph
    G = nx.DiGraph()

    # Add each string as a node
    for s in strings:
        G.add_node(s)

    # Create edges based on adjacency (e.g., consecutive strings in the list)
    for i in range(len(strings) - 1):
        G.add_edge(strings[i], strings[i + 1])

    return G


def plot_graph(G):
    # Generate positions for nodes
    try:
        pos = nx.nx_agraph.graphviz_layout(G)
    except ImportError:
        pos = nx.spring_layout(G, seed=2018)

    node_sizes = [len(node) * 100 for node in G.nodes()]  # Adjust 100 to change the scaling factor

    # Draw the graph
    plt.figure(figsize=(8, 30))
    nx.draw(
        G,
        pos,
        with_labels=True,
        node_color="lightblue",
        font_size=10,
        node_size=node_sizes,
        font_weight="bold",
        arrows=True,
    )
    plt.title("Word Adjacency in Rolling Down to Rio", fontsize=14)


def main():
    here = os.path.dirname(os.path.abspath(__file__))
    post_dir = os.path.abspath(os.path.join(here, os.pardir, os.pardir))
    figures_dir = os.path.join(post_dir, "figures")
    os.makedirs(figures_dir, exist_ok=True)

    graph = create_graph(lyrics)
    plot_graph(graph)
    plt.savefig(os.path.join(figures_dir, "word-adjacency.png"), dpi=300, bbox_inches="tight")


if __name__ == "__main__":
    main()

Word Adjacency in Rolling Down to Rio