python函数 - 将短信语言转换为英语?

snw*_*e00 1 python dictionary

我在创建一个使用短信缩写字典的函数时遇到了麻烦,并使用字典来编写可以翻译成英语的函数.例如:"yru l8?"翻译为"你为什么迟到?"所以到目前为止我有这个:(我不需要每一个发短信的短语)

def text(string):  
    textDict={'y':'why', "l8":'late','u':'you','gtg':'got to go', 'lol': 'laugh out loud', 'ur': 'your',}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

g.d*_*d.c 6

如果你有替换字典,你可以这样做:

replacements = {
  'lol': 'laugh out loud',
  'y': 'why',
  'l8': 'late',
  'u': 'you',
  'r': 'are'
}

s1 = 'y r u l8'

s2 = ' '.join([replacements.get(w, w) for w in s1.split()])
Run Code Online (Sandbox Code Playgroud)