-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_common_matrix.py
79 lines (71 loc) · 1.91 KB
/
find_common_matrix.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Find phone numbers matrices have in common, reprint matrices.
input:
txt files of matrices
output:
filtered matrices
"""
import sys
import json
def findCommSet(keysets):
# find maximum common set of keys
n = len(keysets)
commkeys = set(keysets[0])
for i in range(1, n):
commkeys = commkeys & set(keysets[i])
return list(commkeys)
def getkeys(inputfile):
f = open(inputfile, 'r')
keys = []
for line in f.readlines():
key = line.split('\t')[0]
keys.append(key)
f.close()
return keys
def printMatrix(inputfile, keys):
outputfile = inputfile + '.filtered'
fi = open(inputfile, 'r')
fo = open(outputfile, 'w')
for line in fi.readlines():
key = line.split('\t')[0]
if key in keys:
fo.write(line)
fi.close()
fo.close()
return
def printIdx(keys):
outputfile = '../Data/filtered_index.json'
f = open(outputfile, 'w')
keyDict = {}
i = 0
for key in keys:
i += 1
keyDict[key] = i
json.dump(keyDict, f, indent=4)
f.close()
return keyDict
def printAdjacency(inputfile, keys, keyDict):
fi = open(inputfile, 'r')
outputfile = inputfile + '.filtered'
fo = open(outputfile, 'w')
for line in fi.readlines():
line = line.split('\t')
node1 = line[0]
node2 = line[1]
weight = line[2]
if node1 in keys and node2 in keys:
fo.write(str(keyDict[node1]) + '\t' + str(keyDict[node2]) + '\t' + weight)
fi.close()
fo.close()
return
if __name__ == '__main__':
inputfiles = sys.argv[1:]
keysets = []
for inputfile in inputfiles:
keys = getkeys(inputfile)
keysets.append(keys)
commkeys = findCommSet(keysets)
for inputfile in inputfiles:
printMatrix(inputfile, commkeys)
keyDict = printIdx(commkeys)
printAdjacency('../Data/all.phone-page-phone.edges', commkeys, keyDict)