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 这种就不能扩写,可以加在词典里。

相关推荐
Bugabooo15 分钟前
python打卡DAY22
开发语言·python
低维歌者35 分钟前
python训练营day27
java·开发语言·python
微刻时光43 分钟前
影刀处理 Excel:智能工具带来的高效变革
人工智能·python·低代码·自动化·excel·rpa·影刀rpa
大帅不是我1 小时前
Python多进程编程执行任务
java·前端·python
Fu_lucas1 小时前
Python Logging 模块完全指南
开发语言·python
Eiceblue1 小时前
Python 在Excel单元格中应用多种字体样式
开发语言·vscode·python·pycharm·excel
Superstarimage3 小时前
使用conda创建python虚拟环境,并自定义路径
windows·python·conda
菜鸡码农,喵。3 小时前
已经装了pygame但pycharm显示没有该模块/软件包无法加载出来下载pygame
python·pycharm·pygame
小羊Linux客栈3 小时前
自动化:批量文件重命名
运维·人工智能·python·自动化·游戏程序
shykevin6 小时前
python开发Streamable HTTP MCP应用
开发语言·网络·python·网络协议·http