-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdivide.py
50 lines (35 loc) · 1.36 KB
/
divide.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
from argparse import ArgumentParser
from random import randint
from os.path import splitext
'''This method takes a single 'input' file representing a set of triples,
and separates it into two files according to the proportion 1:X. '''
def split_1_to_k(file, number):
# Read the corresponding file
inputFile = open(file, "r")
Lines = inputFile.readlines()
# Mine name and extension of original input file
fname = splitext(file)[0]
fext = splitext(file)[1]
# Define output files: two for training, and two for testing
smallOutput = open(fname + "_facts" + fext, "w")
bigOutput = open(fname + "_graph" + fext, "w")
#Initialise a random variable determining if the fact will go to small or big set,
# with probability 1:X
randomVariable = 0
#For each read line, roll the random variable, and write in the corresponding file.
for line in Lines:
randomVariable = randint(1,int(number))
if randomVariable == 1 :
smallOutput.write(line)
else :
bigOutput.write(line)
smallOutput.close
bigOutput.close
if __name__ == '__main__':
# Read the argument from command line
parser = ArgumentParser()
parser.add_argument("input")
parser.add_argument("X")
args = parser.parse_args()
# Execute split on read arguments
split_1_to_k(args.input,args.X)