-
Notifications
You must be signed in to change notification settings - Fork 0
/
WNGraph.py
48 lines (32 loc) · 892 Bytes
/
WNGraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import graph as g;
def readFile(fileName,encode):
lines=[]
with open(fileName,'r',encoding=encode) as f:
for line in f:
line = line.split(' ')
u = 'spa' + line[0][5:]
v = 'spa' + line[1][5:]
lines.append((u,v))
return lines
def generateGraph():
fileName = 'WordNetEdges/ili-wnet30g_rels.txt'
file1 = readFile(fileName,'utf-8')
fileName = 'WordNetEdges/ili-wnet30_rels.txt'
file2 = readFile(fileName,'utf-8')
gr = g.Graph()
for i in file1:
gr.addEdge(i[0] , i[1] , 1)
gr.addEdge(i[1] , i[0] , 1)
for i in file2:
gr.addEdge(i[0] , i[1] , 1)
gr.addEdge(i[1] , i[0] , 1)
return gr
gr = generateGraph()
fileV = open('vertList.txt','r')
for v in fileV:
v = v[:len(v)-1] # remove '\n'
f = open("Distances/"+v+'.txt','w')
delta, previous = g.dijkstra(gr, v)
for key,value in delta.items():
f.write("%s %s\n" % (key,value))
f.close()