python英文缩写单词扩写

词典

python 复制代码
# 缩略词-扩展字典
contractions = {
    "can't": "cannot",
    "won't": "will not",
    "I'm": "I am",
    "your's":"your"
    # 添加更多的缩略词及其扩展形式
}

# 扩展缩略词的函数
def expand_contractions(text):
    words = text.split()
    expanded_words = []
    for word in words:
        if word in contractions:
            expanded_words.append(contractions[word])
        else:
            expanded_words.append(word)
    expanded_text = " ".join(expanded_words)
    return expanded_text

# 测试扩展缩略词函数
text = "I can't believe it! Won't you join us? your's honer?"
expanded_text = expand_contractions(text)
print(expanded_text)
python 复制代码
I cannot believe it! Won't you join us? your honer?

掉包

python 复制代码
pip install contractions -i  https://pypi.tuna.tsinghua.edu.cn/simple
python 复制代码
import contractions

def expand_contractions(text):
    expanded_text = contractions.fix(text)
    return expanded_text

# Example usage
input_text = "I can't believe it's already Friday! you'll be learning alongside other students who are studying for a number of different degrees, typically in small classes, supporting one another to develop and succeed."
text = expand_contractions(text)
expanded_text = expand_contractions(input_text)
print(expanded_text)
python 复制代码
I cannot believe it is already Friday! you will be learning alongside other students who are studying for a number of different degrees, typically in small classes, supporting one another to develop and succeed.

组合使用:contractions只能扩写一般缩写词组,像 your's 这种就不能扩写,可以加在词典里。

相关推荐
zzzzzz3101 小时前
AI 助手最危险的不是报错,而是“看起来成功”:我给工作流加了四道保险
人工智能·python
用户8356290780512 小时前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
菜冻鱼2 小时前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd1232 小时前
python webview打包版卡死、开发版正常
开发语言·python
用户8356290780512 小时前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python
菜冻鱼2 小时前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
亿牛云爬虫专家2 小时前
爬虫调度系统设计:用 Celery 实现按媒体权重动态调整的抓取频率控制
爬虫·python·隧道代理·舆情采集·媒体权重·频率控制·ip自动轮换
Code额3 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程
Suhan424 小时前
SQLAlchemy的两种查询query()查询、stmt=select()查询
数据库·python·sql·oracle
AAA@峥4 小时前
Python 基础开篇:认识 Python、版本选择、环境部署与首个 HelloWorld
开发语言·python