-
Notifications
You must be signed in to change notification settings - Fork 12
/
TOEFL_dataParse.py
165 lines (132 loc) · 4.24 KB
/
TOEFL_dataParse.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pandas as pd
import numpy as np
import csv
import json
import os
from io import StringIO
import io
import unicodedata
import re
def get_text_data(path, cols, csv_path):
w = csv.writer(open(csv_path,'w'))
w.writerow(cols)
l_files = os.listdir(path)
for l in l_files:
fpath = os.path.join(path, l)
with open(fpath) as file:
w.writerow([l, file.read()])
path = os.path.join(os.getcwd(),'ETS_Corpus_of_Non-Native_Written_English/data/text/responses/original')
directory = 'data'
if not os.path.exists(directory):
os.makedirs(directory)
directory = 'data/TOEFL'
if not os.path.exists(directory):
os.makedirs(directory)
data_path = os.path.join(directory, 'textdata.csv')
get_text_data(path, ['Filename', 'text'], data_path)
text_pd = pd.read_csv(data_path)
path_csv = os.path.join(os.getcwd(),'ETS_Corpus_of_Non-Native_Written_English/data/text')
l_csv = ['train', 'dev', 'test']
l_csvfiles = os.listdir(path_csv)
for i in l_csvfiles:
if l_csv[0] in i:
pd_ = pd.read_csv(os.path.join(path_csv,i), header = None, names = ['Filename', 'prompt', 'lang', 'score'])
df_train = pd_.merge(text_pd ,on='Filename', how = 'inner')
if l_csv[1] in i:
pd_ = pd.read_csv(os.path.join(path_csv,i), header = None, names = ['Filename', 'prompt', 'lang', 'score'])
df_dev = pd_.merge(text_pd ,on='Filename', how = 'inner')
if l_csv[2] in i:
pd_ = pd.read_csv(os.path.join(path_csv,i), header = None, names = ['Filename', 'prompt', 'lang', 'score'])
df_test = pd_.merge(text_pd ,on='Filename', how = 'inner')
def clean(t_):
t_ = re.sub('\s+',' ',t_)
t_ = re.sub('- ','',t_)
#url_reg = r'[a-z]*[:.]+\S+'
#t_ = re.sub(url_reg, '', t_)
t_ = re.sub('([.,!?()])', r' \1 ', t_)
t_ = re.sub('\"', ' \" ',t_)
t_ = re.sub('$', ' $ ',t_)
t_ = re.sub(r'\'s', ' \'s', t_)
t_ = re.sub(r'\'re', ' \'re', t_)
t_ = re.sub(r'\'ll', ' \'ll', t_)
t_ = re.sub(r'\'m', ' \'m', t_)
t_ = re.sub(r'\'d', ' \'d', t_)
t_ = re.sub(r'can\'t', 'can n\'t', t_)
t_ = re.sub(r'n\'t', ' n\'t', t_)
t_ = re.sub(r'sn\'t', 's n\'t', t_)
t_ = re.sub('\s{2,}', ' ', t_)
t_ = t_.lower()
mydict = us_gb_dict()
t_ = replace_all(t_, mydict)
return(t_)
def clean_par(t_):
t_ = re.sub('- ','',t_)
t_ = re.sub('([.,!?()])', r' \1 ', t_)
t_ = re.sub('\"', ' \" ',t_)
t_ = re.sub('$', ' $ ',t_)
t_ = re.sub(r'\'s', ' \'s', t_)
t_ = re.sub(r'\'re', ' \'re', t_)
t_ = re.sub(r'\'ll', ' \'ll', t_)
t_ = re.sub(r'\'m', ' \'m', t_)
t_ = re.sub(r'\'d', ' \'d', t_)
t_ = re.sub(r'can\'t', 'can n\'t', t_)
t_ = re.sub(r'n\'t', ' n\'t', t_)
t_ = re.sub(r'sn\'t', 's n\'t', t_)
#t_ = re.sub('\s{2,}', ' ', t_)
t_ = t_.lower()
mydict = us_gb_dict()
t_ = replace_all(t_, mydict)
return(t_)
def us_gb_dict():
filepath = 'us_gb.txt'
with open(filepath, 'r') as fp:
read = fp.read()
us = []
gb = []
gb_f = True
for i in read.splitlines():
line = i.strip()
#print(line)
if line == "US":
gb_f = False
elif gb_f == True:
gb.append(line)
else:
us.append(line)
us2gb = dict(zip(gb, us))
return us2gb
def replace_all(text, mydict):
for gb, us in mydict.items():
text = text.replace(gb, us)
return text
def one_hot_score(df, col = 'score'):
s = []
for i in df[col]:
if str(i) == 'low':
s.append(1)
if str(i) == 'medium':
s.append(2)
if str(i) == 'high':
s.append(3)
df['label'] = s
return df
def clean_text(df, col = 'text'):
t = []
t_par = []
for i in df[col]:
t.append(clean(i))
t_par.append(clean_par(i))
df['text1'] = t
df['text_par'] = t_par
return df
df_test['score'] = df_test['lang']
df_test = clean_text(df_test)
df_test = one_hot_score(df_test)
df_test.to_csv(os.path.join(directory,'test.csv'))
df_train = clean_text(df_train)
df_train = one_hot_score(df_train)
df_train.to_csv(os.path.join(directory,'train.csv'))
df_dev = clean_text(df_dev)
df_dev = one_hot_score(df_dev)
df_dev.to_csv(os.path.join(directory,'dev.csv'))
os.remove(data_path)