-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (59 loc) · 2.63 KB
/
utils.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
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
import warnings
warnings.filterwarnings('ignore')
from tqdm import tqdm
import pandas as pd
from sklearn.model_selection import train_test_split
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
def punctuations_preprocessing(sent, punc_list=[',', '।', '.']):
sent = [_ for _ in sent]
for idx in range(len(sent)):
if idx != 0 and sent[idx] in punc_list and sent[idx-1] == ' ':
sent[idx-1] = ''
return ''.join(sent)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
def tokenizeInstances(tokenizer, df):
train_sources_encodings = []
train_mask_encodings = []
train_targets_encodings = []
for i in tqdm(range(len(df))):
correct = df['Correct'][i]
erroneous = df['Erroneous'][i]
# print(correct)
# print(erroneous)
correct_encoding = tokenizer(correct)
erroneous_encoding = tokenizer(erroneous)
# print(correct_encoding)
# print(erroneous_encoding)
train_source_encoding = erroneous_encoding['input_ids']
train_mask_encoding = erroneous_encoding['attention_mask']
train_target_encoding = correct_encoding['input_ids']
# print(train_source_encoding)
# print(train_mask_encoding)
# print(train_target_encoding)
train_sources_encodings.append(train_source_encoding)
train_mask_encodings.append(train_mask_encoding)
train_targets_encodings.append(train_target_encoding)
# break
return (train_sources_encodings, train_mask_encodings, train_targets_encodings)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
def trainValidationTestSplit(df, valid_size, test_size):
etypes = list(set(df['ErrorType']))
train_df = pd.DataFrame()
valid_df = pd.DataFrame()
test_df = pd.DataFrame()
for etype in etypes:
etype_df = df.loc[df['ErrorType'] == etype]
train, test = train_test_split(etype_df, test_size=test_size)
train, valid = train_test_split(train, test_size=valid_size)
train_df = pd.concat([train_df, train])
valid_df = pd.concat([valid_df, valid])
test_df = pd.concat([test_df, test])
train_df = train_df.sample(frac=1).reset_index(drop=True)
valid_df = valid_df.sample(frac=1).reset_index(drop=True)
test_df = test_df.sample(frac=1).reset_index(drop=True)
return (train_df, valid_df, test_df)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------