ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 08 Jul 2018 17:39:13 +0200Is there a way to have Context Sensitive Morphisms?https://ask.sagemath.org/question/42856/is-there-a-way-to-have-context-sensitive-morphisms/I be been using WordMorphism and I was wondering if there is a way to have context sensitive substitutions?
An example would be b”a”c -> ab so a is replaced iff bac appears.
WordMorphism(“bac->ab”)Sat, 07 Jul 2018 20:51:10 +0200https://ask.sagemath.org/question/42856/is-there-a-way-to-have-context-sensitive-morphisms/Answer by vdelecroix for <p>I be been using WordMorphism and I was wondering if there is a way to have context sensitive substitutions? </p>
<p>An example would be b”a”c -> ab so a is replaced iff bac appears.</p>
<pre><code>WordMorphism(“bac->ab”)
</code></pre>
https://ask.sagemath.org/question/42856/is-there-a-way-to-have-context-sensitive-morphisms/?answer=42864#post-id-42864Yes and no. You can always recode your word on a larger alphabet and apply a substitution on this larger alphabet. For your example
sage: W = Words('abc')
sage: w = W('abbabacacabcabcaacabcbac')
sage: WW = Words('abcA')
sage: ww = WW(['A' if w[i] == 'a' and i > 0 and w[i-1] == 'b' and i < len(w)-1 and w[i+1] == 'c' else w[i] for i in range(len(w))])
sage: ww
word: abbabAcacabcabcaacabcbAc
sage: f = WordMorphism('a->a,b->b,c->c,A->ab', domain=WW, codomain=W)
sage: f(ww)
word: abbababcacabcabcaacabcbabc
Sun, 08 Jul 2018 17:39:13 +0200https://ask.sagemath.org/question/42856/is-there-a-way-to-have-context-sensitive-morphisms/?answer=42864#post-id-42864