Python Forum

Full Version: Trouble with Regex Translator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am trying to build a translator using a regex compiler. Here is the code I have so far:

try:
    import re

    regex = re.compile(
        r"cei"
        r"|[Aa]"
        r"|sh"
        r"|[Cc]"
        r"|[Ss]"
        r"|[Bb]"
        r"|[Dd]"
        r"|[Ee]"
        r"|[Ff]"
        r"|[Gg]"
        r"|[Hh]"
        r"|[Ii]"
        r"|[Jj]"
        r"|[Kk]"
        r"|[Ll]"
        r"|[Mm]"
        r"|[Nn]"
        r"|[Oo]"
        r"|[Pp]"
        r"|[Qq]"
        r"|[Rr]"
        r"|[Ss]"
        r"|[Tt]"
        r"|[Uu]"
        r"|qu"
)
    
    mapping = {}

    def register(*s):
        def decorate(func):
            for k in s:
                mapping[k] = func
            return func
        return decorate

    @register("cei")
    def _():
        return "си"
    
    @register("C", "c")
    def _():
        return "к"

    @register("B", "b")
    def _():
        return "б"

    @register("D", "d")
    def _():
        return "д"

    @register("A", "a")
    def _():
        return "а"

    @register("S", "s")
    def _():
        return "с"

    @register("E", "e")
    def _():
        return "е"

    @register("F", "f")
    def _():
        return "ф"

    @register("G", "g")
    def _():
        return "г"

    @register("H", "h")
    def _():
        return "х"

    @register("I", "i")
    def _():
        return "и"

    @register("J", "j")
    def _():
        return "дж"

    @register("K", "k")
    def _():
        return "к"

    @register("L", "l")
    def _():
        return "л"

    @register("M", "m")
    def _():
        return "м"

    @register("N", "n")
    def _():
        return "н"

    @register("O", "o")
    def _():
        return "о"

    @register("P", "p")
    def _():
        return "п"

    @register("Q", "q")
    def _():
        return "к"

    @register("U", "u")
    def _():
        return "у"

    @register("qu")
    def _():
        return "кв"
    
    @register("sh")
    def _():
        return "ш"

    def action(match):
        return mapping[match.group(0)]()

    def translate(phrase):
        return regex.sub(action, phrase)

    if __name__ == '__main__':
        result = translate(input(''))
        print(result)

except KeyError as err:
     print (err)
except UnboundLocalError as err:
    print (err)
except IndexError as err:
    print (err)
except NameError as err:
    print (err)
It prints "ш" when I input "sh", and it prints "си" when I type "cei". However, for some reason it does not type "кв" when I type "qu". Can somebody help me out and see what exactly is wrong with my code? Thanks.
I'm guessing it's getting caught by your mapping for 'q' before it gets caught by your mapping for 'qu'. This is the same problem you have been having all along with this code: conflicts between your single character translations and your multiple character translations. I think the best solution would be to separate out all of the multiple character translations. Do them first. Then do the single character translations.
(Dec-15-2018, 02:39 PM)ichabod801 Wrote: [ -> ]I'm guessing it's getting caught by your mapping for 'q' before it gets caught by your mapping for 'qu'. This is the same problem you have been having all along with this code: conflicts between your single character translations and your multiple character translations. I think the best solution would be to separate out all of the multiple character translations. Do them first. Then do the single character translations.
Thanks! It's working now again.
Yes you need to move the r"|qu" before the r"|[Qq]"