def symmAndRefl(t, ipt=False): """ Return the transducer t | t.inverse, if ipt is True; return the transducer t | t.inverse | id, otherwise :type t: SFT :type ipt: bool :rtype: SFT""" t1 = t | t.inverse() if ipt is True: return t1 t2 = SFT() s0 = t2.addState() t2.addInitial(s0) t2.addFinal(s0) for sym in t.Sigma: t2.addTransition(s0, sym, sym, s0) return t1 | t2
def startTRANSSemRule(self, lst, context=None): """ :param lst: :param context:""" if self.TRtype is None: new = SFT() elif self.TRtype == "GFT": new = GFT() else: raise common.TRError new.Sigma = self.alphabet new.Output = self.alphabetOut while self.states: x = self.states.pop() new.addState(x) while self.initials: x = self.initials.pop() new.addInitial(new.stateIndex(x)) while self.finals: x = self.finals.pop() new.addFinal(new.stateIndex(x)) while self.transitions: (x1, x2, x3, x4) = self.transitions.pop() new.addTransition(new.stateIndex(x1), x2, x3, new.stateIndex(x4)) self.theList.append(new) self.initLocal()
def createInputAlteringSIDTrans(n, sigmaSet): """Create an input-altering SID transducer based :param int n: max number of errors :param set sigmaSet: alphabet :return: a transducer representing the SID channel :rtype: SFT""" new = SFT() new.setSigma(sigmaSet) new.setOutput(sigmaSet) init = new.stateIndex((0, None), True) new.addInitial(init) for sy in new.Sigma: new.addTransition(init, sy, sy, init) i = 1 while i <= n: d1 = new.stateIndex((i, None), True) new.addFinal(d1) for s in new.Sigma: new.addTransition(d1, s, s, d1) d = new.stateIndex((i, s), True) new.addFinal(d) if i == 1: new.addTransition(init, s, Epsilon, d) for s1 in new.Sigma - {s}: new.addTransition(init, s, s1, d1) else: bar = new.stateIndex((i - 1, None)) new.addTransition(bar, s, Epsilon, d1) new.addTransition(bar, Epsilon, s, d1) for s1 in new.Sigma: foo = new.stateIndex((i - 1, s)) new.addTransition(foo, s1, Epsilon, d) if s1 != s: new.addTransition(bar, s, s1, d1) new.addTransition(foo, Epsilon, s1, d1) for s2 in new.Sigma: new.addTransition(foo, s2, s1, d1) for s1 in new.Sigma - {s}: new.addTransition(d, s1, s1, d1) i += 1 return new
def notSatisfiesW(self, aut): """Test whether the language is a code. :param DFA|NFA aut: the automaton :return: two different factorizations of the same word :rtype: tuple of list""" def _findFactors(w, m): l = [] i1 = 0 while True: j = m.find('1', i1) if j < 0: break l.append(w[i1:j + 1]) i1 = j + 1 return l n = aut.toNFA() # .eliminateEpsilonTransitions() for i in n.Initial: if i in n.Final: return [Epsilon], [Epsilon, Epsilon] t = SFT() t.States = copy.deepcopy(n.States) t.setInitial(n.Initial) t.setFinal(n.Initial) t.setSigma(n.Sigma) t.setOutput(['1', '0']) for si in n.delta: for sym in n.delta[si]: for so in n.delta[si][sym]: if so not in n.Final: t.addTransition(si, sym, '0', so) else: t.addTransition(si, sym, '0', so) for ss in t.Initial: t.addTransition(si, sym, '1', ss) t.trim() foo = t.nonFunctionalW() if foo == (None, None, None): return None, None else: return _findFactors(foo[0], foo[1]), _findFactors(foo[0], foo[2])
def trajToTransducer(traj, Sigma): """ Input Altering Tranducer corresponding to a Trajectory :param NFA traj: trajectory language :param set Sigma: alphabet :rtype: SFT""" t = SFT() t.Sigma = Sigma for s, sn in enumerate(traj.States): no = t.stateIndex((sn, False), True) yes = t.stateIndex((sn, True), True) if s in traj.Final: t.addFinal(yes) if s in traj.Initial: t.addInitial(no) for b in traj.delta.get(s, {}): for c in Sigma: for st in traj.delta[s][b]: fNo = t.stateIndex((traj.States[st], False), True) fYes = t.stateIndex((traj.States[st], True), True) if b == "0": t.addTransition(no, c, c, fNo) t.addTransition(yes, c, c, fYes) if b == "1": t.addTransition(no, Epsilon, c, fYes) t.addTransition(yes, Epsilon, c, fYes) return t