2
import re
text = input("Enter text: ")
text = text.lower()
len = len(text)
lenid = len - 1

def translate(text):
    translation = ""
    slocid = text.index("s")
    for letter in text:
        if (text[lenid] == "s") or (text[slocid + 1] == " "):
          translation = re.sub(r"s\b", lambda m: m.group().replace("s", "ς"), text)
        if letter == "s":
          translation = translation.replace("s", "σ")
        if letter == "a":
            translation = translation + "α"
        else:
            translation = translation + letter
    return translation

print(translate(text))

I want to make a program where, when I enter a text, I get back the same text but

  • all the "s" at the end of a word are replaced with "ς"
  • if they're not at the end of the word, I want the "s" to be replaced with "σ".
  • If the letter is "a" I want it replaced with "α".

I'm trying to make a Greeklish to Greek translator and it just seems I messed up.


input: "as"

output: "aςs"

what I want: "ας"


input: "sa"

output: "sa"

what I want: "σα"


input: "sakas"

output: "σakaςs"

what I want: "σαkας"

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Manos Stg
  • 84
  • 7

2 Answers2

2

You currently translate the text character by character, but your regex and replace work on the whole string so you can just apply them to the whole string instead.

def translate(text):
    return re.sub(r"s\b", "ς", text)\
           .replace("s", "σ")\
           .replace("a", "α")

This will first sub your regex to replace ending 's' characters, then replaces the remaining 's' characters and then the 'a' characters.

Henry
  • 3,472
  • 2
  • 12
  • 36
  • Thank you, also what is the exact purpose of r"s\b"? – Manos Stg Jul 16 '22 at 15:59
  • Hi, for reference, this deals with the same problem and is the function I am using: https://stackoverflow.com/a/15448887/1793606 – HCLivess Jul 16 '22 at 23:30
  • `s\b` is a regular expression. It means a literal s character followed by a word boundary. This basically just means it will only match s characters at the end of a word. The r in front of the string makes it a raw string so you don't have to use double backslashes. – Henry Jul 16 '22 at 23:50
  • 1
    You can see an interactive example of the regular expression [here](https://regex101.com/r/qGOIK4/1). – Henry Jul 16 '22 at 23:55
-1

If you wanna understand the logic behind, the following code will help you understand how does it works.

def translate(text):
# checks 's' is present in 'text'
if 's' in text:
    # checks if 's' appears at the end of 'text'
    if text[-1] == 's':
        # checks if 's' appears between 'tex'
        if 's' in text[:-1]:
            text = text.replace('s', 'σ')

        # replace 's' with 'ς'
        text = text[:-1] + 'ς'
    else:
        # replace 's' with 'σ'
        text = text.replace('s', 'σ')

# checks 'a' is present in 'text'
if 'a' in text:
    # replace 'a' with 'α'
    text = text.replace('a', 'α')

return text
Asad Hussain
  • 129
  • 4