-3

我有一个文本文件中的单词列表,我想对每个单词执行 leet。例如,如果我有“Hello”这个词,这将变为“5E11O”。换句话说,程序应该能够自动替换单词中的每个字母。

请问这可能吗?谢谢

4

4 回答 4

4

这是进行一系列一对一替换的一种方法。请注意在逐字母转换之前如何应用“整个单词”转换。

replacements = ( ('hacker','haxor'), ('elite','eleet'), ('a','4'), ('e','3'),
                 ('l','1'), ('o','0'), ('t','+') )
my_string = "I am an elite hacker."
new_string = my_string
for old, new in replacements:
    new_string = new_string.replace(old, new)

print ( new_string )

给出:

I 4m 4n 3133+ h4x0r.

一个有趣的扩展是允许对某些替换进行多种选择。例如,字符l可以呈现为数字1或管道符号|

于 2012-05-08T06:19:15.477 回答
3

这完全取决于您如何定义 leet。

如果是简单的对每个字符进行1:1的翻译,比如H->5, e->E, l->1, l->1, o->0为了翻译Hello->5E11O就不难了。使用string.maketrans来定义这个字符到字符的转换。

更困难的是,如果您希望支持更完整的 leet正字法,例如hacker-lingo-->l33tsp33k这也涉及缩短字符数。

周围有各种各样的 leet 源代码工作。试一试。看看鞋子合不合适。我认为 leet 还没有通过 ISO 标准化... ;-}

于 2012-05-08T05:55:26.190 回答
0

此代码有助于:

import random

try:
    import pyperclip  #  check for util that copies text to clipboard.
except ImportError:
    pass


def main():
    print("***** L3375P34]k 63n3r470R *****")
    print("Enter your message:")
    english = input("> ")
    print()
    leetspeak = english_to_leetspeak(english)
    print(leetspeak)

    try:
        pyperclip.copy(leetspeak)
        print("(Copied leetspeak to clipboard.)")
    except NameError:
        pass


def english_to_leetspeak(message):
    """Convert english string to leetspeak"""
    char_map = {
        "a": ["4", "@", "/-\\", "^"],
        "b": ["I3", "8", "13", "|3"],
        "c": ["[", "{", "<", "("],
        "d": [")", "|)", "[)", "|>"],
        "e": ["3", "[-"],
        "f": ["|=", "ph", "|#", "/="],
        "g": ["&", "6", "(_+]", "9", "C-", "gee"],
        "h": ["#", "/-/", "[-]", "]-[", ")-(", "(-)", ":-:", "|-|", "}{"],
        "i": ["1", "[]", "!", "|", "eye", "3y3", "]["],
        "j": [",_|", "_|", "._|", "._]", ",_]", "]"],
        "k": [">|", "|<", "/<", "1<", "|c", "|(", "|{"],
        "l": ["1", "7", "|_", "|"],
        "m": ["/\\/\\", "/V\\", "JVI", "[V]", "[]V[]", "|\\/|", "^^"],
        "n": ["^/", "|\\|", "/\\/", "[\]", "<\\>", "{\\}", "|V", "/V"],
        "o": ["0", "Q", "()", "oh", "[]"],
        "p": ["|*", "|o", "?", "|^", "[]D"],
        "q": ["(_,)", "()_", "2", "O_"],
        "r": ["12", "|`", "|~", "|?", "/2", "|^", "Iz", "|9"],
        "s": ["$", "5", "z", "ehs", "es"],
        "t": ["7", "+", "-|-", "']['", '"|"', "~|~"],
        "u": ["|_|", "(_)", "V", "L|"],
        "v": ["\\/", "|/", "\\|"],
        "w": ["\\/\\/", "VV", "\\N", "'//", "\\\\'", "\\^/", "\\X/"],
        "x": ["><", ">|<", "}{", "ecks"],
        "y": ["j", "`/", "\\|/", "\\//"],
        "z": ["2", "7_", "-/_", "%", ">_", "~/_", "-\_", "-|_"],
    }
    leetspeak = ""
    for char in message:
        if char.lower() in char_map and random.random() <= 0.70:  # 70% convert
            possible_replacements = char_map[char.lower()]
            leet_replacement = random.choice(possible_replacements)
            leetspeak = leetspeak + leet_replacement
        else:
            leetspeak = leetspeak + char
    return leetspeak


if __name__ == "__main__":
    main()
    
于 2022-01-11T15:05:44.450 回答
0

我想最简单的是制作一个字符集,然后转换它

from utilitybelt import change_charset 

origspace = "abcdefghijklmnopqrstuvwxyz"
keyspace  = "abcd3fgh1jklmnopqr57uvwxyz"

print(change_charset("leetspeak",origspace, keyspace))

#l3375p3ak
于 2017-12-20T23:00:28.363 回答