File size: 4,251 Bytes
44db343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import numpy as np
from tqdm import tqdm
from math import ceil

def load_vsec_dataset(base_path, corr_file, incorr_file):
    # load files
    if base_path:
        assert os.path.exists(base_path) == True
    incorr_data = []
    opfile1 = open(os.path.join(base_path, incorr_file), "r", encoding="utf-8")
    for line in opfile1:
        if line.strip() != "":
            incorr_data.append(line.strip())
    opfile1.close()
    corr_data = []
    opfile2 = open(os.path.join(base_path, corr_file), "r", encoding="utf-8")
    for line in opfile2:
        if line.strip() != "":
            corr_data.append(line.strip())
    opfile2.close()

    assert len(incorr_data) == len(corr_data)

    data = []
    for x, y in zip(corr_data, incorr_data):
        data.append((x, y))

    print(f"loaded tuples of (incorr, corr) examples from {base_path}")
    return data

def load_dataset(base_path, corr_file, incorr_file, length_file = None):
    # load files
    if base_path:
        assert os.path.exists(base_path) == True
    
    data = []
    opfile2 = open(os.path.join(base_path, corr_file), "r", encoding="utf-8")
    for line in tqdm(opfile2):
        if line.strip() != "":
            data.append([line.strip()])
            data.append([line.strip()])
    opfile2.close()

    opfile1 = open(os.path.join(base_path, incorr_file), "r", encoding="utf-8")
    for i, line in tqdm(enumerate(opfile1)):
        if line.strip() != "":
            data[i].append(line.strip())
    opfile1.close()

    opfile4 = open(os.path.join(base_path, length_file), "r", encoding="utf-8")
    for i, line in tqdm(enumerate(opfile4)):
        if line.strip() != "":
            data[i].append(int(line))
    opfile4.close()

    print(f"loaded tuples of (incorr, corr, length) examples from {base_path}")
    return data

def load_epoch_dataset(base_path, corr_file, incorr_file, length_file, epoch: int, num_epoch: int):
    # load files
    if base_path:
        assert os.path.exists(base_path) == True
    assert num_epoch >= 1
    assert epoch >= 1 and epoch <= num_epoch

    ## Count number of data
    opfile = open(os.path.join(base_path, length_file), "r", encoding="utf-8")
    count = 0
    for i, line in tqdm(enumerate(opfile)):
        count +=1
    opfile.close()
    print(f"Number of training datas: {count} examples!")
    
    epochdataset_examples = int(ceil(1 / num_epoch * count))
    start_index = epochdataset_examples * (epoch - 1)
    end_index = start_index + epochdataset_examples

    data = []
    opfile2 = open(os.path.join(base_path, corr_file), "r", encoding="utf-8")
    traverse_count = 0
    for i, line in tqdm(enumerate(opfile2)):

        if line.strip() != "":
            if traverse_count >= start_index and traverse_count < end_index :
                data.append([line.strip()])
                traverse_count += 1
            elif traverse_count >= end_index:
                break
            else:
                traverse_count += 1

            if traverse_count >= start_index and traverse_count < end_index :
                data.append([line.strip()])
                traverse_count += 1
            elif traverse_count >= end_index:
                break
            else:
                traverse_count += 1

    opfile2.close()
    opfile1 = open(os.path.join(base_path, incorr_file), "r", encoding="utf-8")
    traverse_count = 0
    for i, line in tqdm(enumerate(opfile1)):
        if line.strip() != "":
            if traverse_count >= start_index and traverse_count < end_index :
                data[i - start_index].append(line.strip())
            elif traverse_count >= end_index:
                break
        traverse_count += 1
    opfile1.close()
    traverse_count = 0
    opfile4 = open(os.path.join(base_path, length_file), "r", encoding="utf-8")
    for i, line in tqdm(enumerate(opfile4)):
        if line.strip() != "":
            if traverse_count >= start_index and traverse_count < end_index :
                data[i - start_index].append(int(line))
            elif traverse_count >= end_index:
                break
        traverse_count += 1
    opfile4.close()

    print(f"loaded tuples of (incorr, corr, length) examples from {base_path}")
    return data