Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are some comments about some things to watch out for in your code.

The lines

    i = S.index('word: ' + s1)
    j = S.index('word: ' + s2)

should not need the prefix 'word: ', since words in S don't contain such prefix by the way it is defined.

Line 6, f.readline(), might just be discarding the first line of your input.

When you set M[i,j]=1 you might lose track of multiple edges, maybe changing it to M[i,j] += 1 will do.

Also, I guess the labels a, b and c are relevant, so to preserve labels you can just add an edge between s1 and s2 with label g.

Perhaps the following version might do the job

def my_digraph(filename):
    f = open(filename, 'r')
    D=DiGraph(loops=True,multiedges=True)
    for c in f:
        c = c[1:-2]
        s1, g, s2 = c.split(', ')
        D.add_edge([ s1, s2, g ])
    f.close()
    return D