-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvmRankFormat.py
More file actions
36 lines (31 loc) · 874 Bytes
/
svmRankFormat.py
File metadata and controls
36 lines (31 loc) · 874 Bytes
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
import sys
# To run:
# python svmRankFormat.py w2v_test/*.out w2v_test/labels.txt w2v_test_svmRank.txt
# Which file we're on
counter = 0
# Label vector
# label = [5, 7, 3, 5, 9, 9, 6, 5, 2, 8, 7, 6, 3, 2, 6, 7, 4, 8, 7, 6]
# Outfile to write to
print "\nOutput file: " + sys.argv[-1]
outFile = open(sys.argv[-1], 'w')
# Read in labels
with open (sys.argv[-2], "r") as myfile:
label=myfile.read().split('\n')
print ""
# Read in vector features and format
for arg in sys.argv[1:-2]:
print arg + " " + str(label[counter])
with open (arg, "r") as myfile:
data=myfile.read()
trainVec = str(label[counter]) + " qid:1 "
featureCounter = 1
for feature in data.split(' '):
if(feature == ""):
continue
trainVec += str(featureCounter) + ":" + feature + " "
featureCounter += 1
trainVec += '\n'
counter += 1
outFile.write(trainVec)
outFile.close
print ""