Python Forum
Trouble with Regex Translator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Regex Translator
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
Yes you need to move the r"|qu" before the r"|[Qq]"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,618 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  i want to write symbole as varibales in python to make translator rachidel07 9 3,529 Feb-03-2021, 09:57 PM
Last Post: nilamo
  letter translator (or someting) Obsilion 4 2,465 Apr-28-2020, 12:40 PM
Last Post: deanhystad
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,109 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Output discrepancy when building Translator skrivver99 17 6,677 Nov-26-2018, 01:22 AM
Last Post: ichabod801
  Help with pig latin translator DragonG 1 2,275 Nov-01-2018, 03:57 AM
Last Post: ichabod801
  PigLatin Sentence Translator Help 2skywalkers 7 5,829 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Pig Latin Translator RickyWilson 1 3,195 Dec-02-2017, 08:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020