python 替换单词中的第一个元音,但不替换第二个元音,卢语

rslzwgfq  于 2023-05-16  发布在  Python
关注(0)|答案(2)|浏览(122)

所以我想做的是一个程序它可以编码和解码“Löffelsprache”这是一种德国有趣的编码
其工作原理如下:每个元音被替换为“元音本身”+短+“再次元音”默认短为“lew”;例如:hello > helewellolewo
我在代码中实现了这一点,但它很混乱,不是最优雅的解决方案。
也有规则,“耳朵”不会翻译“elewealewar”,而只是“elewear”。
所以如果后面有第二个元音,它会被忽略。
为了更好地理解,我发现这个在线生成器使用“b”作为缩写:https://de.toolpage.org/tool/bsprache
验证码:

# Get user input
inputtextraw = input("What to en/decrypt?\n").lower()
# set the message to be translated (inputtext)
inputtext = inputtextraw
# set the short
shortraw = "lew"

# Set a placeholder so the following replace functions don't replace part of the previously placed short
short_placeholder = "@"

# Find out if it's already encoded or needs to be encoded
if "a" + shortraw + "a" in inputtext or "e" + shortraw + "e" in inputtext or "i" + shortraw + "i" in inputtext or "o" + shortraw + "o" in inputtext or "u" + shortraw + "u" in inputtext:
    # DECODE
    try:
        # Replace all shorts with the placeholder
        inputtext = inputtext.replace(shortraw, short_placeholder)
        # Decode
        inputtext = inputtext.replace("e" + short_placeholder + "e", "e")
        inputtext = inputtext.replace("a" + short_placeholder + "a", "a")
        inputtext = inputtext.replace("i" + short_placeholder + "i", "i")
        inputtext = inputtext.replace("o" + short_placeholder + "o", "o")
        inputtext = inputtext.replace("u" + short_placeholder + "u", "u")
        # Save result
        result = inputtext
    except:
        print("error")
    print('\n"' + inputtextraw + '" means:\n' + result + '\n')
else:
    # ENCODE
    try:
        # Encode to vowel + placeholder + vowel
        inputtext = inputtext.replace("e", "e" + short_placeholder + "e")
        inputtext = inputtext.replace("a", "a" + short_placeholder + "a")
        inputtext = inputtext.replace("i", "i" + short_placeholder + "i")
        inputtext = inputtext.replace("o", "o" + short_placeholder + "o")
        inputtext = inputtext.replace("u", "u" + short_placeholder + "u")
        # replace the placeholder with the actual short
        result = inputtext.replace(short_placeholder, shortraw)
    except:
        print("error")
    print('\n"' + inputtextraw + '" in ' + shortraw + ' code is:\n' + result + '\n')
hi3rlvi2

hi3rlvi21#

不要使用replace()进行编码,只需迭代字符串并检查每个字符是否在字符/元音列表中。
因为你想跳过第二个元音的编码,你必须在处理下一个字符之前检查最后一个字符是否被编码:

data = []

for c in inputtext:
    if c in vList and not data:
        data.append(c+shortraw+c)
    elif c in vList and shortraw not in data[-1]:
        data.append(c+shortraw+c)
    else:
        data.append(c)

newString = ''.join(data)

最后,你必须将生成的结果join()设置为一个新的字符串。

示例

# Get user input
inputtextraw = input("What to en/decrypt?\n").lower()
# set the message to be translated (inputtext)
inputtext = inputtextraw
# set the short
shortraw = "lew"
# list of characters to check
vList = ['a','e','i','o','u']

def is_encoded(inputtext,shortraw):
    for c in vList: 
        if c+shortraw+c in inputtext:
            return True

    return False

def decode_string(inputtext,shortraw):
    for c in vList:
        inputtext = inputtext.replace(c+shortraw, '')
    return inputtext

def encode_string(inputtext,shortraw):
    data = []

    for c in inputtext:
        if c in vList and not data:
            data.append(c+shortraw+c)
        elif c in vList and shortraw not in data[-1]:
            data.append(c+shortraw+c)
        else:
            data.append(c)

    return ''.join(data)

if is_encoded(inputtext,shortraw):
    #decode
    print(f'String is encoded -> decoded string is {decode_string(inputtext,shortraw)}')
else:
    #encode
    print(f'String is not encoded\n"{inputtextraw}" in {shortraw} code is:\n {encode_string(inputtext,shortraw)} \n')
nqwrtyyt

nqwrtyyt2#

我发现一个简单的正则表达式是优雅的。
捕获一个后面没有另一个元音的元音,然后将捕获的元音替换为它本身,lew,然后再替换它本身。
代码:(Demo

re.sub(r'([a-z])(?![a-z])', r'\1lew\1', inputtext)

相关问题