Python Forum
Output discrepancy when building Translator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output discrepancy when building Translator
#11
(Nov-24-2018, 09:30 PM)j.crater Wrote: Hello, in case of "cei" you use "is" instead of "in", as is the case in other elifs
elif letter is "cei":
This cannot work, because for loop iterates over characters, so no character will equal "cei".
How do I get rid of the "e" and "и" after that then?
Is there any way something similar to this code could work?:
        elif letter in "Cc" and translation[+2:] == "ei":
            translation = translation[:+2] + "си"
Reply
#12
I can't think of a robust solution right off the bat. But you might be able to work something out with string's replace method.
Reply
#13
First of all, stop making new threads about this problem. I'm tired of you trying to get different answers.

I showed you how to fix the cei problem by looking backwards in the translation. You are trying to do that by looking forward in the translation. That's like looking forward in time. You can't do that. You could look forward in the original string, but your loop is not set up for that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
(Nov-24-2018, 10:04 PM)ichabod801 Wrote: First of all, stop making new threads about this problem. I'm tired of you trying to get different answers. I showed you how to fix the cei problem by looking backwards in the translation. You are trying to do that by looking forward in the translation. That's like looking forward in time. You can't do that. You could look forward in the original string, but your loop is not set up for that.
Your solution did not work. In case you don't see, I copied your exact text and put it into the code. I had to comment it out because it didn't work and I didn't want it getting in the way.
Reply
#15
(Nov-24-2018, 10:18 PM)skrivver99 Wrote: Your solution did not work.

Then the correct thing to do would have been to post that my solution didn't work and exactly how it didn't work in the previous thread. Did you use the correction that I posted after finding two different e's in your code? Because I just ran that and it works.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
(Nov-24-2018, 11:15 PM)ichabod801 Wrote:
(Nov-24-2018, 10:18 PM)skrivver99 Wrote: Your solution did not work.
Then the correct thing to do would have been to post that my solution didn't work and exactly how it didn't work in the previous thread. Did you use the correction that I posted after finding two different e's in your code? Because I just ran that and it works.

I posted the exact code including your correction. This is the code that didn't work. Here it is again:

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "Aa":
            translation = translation + "a"
        elif letter.lower() in "b":
            if letter.isupper():
                translation = translation + "Б"
            else:
                translation = translation + "б"
        elif letter in "Ll":
            translation = translation + "л"
        elif letter in 'Ii' and translation[-2:] == 'ке':
            translation = translation[:-2] + "cи"
        elif letter in "Cc" and translation[+2:] == "ei":
            translation = translation[:+2] + "си"
        elif letter in 'Ii':
            translation = translation + "и"
        elif letter in 'Ii':
            translation = translation + "и"
        elif letter in "Ii":
            translation = translation + "и"
        elif letter in "Ee":
            translation = translation + "е"
        elif letter in "Ff":
            translation = translation + "ф"
        elif letter in "cei":
            translation = translation + "си"
        elif letter in "Dd":
            translation = translation + "д"
        elif letter in "Gg":
            translation = translation + "г"
        else:
            translation = translation + letter
            '''
This list
Needs to be completed
'''
    return translation
Reply
#17
I would simply use a regex for this task. It will be much faster at run time than a loop over the letters and a huge sequence of ifs
import re

regex = re.compile(
    r"cei"
    r"|[Aa]"
    r"|[Bb]"
    r"|[Cc]"
    r"|[Ll]"
    r"|[Ii]"
    r"|[Ee]"
    r"|[Dd]"
    r"|[Gg]"
    r"|[Ff]"
)

mapping = {}

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

@register("cei")
def _():
    return  "си"

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

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

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

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

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

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

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

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

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

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

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

if __name__ == '__main__':
    result = translate('ceiling')
    print(result)
The fundamental theorem of software engineering Wrote:We can solve any problem by introducing an extra level of indirection.
Reply
#18
That's not the code with my correction. This is:

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "Aa":
            translation = translation + "a"
        elif letter in "Bb":
            translation = translation + "б"
        elif letter in "Cc":
            translation = translation + "к"
        elif letter in "Ll":
            translation = translation + "л"
        elif letter in 'Ii' and translation[-2:] == 'ке':
            translation = translation[:-2] + "cи"
        elif letter in 'Ii':
            translation = translation + "и"
        elif letter in "Ee":
            translation = translation + "е"
        elif letter in "cei":
            translation = translation + "си"
        else:
            translation = translation + letter
    return translation


print(translate(input("Enter word: ")))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  discrepancy with datetime with timezone XJia 3 792 Sep-03-2023, 02:58 PM
Last Post: deanhystad
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,619 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  i want to write symbole as varibales in python to make translator rachidel07 9 3,532 Feb-03-2021, 09:57 PM
Last Post: nilamo
  letter translator (or someting) Obsilion 4 2,466 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,110 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Trouble with Regex Translator skrivver99 3 2,772 Dec-15-2018, 03:55 PM
Last Post: Gribouillis
  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,830 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Pig Latin Translator RickyWilson 1 3,196 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