def makeCode(self, N, ell, s, n=2000, ov_free=False): """Returns an NFA and a list W of up to N words of length ell, such that the NFA accepts W, which is an error-detecting language. The alphabet to use is {0,1,...,s-1}. where s <= 10. :param int N: the number of words to construct :param int ell: the codeword length :param int s: the alphabet size (must be <= 10) :param int n: number of tries when needing a new word :return: an automaton and a list of strings :rtype: tuple""" w = pickFrom(s, ell) if ov_free: while not overlapFreeP(w): w = pickFrom(s, ell) W = [w] aut = fl.FL(W).toNFA() cnt = 1 self.AutDetect = (self.Aut | self.Aut.inverse()) while cnt < N: w = self.notMaxStatW(aut, ell, n, ov_free) if w is None: break W.append(w) aut = fl.FL(W).toNFA() cnt += 1 return aut, W
def evalWordP(self, wp): """Tests whether the transducer returns the second word using the first one as input :param tuple wp: pair of words :rtype: bool""" import fl (win, wout) = wp inT = self.inIntersection(fl.FL([win]).MADFA()) return not inT.outIntersection(fl.FL([wout]).MADFA()).emptyP()
def runOnWord(self, word): """Returns the automaton accepting the outup of the transducer on the input word :param word: the word :rtype: NFA""" lang = fl.FL([word]) return self.runOnNFA(lang.trieFA().toNFA())
def words2NFA(lst): ''' lst is a list of strings. the function creates an NFA accepting exactly these strings It returns the the NFA ''' return fl.FL(lst).trieFA().toNFA()
def constructCode(n, l, p, ipt=False, seed=None): """ Returns up to n words of length l satisfying the property p, the first one being seed. If ipt is True, the property is assumed to be input-preserving transducer type :type p: IATProp|IPTProp :type l: int :type n: int :type seed: str :type ipt: bool :rtype: list""" if n == 0: return [] if l == 0: return [Epsilon] t = symmAndRefl(p.Aut, ipt) # make a version of p.Aut that is symmetric and reflexive if seed is None or len(seed) != l: a0 = list(t.Sigma)[0] seed = "".join([a0 for _ in range(l)]) lt = [seed] cnt, more = 1, True while more and cnt < n: a = fl.FL(lt).trieFA().toNFA() b = t.runOnNFA(a) (w, i) = notUniversalStatW(b, l) if w is None: more = False else: cnt += 1 lt.append(w) return lt
def addToCode(self, aut, N, n=2000): """Returns an NFA and a list W of up to N words of length ell, such that the NFA accepts L(aut) union W, which is an error-detecting language. ell is computed from aut :param NFA aut: the automaton :param int N: the number of words to construct :param int n: number of tries when needing a new word :return: an automaton and a list of strings :rtype: tuple""" a_new = aut.dup() W = [] u = aut.witness() if u is None: return a_new, W ell = len(u) cnt = 0 while cnt < N: w = self.notMaxStatW(a_new, ell, n) if w is None: break else: W.append(w) a_new = a_new | fl.FL([w]).trieFA().toNFA() cnt += 1 return a_new, W
def makeCodeO(self, N, ell, s, n=2000, end=None, ov_free=False): """Returns an NFA and a list W of up to N words of length ell, such that the NFA accepts W, which is an error-detecting language. The alphabet to use is {0,1,...,s-1}. where s <= 10. :param int N: the number of words to construct :param int ell: the codeword length :param int s: the alphabet size (must be <= 10) :param int n: number of tries when needing a new word :param Word end: a Word or None that should much the end of code words :param Boolean ov_free: if True code words much be overlap free :return: an automaton and a list of strings :rtype: tuple Note: not ov_free and end defined simultaneously Note: end should be a Word """ a = fl.sigmaInitialSegment(set([str(i) for i in range(s)]), ell, True) g = fl.RndWGen(a) w = g.next() if ov_free and end is not None: l_end = len(end) end = list(end) we = w[-l_end:] while we != end or not overlapFreeP(w): w = g.next() we = w[-l_end:] if ov_free and end is None: while not overlapFreeP(w): w = g.next() if not ov_free and end is not None: l_end = len(end) end = list(end) we = w[-l_end:] while we != end: w = g.next() we = w[-l_end:] W = [w] aut = fl.FL(W).MADFA() #toNFA() cnt = 1 self.AutDetect = (self.Aut | self.Aut.inverse()) while cnt < N: b = self.AutDetect.runOnNFA(aut).trim() tr = 1 while tr <= n: w = g.next() if ov_free: if not overlapFreeP(w): continue if end is not None: if w[-l_end:] != end: continue if not b.evalWordP(w): # == False: W.append(w) aut = fl.FL(W).MADFA() #toNFA() cnt += 1 break tr += 1 if tr > n: break return aut, W