Here are the lyrics of The Little Irish Girl with punctuation and capitalization ignored:
"""Lyrics of *The Little Irish Girl* as a list of words."""
lyrics = """in the heart of the town near the county square there lives a little colleen with dark brown hair and if you but knew her i know you would say she's the prettiest colleen in ireland today ah she is my joy and my delight her name it is mcdonnell and with her i would walk all the day and dance in the evening light for she is my joy and my delight her name it is mcdonnell and with her i would walk all the day and dance in the evening light i have travelled through the west and i've travelled through the east and of all the handsome colleens that i have ever seen the little irish girl with her dark brown hair she is the prettiest colleen that i have ever seen ah she is my joy and my delight her name it is mcdonnell and with her i would walk all the day and dance in the evening light for she is my joy and my delight her name it is mcdonnell and with her i would walk all the day and dance in the evening light"""
lyrics = lyrics.split(" ")"""Plot the word adjacency graph for *The Little Irish Girl*."""
import os
import matplotlib.pyplot as plt
import networkx as nx
from .lyrics import lyrics
def create_graph(strings):
G = nx.DiGraph()
for s in strings:
G.add_node(s)
for i in range(len(strings) - 1):
G.add_edge(strings[i], strings[i + 1])
return G
def plot_graph(G):
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()]
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 The Little Irish Girl", 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()