-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_Split.py
More file actions
54 lines (44 loc) · 1.48 KB
/
data_Split.py
File metadata and controls
54 lines (44 loc) · 1.48 KB
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
# -*-coding:UTF-8 -*-
# 將要訓練的句子存起來
def SaveSentence(filepath, sent_list):
f = open(filepath, 'w', encoding='UTF-8')
for sent in sent_list:
f.write(sent + '\n')
f.close()
# 分割train_data和test_data
def data_Split(FileName):
fp = open(FileName, 'r', encoding='utf-8')
line = fp.readline() # 第一行是label,review
line = fp.readline()
train_sent_num = 3000
test_sent_num = 1000
train_positive_num = 0
train_negative_num = 0
test_positive_num = 0
test_negative_num = 0
train_data = []
test_data = []
# 用 while 逐行讀取檔案內容,直至檔案結尾
while line:
sent = ''
sent = line.replace('\n', '')
if line[:2] == '1,':
if train_positive_num < train_sent_num:
train_data.append(sent)
train_positive_num += 1
elif test_positive_num < test_sent_num:
test_data.append(sent)
test_positive_num += 1
else:
if train_negative_num < train_sent_num:
train_data.append(sent)
train_negative_num += 1
elif test_negative_num < test_sent_num:
test_data.append(sent)
test_negative_num += 1
line = fp.readline()
fp.close()
SaveSentence('train_data.txt', train_data)
SaveSentence('test_data.txt', test_data)
if __name__ == "__main__":
data_Split('waimai_10k_zh_tw.csv')