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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class s0():
|
| 2 |
+
def __init__(self, s):
|
| 3 |
+
self.s = s
|
| 4 |
+
self.l = 0
|
| 5 |
+
def __eq__(self, target):
|
| 6 |
+
return all([self.l==target.l, self.s==target.s])
|
| 7 |
+
|
| 8 |
+
class w():
|
| 9 |
+
def __init__(self, l, W0, T=None, G=[]):
|
| 10 |
+
if type(G)==list and type(l)==int and l>=0 and l==W0.l+1:
|
| 11 |
+
pass
|
| 12 |
+
else:
|
| 13 |
+
raise ValueError('At least one of them wrong: level l; representative element W0; tag T; generation rule G')
|
| 14 |
+
self.l = l
|
| 15 |
+
self.W0 = W0
|
| 16 |
+
self.T = T
|
| 17 |
+
self.G = G
|
| 18 |
+
# To simplify, here we set:
|
| 19 |
+
self.W = self.G
|
| 20 |
+
def __eq__(self, target):
|
| 21 |
+
return all([self.l==target.l, self.W0==target.W0, self.T==target.T, self.G==target.G, self.W==target.W])
|
| 22 |
+
|
| 23 |
+
class s():
|
| 24 |
+
def __init__(self, l, words):
|
| 25 |
+
if type(words)==list and all(list(map(lambda x: x[0]==l and type(x)==list and len(x)==3, words))):
|
| 26 |
+
self.s = words
|
| 27 |
+
else:
|
| 28 |
+
raise ValueError(f'Cannot form a list of words at level {l}!')
|
| 29 |
+
self.l = l
|
| 30 |
+
def __eq__(self, target):
|
| 31 |
+
return all([self.l==target.l, self.s==target.s])
|
| 32 |
+
|
| 33 |
+
class DAHSF():
|
| 34 |
+
def __init__(self, pairs=[]):
|
| 35 |
+
self.ids = []
|
| 36 |
+
self.words = []
|
| 37 |
+
try:
|
| 38 |
+
for ID, word in pairs:
|
| 39 |
+
assert all([type(ID)==list, len(ID)==3, type(ID[0])==int, type(word)==w, word.l==ID[0]])
|
| 40 |
+
assert ID not in self.ids; assert word not in self.words
|
| 41 |
+
self.ids.append(ID); self.words.append(word)
|
| 42 |
+
except:
|
| 43 |
+
raise TypeError('Not pairwise (ID triple, word) or Repetitive!')
|
| 44 |
+
def __call__(self, target):
|
| 45 |
+
if type(target)==list:
|
| 46 |
+
return self.words[self.ids.index(target)]
|
| 47 |
+
elif type(target)==w:
|
| 48 |
+
return self.ids[self.words.index(target)]
|
| 49 |
+
else:
|
| 50 |
+
raise TypeError
|
| 51 |
+
def levels(self):
|
| 52 |
+
assert self.ids!=[] and self.words!=[]
|
| 53 |
+
return sorted(self.ids, reverse=True)[0][0]
|
| 54 |
+
def receptor_grows(self, l):
|
| 55 |
+
receptor = []
|
| 56 |
+
for coordinate in self.ids:
|
| 57 |
+
if coordinate[0]==l:
|
| 58 |
+
word = self(coordinate)
|
| 59 |
+
for probe in map(lambda x: x.s, word.W):
|
| 60 |
+
receptor.append([probe, coordinate])
|
| 61 |
+
receptor.sort(key=lambda x: len(x[0]), reverse=True)
|
| 62 |
+
return receptor
|
| 63 |
+
def generator_grows(self, l):
|
| 64 |
+
generator = []
|
| 65 |
+
for coordinate in self.ids:
|
| 66 |
+
if coordinate[0]==l:
|
| 67 |
+
word = self(coordinate)
|
| 68 |
+
generator.append([word.W0.s, list(map(lambda x: x.s, word.W))])
|
| 69 |
+
return generator
|
| 70 |
+
def W(self, W0):
|
| 71 |
+
for idx, ID in enumerate(self.ids):
|
| 72 |
+
if ID[-1]==W0:
|
| 73 |
+
return list(map(lambda x: x.s, self.words[idx].W))
|
| 74 |
+
def W0(self, Wi):
|
| 75 |
+
for idx, word in enumerate(self.words):
|
| 76 |
+
if Wi in map(lambda x: x.s, word.W):
|
| 77 |
+
return word.W0.s
|
| 78 |
+
def tags(self, l):
|
| 79 |
+
TAGS = set()
|
| 80 |
+
for coordinate in self.ids:
|
| 81 |
+
if coordinate[0]==l:
|
| 82 |
+
TAGS.add(coordinate[1])
|
| 83 |
+
return TAGS
|
| 84 |
+
def get_tag(self, Wi):
|
| 85 |
+
for idx, word in enumerate(self.words):
|
| 86 |
+
if Wi in map(lambda x: x.s, word.W):
|
| 87 |
+
return word.T
|
| 88 |
+
def refresh_changes(self, changes):
|
| 89 |
+
from copy import deepcopy
|
| 90 |
+
levels = list(map(lambda x: x[0], self.ids))
|
| 91 |
+
levels.append(levels[-1]+1)
|
| 92 |
+
for level in range(int(*changes) + 1, levels[-1]):
|
| 93 |
+
changes[level] = []
|
| 94 |
+
for index, coordinate in enumerate(self.ids):
|
| 95 |
+
if coordinate[0]!=level:
|
| 96 |
+
continue
|
| 97 |
+
for change in changes[level-1]:
|
| 98 |
+
old_ID = deepcopy(coordinate)
|
| 99 |
+
while change[0] in coordinate[2]:
|
| 100 |
+
idx = coordinate[2].index(change[0])
|
| 101 |
+
coordinate[2][idx] = change[1]
|
| 102 |
+
for W in self.words[index].W:
|
| 103 |
+
while change[0] in W.s:
|
| 104 |
+
idx = W.s.index(change[0])
|
| 105 |
+
W.s[idx] = change[1]
|
| 106 |
+
# in this example self.W = self.G
|
| 107 |
+
changes[level].append((old_ID, coordinate, self.words[index]))
|
| 108 |
+
del old_ID
|
| 109 |
+
def modify_tag(self, ID, T):
|
| 110 |
+
# check if ID valid
|
| 111 |
+
W = self(ID)
|
| 112 |
+
from copy import deepcopy
|
| 113 |
+
old_ID = deepcopy(ID)
|
| 114 |
+
# get the index
|
| 115 |
+
idx = self.ids.index(ID)
|
| 116 |
+
# modify the target itself
|
| 117 |
+
W.T = T
|
| 118 |
+
ID[1] = T
|
| 119 |
+
# modify database
|
| 120 |
+
self.ids[idx] = ID
|
| 121 |
+
self.words[idx] = W
|
| 122 |
+
# refresh state spaces with greater levels
|
| 123 |
+
changes = {ID[0]: [(old_ID, ID)]}
|
| 124 |
+
del old_ID, W
|
| 125 |
+
self.refresh_changes(changes)
|
| 126 |
+
def change_representative_element(self, old_ID, new_representative_element):
|
| 127 |
+
# check if ID valid
|
| 128 |
+
W = self(old_ID)
|
| 129 |
+
assert new_representative_element in list(map(lambda x: x.s, W.W))
|
| 130 |
+
# get the index
|
| 131 |
+
idx = self.ids.index(old_ID)
|
| 132 |
+
# generate new ID
|
| 133 |
+
new_ID = old_ID[:-1]
|
| 134 |
+
new_ID.append(new_representative_element)
|
| 135 |
+
# modify database
|
| 136 |
+
self.ids[idx] = new_ID
|
| 137 |
+
W.W0.s = new_representative_element
|
| 138 |
+
self.words[idx] = W
|
| 139 |
+
# refresh state spaces with greater levels
|
| 140 |
+
changes = {new_ID[0]: [(old_ID, new_ID)]}
|
| 141 |
+
del W
|
| 142 |
+
self.refresh_changes(changes)
|
| 143 |
+
# in this example self.W = self.G
|
| 144 |
+
def add_generation_rule(self, ID, generation_rule):
|
| 145 |
+
# check if ID valid
|
| 146 |
+
W = self(ID)
|
| 147 |
+
# add generation rule
|
| 148 |
+
W.G.append(generation_rule)
|
| 149 |
+
def delete_generation_rule(self, ID, generation_rule):
|
| 150 |
+
# check if ID valid
|
| 151 |
+
W = self(ID)
|
| 152 |
+
# delete generation rule
|
| 153 |
+
for item in W.G:
|
| 154 |
+
if generation_rule==item:
|
| 155 |
+
W.G.remove(item)
|
| 156 |
+
break
|
| 157 |
+
def insert(self, word):
|
| 158 |
+
assert type(word)==w
|
| 159 |
+
ID = [word.l, word.T, word.W0.s]
|
| 160 |
+
assert ID not in self.ids; assert word not in self.words
|
| 161 |
+
self.ids.append(ID); self.words.append(word)
|
| 162 |
+
def remove(self, ID):
|
| 163 |
+
# check if ID valid
|
| 164 |
+
W = self(ID)
|
| 165 |
+
# remove it
|
| 166 |
+
self.ids.remove(ID); self.words.remove(W)
|
| 167 |
+
changes = {ID[0]: [ID]}
|
| 168 |
+
del ID, W
|
| 169 |
+
# refresh state spaces with greater levels
|
| 170 |
+
from copy import deepcopy
|
| 171 |
+
levels = list(map(lambda x: x[0], self.ids))
|
| 172 |
+
levels.append(levels[-1]+1)
|
| 173 |
+
for level in range(int(*changes) + 1, levels[-1]):
|
| 174 |
+
changes[level] = []
|
| 175 |
+
index = 0
|
| 176 |
+
while index<len(self.words):
|
| 177 |
+
word = self.words[index]
|
| 178 |
+
if word.l==level:
|
| 179 |
+
for change in changes[level-1]:
|
| 180 |
+
if any(map(lambda w: change in w.s, word.W)):
|
| 181 |
+
changes[level].append(self.ids[index])
|
| 182 |
+
del self.ids[index]; del self.words[index]
|
| 183 |
+
index -= 1
|
| 184 |
+
break
|
| 185 |
+
index += 1
|