In [194]:
import matplotlib.pyplot as plt
%matplotlib inline

In [195]:
import nltk
from nltk.book import text6

In [196]:
#get a collection of words from text6 that both words and whose length is greater than 11
col = list(set([w.upper() for w in text6 if w.isalpha() and len(w)>11]))
col


Out[196]:
[u'INDIVIDUALLY',
 u'CONSIDERABLE',
 u'PARTICULARLY',
 u'UNFORTUNATELY',
 u'DICTATORSHIP',
 u'INDEFATIGABLE',
 u'ILLEGITIMATE',
 u'ACCOMPLISHED',
 u'UNDERSTANDING',
 u'DISTRIBUTING',
 u'CAMAAAAAARGUE',
 u'DISHEARTENED',
 u'ELDERBERRIES',
 u'OOOOHOOHOHOOO',
 u'INTERMISSION',
 u'BRIDGEKEEPER',
 u'PERPETUATING',
 u'DRAMATICALLY',
 u'AUTOMATICALLY']

In [197]:
import networkx as nx
G=nx.Graph()
#We use the words from above as the nodes on the graph
G.add_nodes_from(col)

In [198]:
#add an edge between the second and third entry
G.add_edge(col[1],col[2])

In [199]:
#add an edge between the first entry and all even-length entries in the collection of words.
for i in range(0,len(col)):
    if len(col[i])%2 ==0:
        G.add_edge(col[0],col[i])

In [200]:
nx.draw_networkx(G)



In [201]:
G.edges()


Out[201]:
[(u'INDIVIDUALLY', u'INDIVIDUALLY'),
 (u'INDIVIDUALLY', u'DISTRIBUTING'),
 (u'INDIVIDUALLY', u'PARTICULARLY'),
 (u'INDIVIDUALLY', u'DICTATORSHIP'),
 (u'INDIVIDUALLY', u'INTERMISSION'),
 (u'INDIVIDUALLY', u'DISHEARTENED'),
 (u'INDIVIDUALLY', u'DRAMATICALLY'),
 (u'INDIVIDUALLY', u'ILLEGITIMATE'),
 (u'INDIVIDUALLY', u'ACCOMPLISHED'),
 (u'INDIVIDUALLY', u'ELDERBERRIES'),
 (u'INDIVIDUALLY', u'BRIDGEKEEPER'),
 (u'INDIVIDUALLY', u'PERPETUATING'),
 (u'INDIVIDUALLY', u'CONSIDERABLE'),
 (u'PARTICULARLY', u'CONSIDERABLE')]

A Look at the sample Florentine Family Graph


In [202]:
nx.draw_networkx(nx.florentine_families_graph())



In [203]:
nx.florentine_families_graph().edges()


Out[203]:
[('Strozzi', 'Ridolfi'),
 ('Strozzi', 'Castellani'),
 ('Strozzi', 'Bischeri'),
 ('Strozzi', 'Peruzzi'),
 ('Tornabuoni', 'Guadagni'),
 ('Tornabuoni', 'Ridolfi'),
 ('Tornabuoni', 'Medici'),
 ('Medici', 'Barbadori'),
 ('Medici', 'Albizzi'),
 ('Medici', 'Acciaiuoli'),
 ('Medici', 'Salviati'),
 ('Medici', 'Ridolfi'),
 ('Albizzi', 'Guadagni'),
 ('Albizzi', 'Ginori'),
 ('Guadagni', 'Bischeri'),
 ('Guadagni', 'Lamberteschi'),
 ('Pazzi', 'Salviati'),
 ('Bischeri', 'Peruzzi'),
 ('Peruzzi', 'Castellani'),
 ('Castellani', 'Barbadori')]

In [204]:
#We can see each family and its connections.
flo = nx.florentine_families_graph()
for i in range(1, len(flo.nodes())):
    print str(flo.nodes()[i]) + " --- " + str(flo.neighbors(flo.nodes()[i]))


Tornabuoni --- ['Guadagni', 'Ridolfi', 'Medici']
Medici --- ['Tornabuoni', 'Barbadori', 'Albizzi', 'Acciaiuoli', 'Salviati', 'Ridolfi']
Albizzi --- ['Guadagni', 'Ginori', 'Medici']
Guadagni --- ['Bischeri', 'Tornabuoni', 'Albizzi', 'Lamberteschi']
Pazzi --- ['Salviati']
Acciaiuoli --- ['Medici']
Bischeri --- ['Guadagni', 'Strozzi', 'Peruzzi']
Peruzzi --- ['Castellani', 'Bischeri', 'Strozzi']
Ginori --- ['Albizzi']
Salviati --- ['Medici', 'Pazzi']
Castellani --- ['Strozzi', 'Peruzzi', 'Barbadori']
Lamberteschi --- ['Guadagni']
Ridolfi --- ['Strozzi', 'Tornabuoni', 'Medici']
Barbadori --- ['Castellani', 'Medici']

In [ ]: