John Keats Poems in 1820: Graphing Words Used with NetworkX and Python

14 Dec
Nodes Weighted by Number of Times They Appear in Keats 1820 Poems.

Nodes Weighted by Number of Times They Appear in Keats 1820 Poems.

You should have watched the NetworkX and Gephi talk from PyData NYC 2012 by Gilad Lotan by now. This is my second example of NetworkX inspired by his talk. This example reads through John Keats poems from 1820 and using a portion of Gilad’s add_node(): I create a node for each word used and a weight. Then I brought it in to Gelphi to make it pretty.I took out any word that occured less than 10x – otherwise ‘the’ and ‘and’ are the tops words. Not exciting. I’m looking for rhyming and interesting words. Here is the code.

import networkx as nx
import matplotlib.pyplot as plt
from collections import Counter
import re

words= re.findall(‘\w+’,open(‘keats1820.txt’).read().lower())

g=nx.Graph()

def add_node(n,weight=None):
if not g.has_node(n):
g.add_node(n)
g.node[n][‘weight’] = weight

occur=Counter(words)

for x in Counter(words):
add_node(x,occur[x])

nx.draw(g)
nx.write_gexf(g, “keats1820.gexf”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: