You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手求助:自定义Contractions展开函数失效问题排查

问题分析与修复方案

你的缩写展开功能失效,核心是代码里的几个逻辑错误:

  • 循环提前返回:在for key in contractions循环里,第一次替换就执行了return x,导致循环只跑了一次(仅处理字典第一个键"ain't"),后续缩写完全没机会被处理。
  • 缩进错误:else语句的缩进位置错误,它现在属于for循环的else分支(循环正常结束时触发),而非if语句的分支,逻辑完全偏离预期。
  • 测试逻辑混乱:测试代码的位置虽在函数外,但函数内部的错误已经导致无法正确处理输入。

修正后的代码

contractions = {
"ain't" :"am not",
"aren't": "are not",
"can't": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not have",
"didn't": "did not",
"doesn't": "does not",
"don't": "do not",
"hadn't": "had not",
"hadn't've": "had not have",
"hasn't": "has not",    
"haven't": "have not",
"he'd": "he would",
"he'd've": "he would have",
"he'll": "he will",
"he'll've": "he will have",
"he's": "he is",
"how'd": "how did",
"how'd'y": "how do you",
"how'll": "how will",
"how's": "how does",
"i'd": "i would",
"i'd've": "i would have",
"i'll": "i will",
"i'll've": "i will have",
"i'm": "i am",
"i've": "i have",
"isn't": "is not",
"it'd": "it would",
"it'd've": "it would have",
"it'll": "it will",
"it'll've": "it will have",
"it's": "it is",
"let's": "let us",
"ma'am": "madam",
"mayn't": "may not",
"might've": "might have",
"mightn't": "might not",
"must've": "must have",
"mustn't": "must not",
"musn't've": "must not have",
"needn't": "need not",
"needn't've": "need not have",
"o'clock": "of the clock",
"oughtn't": "ought not",
"oughtn't've": "ought not have",
"shan't": "shall not",
"sha'n't": "shall not have",
"she'd": "she would",
"she'd've": "she would have",
"she'll": "she will",
"she'll've": "she will have",
"she's": "she is",
"should've": "should have",
"shouldn't": "should not",
"shouldn't've": "should not have",
"so've": "so have",
"so's": "so is",
"that'd": "that would",
"that'd've": "that would have",
"that's": "that is",
"there'd": "there would",
"they'd":  "they would",
"they'd've": "they would have",
"they'll": "they will",    
"they'll've": "they will have",
"they're": "they are",
"they've": "they have",
"to've": "to have",
"wasn't": "was not",
" u ": "you",
" ur ": "your",
" n ": " and "
}

def cont_to_exp(x):
    if isinstance(x, str):
        # 先移除反斜杠
        x = x.replace('\\', '')
        # 遍历所有缩写键,完成全部替换后再返回
        for key, value in contractions.items():
            x = x.replace(key, value)
        return x
    # 非字符串类型返回原内容
    return x

# 测试代码
test_str = "don't you know i'm not ain't gonna do that \\test"
print(cont_to_exp(test_str))

额外优化建议

  • 用isinstance(x, str)替代type(x) is str,能兼容字符串子类的情况,代码更健壮。
  • 遍历字典时直接用items()获取键值对,比单独取key再查value更高效。
  • 如果担心替换顺序问题(比如先替换短缩写导致长缩写匹配失败,例如"he'd've"可能被先替换"he'd"),可以把字典键按长度从长到短排序,确保长缩写优先匹配:
    # 在函数内添加排序逻辑
    sorted_keys = sorted(contractions.keys(), key=len, reverse=True)
    for key in sorted_keys:
        x = x.replace(key, contractions[key])
    

内容的提问来源于stack exchange,提问作者ACE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 06:57:17