-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_file.py
65 lines (45 loc) · 1.25 KB
/
split_file.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
__author__ = 'Shohreh'
import re
fp = open("Collection UNI.txt","r",encoding='utf-8')
fp.seek(0)
text = fp.read()
punc ={'#','?','.','؟'}
c=0
try:
K=int(input("Enter K to divide file into k train and k test files:(default is 1)"))
except:
K=1
for p in punc:
c+=text.count(p)
train_number = int(c*0.9)
test_number = c-train_number
fp.seek(0)
words = list()
i =0
counter1=0
counter2=1
y=0
for k in range(K):
file_train = open("train_data_"+str(k+1)+".txt","w",encoding='utf-8')
file_test = open("test_data_"+str(k+1)+".txt","w",encoding='utf-8')
i=0
for line in fp:
y+=1
line=line.replace("\n","")
line1= re.sub(r'\s{2,100}',r'\t',line)
word_tag=line1.split("\t")
if(i<=test_number*counter1):
file_train.write(line+"\n")
if(word_tag[0]=='#'or word_tag[0]=='.'or word_tag[0]=='?'or word_tag[0]=='؟'):
i+=1
elif(i<=test_number*counter2):
file_test.write(line+"\n")
if(word_tag[0]=='#'or word_tag[0]=='.'or word_tag[0]=='?'or word_tag[0]=='؟'):
i+=1
else:
file_train.write(line+"\n")
file_test.close()
file_train.close()
counter1+=1
counter2+=1
fp.seek(0)