-
Notifications
You must be signed in to change notification settings - Fork 22
/
process_absa_data.py
150 lines (129 loc) · 5.87 KB
/
process_absa_data.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
import os
import sys
import argparse
__author__ = "Jianfei"
TASKS = ["twitter", "twitter2015"]
def format_absa(data_dir, task):
print("Processing..."+task)
absa_dir = os.path.join(data_dir, task)
if not os.path.isdir(absa_dir):
os.mkdir(absa_dir)
absa_train_file = os.path.join(absa_dir, "train.txt")
absa_test_file = os.path.join(absa_dir, "test.txt")
assert os.path.isfile(absa_train_file), "Train data not found at %s" % absa_train_file
assert os.path.isfile(absa_test_file), "Test data not found at %s" % absa_test_file
fin = open(absa_train_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "train.tsv"), 'w') as train_fh:
train_fh.write("index\t#1 Label\t#2 String\t#2 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
label = str(int(polarity) + 1)
train_fh.write("%s\t%s\t%s\t%s\n" % (count, label, text, aspect))
fin = open(absa_test_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "dev.tsv"), 'w') as train_fh:
train_fh.write("index\t#1 Label\t#2 String\t#2 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
label = str(int(polarity) + 1)
train_fh.write("%s\t%s\t%s\t%s\n" % (count, label, text, aspect))
fin = open(absa_test_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "test.tsv"), 'w') as test_fh:
test_fh.write("index\t#1 Label\t#2 String\t#2 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
label = str(int(polarity) + 1)
test_fh.write("%d\t%s\t%s\t%s\n" % (count, label, text, aspect))
print("\tCompleted!")
def format_abmsa(data_dir, task):
print("Processing..."+task)
absa_dir = os.path.join(data_dir, task)
if not os.path.isdir(absa_dir):
os.mkdir(absa_dir)
absa_train_file = os.path.join(absa_dir, "train.txt")
absa_dev_file = os.path.join(absa_dir, "dev.txt")
absa_test_file = os.path.join(absa_dir, "test.txt")
assert os.path.isfile(absa_train_file), "Train data not found at %s" % absa_train_file
assert os.path.isfile(absa_test_file), "Test data not found at %s" % absa_test_file
fin = open(absa_train_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "train.tsv"), 'w') as train_fh:
train_fh.write("index\t#1 Label\t#2 ImageID\t#3 String\t#3 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
imgid = lines[i + 3].strip()
label = str(int(polarity) + 1)
train_fh.write("%d\t%s\t%s\t%s\t%s\n" % (count, label, imgid, text, aspect))
fin = open(absa_dev_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "dev.tsv"), 'w') as train_fh:
train_fh.write("index\t#1 Label\t#2 ImageID\t#3 String\t#3 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
imgid = lines[i + 3].strip()
label = str(int(polarity) + 1)
train_fh.write("%d\t%s\t%s\t%s\t%s\n" % (count, label, imgid, text, aspect))
fin = open(absa_test_file, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
with open(os.path.join(absa_dir, "test.tsv"), 'w') as test_fh:
test_fh.write("index\t#1 Label\t#2 ImageID\t#2 String\t#2 String\n")
count = 0
for i in range(0, len(lines), 4):
count += 1
text = lines[i].strip()
aspect = lines[i + 1].strip()
polarity = lines[i + 2].strip()
imgid = lines[i + 3].strip()
label = str(int(polarity) + 1)
test_fh.write("%d\t%s\t%s\t%s\t%s\n" % (count, label, imgid, text, aspect))
print("\tCompleted!")
def get_tasks(task_names):
task_names = task_names.split(',')
if "all" in task_names:
tasks = TASKS
else:
tasks = []
for task_name in task_names:
assert task_name in TASKS, "Task %s not found!" % task_name
tasks.append(task_name)
return tasks
def main(arguments):
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', help='directory to save data to', type=str, default='absa_data')
parser.add_argument('--tasks', help='tasks to download data for as a comma separated string',
type=str, default='all')
parser.add_argument('--path_to_mrpc', help='path to directory containing extracted MRPC data, msr_paraphrase_train.txt and msr_paraphrase_text.txt',
type=str, default='')
args = parser.parse_args(arguments)
if not os.path.isdir(args.data_dir):
os.mkdir(args.data_dir)
tasks = get_tasks(args.tasks)
for task in tasks:
if task == 'restaurant' or task == 'laptop' or task == 'twitter2014':
format_absa(args.data_dir, task)
else:
format_abmsa(args.data_dir, task)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))