-
Notifications
You must be signed in to change notification settings - Fork 0
/
katestable.py
313 lines (288 loc) · 9.54 KB
/
katestable.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# katestable
'''
:author: madkote
:contact: madkote(at)bluewin.ch
K-testable languages with DFA
-----------------------------
:TODO add here some description
Usage
-----
>>> from katestable import KTestable
>>> kt = KTestable.build(3, [...])
>>> kt.detect('abba')
'''
VERSION = (0, 2, 1)
__all__ = ['KTestable']
__author__ = 'madkote <madkote(at)bluewin.ch>'
__version__ = '.'.join(str(x) for x in VERSION)
# ============================================================================
# INNER
# ============================================================================
class DFA(object):
'''
(Simple) Deterministic Finite Automata.
'''
def __init__(self, alphabet, accepts, start, states, trans):
'''
The constructor.
:param alphabet: Alphabet of DFA
:param accepts: Final states
:param start: Start state
:param states: States
:param trans: Transitions (delta)
'''
self.alphabet = alphabet
self.accepts = accepts
self.current_state = start
self.start = start
self.states = states
self.trans = trans
def reset(self):
'''
Reset DFA: set the current state to the start state.
'''
self.current_state = self.start
def status(self):
'''
Get the status of the DFA.
:return: True if the current state is in the set of the final states,
otherwise False.
'''
return self.current_state in self.accepts
class KDFA(DFA):
'''
DFA built from K-Testable-Machine
>>> dfa = DFA.build(ktmachine)
>>> dfa.detect('00011100')
'''
STATE_FAILURE = 'FAILURE'
def _detect_char(self, c):
'''
Detect the character: find the transition for the given character.
:param c: Character to be detected
'''
if c in self.alphabet:
# new state
self.current_state = self.trans[self.current_state][c]
else:
# unknown character
self.current_state = self.STATE_FAILURE
def _detect_string(self, s):
'''
Detect the string: the final state is crucial.
:param s: String to be detected.
'''
for c in s:
self._detect_char(c)
def detect(self, s):
'''
Detect the string (string should belong to the language).
:param s: String to be analyzed
:return: True if the string is detected and belongs to the language,
otherwise False.
'''
self.reset()
self._detect_string(s)
return self.status()
@staticmethod
def build(ktmachine):
'''
Build the DFA from the K-Testable-Machine.
-# merge prefixes and short strings :: I&C
-# create states as prefixes from I&C
-# create states from valid strings T as suff(k-1) and pref(k-1)
-# initialize transitions
-# transitions for I&C
-# transitions for set of valid strings T
-# add state from suffixes to accepted states
-# add state from short string to accepted states
:param ktmachine: K-Testable-Machine
:return: DFA from K-Testable-Machine
'''
accepts = []
start = ''
failure = KDFA.STATE_FAILURE
states = [start, failure]
trans = {}
# merge I&C and create states as prefixes from I&C
for x in set(ktmachine.prefixes) | set(ktmachine.shortstr):
for i in range(1, len(x) + 1):
state = x[:i]
if state not in states:
states.append(state)
# create states from valid strings T as suff(k-1) and pref(k-1)
for x in ktmachine.validstr:
if len(x) > 1:
tpref = x[:len(x) - 1]
tsuff = x[1:]
if tpref not in states:
states.append(tpref)
if tsuff not in states:
states.append(tsuff)
# initialize transitions
for x in states:
trans[x] = {}
for y in ktmachine.alphabet:
trans[x][y] = failure
# transitions for set of I and C
for x in set(ktmachine.prefixes) | set(ktmachine.shortstr):
if not x or x == failure:
continue
for i in range(0, len(x)):
char = x[i]
if i == 0:
source = ''
else:
source = x[:i]
dest = x[:i + 1]
trans[source][char] = dest
# transitions for set of valid strings T
for x in ktmachine.validstr:
if len(x) < 2:
# suffix and prefix are required
continue
source = x[:len(x) - 1]
dest = x[1:]
char = x[-1]
trans[source][char] = dest
# add state from suffixes to accepted states
for x in ktmachine.suffixes:
if x not in accepts:
accepts.append(x)
# add state from short string to accepted states
for x in ktmachine.shortstr:
if x not in accepts:
accepts.append(x)
# DFA
return KDFA(ktmachine.alphabet, accepts, start, states, trans)
class KTMachine(object):
'''
K-Testable-Machine.
The machine can be built from the language and the K-value.
Usage:
>>> KTMachine.build(k, language)
'''
def __init__(self, k, alphabet, prefixes, shortstr, suffixes, validstr):
'''
The constructor of the machine
for K, M=(alphabet, prefixes, shortstr, suffixes, validstr).
:param k: K value
:param alphabet: Alphabet
:param prefixes: Prefixes (k-1)
:param shortstr: short strings (<k)
:param suffixes: suffixes (k-1)
:param validstr: allowed/valid strings (k)
'''
self.k = k
self.alphabet = alphabet
self.prefixes = prefixes
self.shortstr = shortstr
self.suffixes = suffixes
self.validstr = validstr
@staticmethod
def build(k, language):
'''
Build the machine from a language.
-# get all short strings (size < k)
-# get all prefixes and suffixes (k-1)
-# extract allowed/valid strings (size == k)
-# build alphabet
:param k: K value
:param language: The language as a list of language's words
:return: The K-Testable-Machine
'''
alphabet = []
prefixes = []
shortstr = []
suffixes = []
validstr = []
# build
for word in language:
# get all short strings (size < k)
if len(word) < k:
if word not in shortstr:
shortstr.append(word)
# get all prefixes and suffixes (k-1)
if len(word) >= (k - 1):
p = word[:k - 1]
s = word[len(word) - k + 1:]
if p not in prefixes:
prefixes.append(p)
if s not in suffixes:
suffixes.append(s)
# extract allowed strings (size == k)
if len(word) >= k:
for i in range(0, len(word) - k + 1):
tword = word[i:i + k]
if tword not in validstr:
validstr.append(tword)
# build alphabet by each character
for c in word:
if c not in alphabet:
alphabet.append(c)
# done
return KTMachine(k, alphabet, prefixes, shortstr, suffixes, validstr)
# ============================================================================
# PUBLIC
# ============================================================================
class KTestable(object):
'''
K-Testable public facade.
'''
def __init__(self, kdfa):
'''
The constructor with DFA
:param kdfa: K-testable DFA
'''
self.kdfa = kdfa
def detect(self, s):
'''
Detect the string (string should belong to the language).
:param s: String to be analyzed
:return: True if the string is detected and belongs to the language,
otherwise False.
'''
return self.kdfa.detect(s)
@staticmethod
def build(k, language):
'''
Build the machine from a language.
:param k: K value
:param language: The language as a list of language's words
:return: The K-Testable
'''
return KTestable(KDFA.build(KTMachine.build(k, language)))
# ============================================================================
# DEMO
# ============================================================================
def demo():
'''
Simple demo
'''
k = 3
language = ['a', 'aa', 'abba', 'abbbba', 'aaabbbbba']
kt = KTestable.build(k, language)
print('=' * 30)
print('=== Demo')
print('=' * 30)
print(f'K-value = {k}')
for x in sorted(language):
print('{0:20}'.format(x))
# test
language = {x: True for x in language}
language.update({'aba': False,
'abc': False,
'aaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaa': False,
'aabbbaaac': False,
'aaaaabbbbbbbbba': True})
print('=' * 30)
print('=== Detect')
print('=' * 30)
for x in sorted(language):
result = kt.detect(x)
print('{0:20} = {1}'.format(x, result))
assert (language[x] == result) # nosec B101
if __name__ == '__main__':
demo()