-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.py
159 lines (121 loc) · 3.37 KB
/
sequence.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
import math
from dataclasses import dataclass
from typing import List, Dict, Tuple, Any
# from collections import Counter
'''
class PositionalEncoding:
def __init__(self, max_length: int):
self._max_length = max_length
self._pe = [0] * max_length
pos = list(range(max_length))
e2i = math.exp(-1 * math.log(10_000.0))
alpha = self._mul(e2i, pos)
pesin = self._apply(math.sin, alpha)
pecos = self._apply(math.cos, alpha)
for index in range(0, max_length, 2):
self._pe[index] = pesin[index]
for index in range(1, max_length, 2):
self._pe[index] = pecos[index]
def _mul(self, a: float, v: list) -> list:
return [a*x for x in v]
def _apply(self, f, v: list) -> list:
return [f(x) for x in v]
def predict(self, sequence: List[int]) -> List[float]:
seq_length = len(sequence)
if seq_length >= self._max_length:
raise ValueError("Sequence in of max length.")
return [self._pe[pos] + sequence[pos] for pos in range(seq_length)]
'''
class Sequence:
@dataclass
class Occurrence:
value: Any
number: int
def __init__(self, seq: List[Any]):
self._sequence = []
self._numbers = []
self._counts = {}
for x in seq:
self._sequence.append(x)
if x in self._counts:
self._counts[x] += 1
self._numbers.append(self._counts[x])
else:
self._counts[x] = 1
self._numbers.append(1)
def indexof(self, occ: Occurrence) -> int:
if occ.value not in self._counts:
return -1
# number = self._counts[occ.value]
# if number == 1:
# return self._sequence.index(occ.value)
# else:
for index, (value, number) in enumerate(zip(self._sequence,
self._numbers)):
if occ.value == value and occ.number == number:
return index
return -1
def __len__(self) -> int:
return len(self._sequence)
def __getitem__(self, index: int) -> Occurrence:
occ = self.Occurrence(None, -1)
occ.value = self._sequence[index]
occ.number = self._numbers[index]
return occ
class SequenceAnalyser:
""" Sequence Analyser """
def _is_ordered(self, seq: List[Any]) -> bool:
current_value = seq[0]
for i in range(1, len(seq)):
if current_value >= seq[i]:
return False
current_value = seq[i]
return True
def get_analysis(self,
pred_tokens: List[Any],
targ_tokens: List[Any]) -> List[int]:
pred_len = len(pred_tokens)
targ_len = len(targ_tokens)
pred_seq = Sequence(pred_tokens)
targ_seq = Sequence(targ_tokens)
results = []
matching = []
pos = -1
max_pos = -1
for index in range(pred_len):
pred = pred_seq[index]
pos = targ_seq.indexof(pred)
if pos != -1:
if pos > max_pos:
results.append(1)
max_pos = pos
else:
results.append(0)
matching.append(pos)
else:
results.append(-2)
matching.append(-2)
# print(matching)
for targ_index in range(targ_len):
if targ_index not in matching:
pos = -1
for index, targ_position in enumerate(matching):
if targ_position > targ_index:
pos = index
break
if pos != -1:
results.insert(pos, -1)
matching.insert(pos, targ_index)
else:
results.append(-1)
matching.append(targ_index)
return results, matching
def test():
targ_seq = [7, 2, 1, 1, 9, 8, 2, 0, 2, 8]
pred_seq = [0, 3, 2, 2, 1, 0, 3, 9, 2, 1]
analyser = SequenceAnalyser()
res, _ = analyser.get_analysis(pred_seq, targ_seq)
print(res)
print(_)
if __name__ == '__main__':
test()