-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_data.py
144 lines (115 loc) · 2.92 KB
/
training_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
import json
import jsonlines
import io
import pickle
import numpy as np
import tokenize
def create_dataset(data, name):
""" Create dataset
Parameters
----------
data : list
list of dictionay elements
name: string
name of dataset
"""
path = "datasets/" + name
with open(path, 'w') as outfile:
json.dump(data, outfile)
def getTestData(file):
""" Get test data
Parameters
----------
file: string
name of file
Returns
---------
new_dataset: list
list of dictionay elements
"""
datas = []
new_dataset = []
i = 0
with jsonlines.open(file) as reader:
for instances in reader:
datas.append(instances)
i = i + 1
i = 0
for d in datas:
new_dataset.append({
'id': datas[i]['id'],
'postText': datas[i]['postText']
})
i = i + 1
return new_dataset
def getData():
""" Get data for training
Returns
---------
new_dataset: list
list of dictionay elements
"""
datas = []
target = []
i = 0
file_inst = 'datasets/1/instances.jsonl'
with jsonlines.open(file_inst) as reader:
for instances in reader:
datas.append(instances)
file_inst2 = 'datasets/2/instances.jsonl'
with jsonlines.open(file_inst2) as reader:
for instances in reader:
datas.append(instances)
file_truth = 'datasets/1/truth.jsonl'
with jsonlines.open(file_truth) as reader:
i = 0
nd = 0
for truths in reader:
target.append(truths)
file_truth = 'datasets/2/truth.jsonl'
with jsonlines.open(file_truth) as reader:
i = 0
nd = 0
for truths in reader:
target.append(truths)
new_dataset = []
i = 0
dataset_idx = 0
for t in target:
dato = {}
dato["truthMean"] = t["truthMean"]
id_t = t["id"]
for d in datas:
id_i = d["id"]
if (id_i == id_t):
dato["postText"] = d["postText"]
dato["id"] = id_i
new_dataset.append(dato)
dataset_idx += 1
break
i += 1
return new_dataset
def getDataUnlabeled():
""" Get unlabeled data for training
Returns
---------
new_dataset: list
list of dictionay elements
"""
new_dataset = []
i = 0
file_inst = 'datasets/3/instances.jsonl'
with jsonlines.open(file_inst) as reader:
for instances in reader:
new_dataset.append({
'id':
instances['id'],
'postText':
instances['postText'],
'targetTitle':
instances['targetTitle'],
'targetParagraphs':
instances['targetParagraphs']
})
i = i + 1
return new_dataset