-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
199 lines (156 loc) · 5.94 KB
/
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from supar.utils.tokenizer import Tokenizer
from supar.utils.transform import Transform, Sentence
from supar.utils import Field
import torch
import os
import csv
from typing import Self, Literal, Iterable, Union, Any, Optional, Generator, Iterator
class EyeTracking(Transform):
r"""
A :class:`EyeTracking` object factorize eye tracking data into five fields,
each associated with one or more :class:`~supar.utils.field.Field` objects.
Attributes:
five attributes
"""
fields = ['WORD', 'NFIX', 'FFD', 'GPT', 'TRT', 'FIXPROP']
def __init__(
self,
WORD: Optional[Union[Field, Iterable[Field]]] = None,
NFIX: Optional[Union[Field, Iterable[Field]]] = None,
FFD: Optional[Union[Field, Iterable[Field]]] = None,
GPT: Optional[Union[Field, Iterable[Field]]] = None,
TRT: Optional[Union[Field, Iterable[Field]]] = None,
FIXPROP: Optional[Union[Field, Iterable[Field]]] = None
) -> Self:
super().__init__()
self.WORD = WORD
self.NFIX = NFIX
self.FFD = FFD
self.GPT = GPT
self.TRT = TRT
self.FIXPROP = FIXPROP
@property
def src(self):
return self.WORD, self.NFIX, self.FFD, self.GPT, self.TRT # [self.WORD]
@property
def tgt(self):
return (self.FIXPROP, ) #self.NFIX, self.FFD, self.GPT, self.TRT, self.FIXPROP
def load(
self,
data: Union[str, Iterable],
lang: Optional[str] = None,
**kwargs
) -> Iterator["EyeTrackingSentence"]:
r"""
Args:
data (Union[str, Iterable]):
A filename or a list of instances.
lang (str):
Language code (e.g., ``en``) or language name (e.g., ``English``) for the text to tokenize.
``None`` if tokenization is not required.
Default: ``None``.
Returns:
A list of :class:`TreeSentence` instances.
"""
if lang is not None:
tokenizer = Tokenizer(lang)
# TODO: change this loading mechanism
assert isinstance(data, str) and os.path.exists(data)
index = 0
if data.endswith('.txt'):
data = (s.split() if lang is None else tokenizer(s) for s in open(data) if len(s) > 1)
for s in data:
yield EyeTrackingSentence(self, s, index)
index += 1
elif data.endswith('.csv'):
index = None
data = csv.reader(open(data, "r"))
sentence = []
nFix_sen = []
ffd_sen = []
gpt_sen = []
trt_sen = []
fixProp_sen = []
for sentence_id, word_id, word, nFix, ffd, gpt, trt, fixProp in data:
if sentence_id == "sentence_id":
continue
sentence_id = int(sentence_id)
nFix = float(nFix)
ffd = float(ffd)
gpt = float(gpt)
trt = float(trt)
fixProp = float(fixProp)
if word.endswith("<EOS>"):
word = word[:-5]
if index is None:
index = sentence_id
if sentence_id > index:
yield EyeTrackingSentence(self, sentence, index,
nFix_sen = nFix_sen,
ffd_sen = ffd_sen,
gpt_sen = gpt_sen,
trt_sen = trt_sen,
fixProp_sen = fixProp_sen)
index += 1
sentence = [word]
nFix_sen = [nFix]
ffd_sen = [ffd]
gpt_sen = [gpt]
trt_sen = [trt]
fixProp_sen = [fixProp]
else:
sentence.append(word)
nFix_sen.append(nFix)
ffd_sen.append(ffd)
gpt_sen.append(gpt)
trt_sen.append(trt)
fixProp_sen.append(fixProp)
else:
raise Exception("Unknown data format.")
class EyeTrackingSentence(Sentence):
r"""
Args:
transform (AttachJuxtaposeTree):
A :class:`AttachJuxtaposeTree` object.
tree (nltk.tree.Tree):
A :class:`nltk.tree.Tree` object.
index (Optional[int]):
Index of the sentence in the corpus. Default: ``None``.
"""
def __init__(
self,
transform: EyeTracking,
sentence: list[str],
index: Optional[int] = None,
**kwargs
):
super().__init__(transform, index)
self.values = [sentence]
self.values.extend(kwargs.values())
length = len(sentence)
assert all(len(v) == length for v in self.values)
#print(self.values)
def __repr__(self):
return f"Sentence({self.values})"
def pretty_print(self):
return f"Sentence({self.values})"
class FloatField(Field):
def transform(self, sequences: Iterable[list[float]]) -> Iterable[torch.Tensor]:
r"""
Turns a list of sequences that use this field into tensors.
Each sequence is first preprocessed and then numericalized if needed.
Args:
sequences (Iterable[List[str]]):
A list of sequences.
Returns:
A list of tensors transformed from the input sequences.
"""
for seq in sequences:
#seq = self.preprocess(seq)
#if self.bos:
# seq = [self.bos_index] + seq
#if self.delay > 0:
# seq = seq + [self.pad_index for _ in range(self.delay)]
#if self.eos:
# seq = seq + [self.eos_index]
yield torch.tensor(seq, dtype=torch.float32)