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

相关推荐
喵手3 分钟前
Python爬虫实战:开放数据多格式入仓 - 构建统一数据管道(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·开放数据多格式·统一数据管道
焦糖夹心14 分钟前
python中,怎么同时输出字典的键和值?
开发语言·python
ValhallaCoder28 分钟前
hot100-回溯II
数据结构·python·算法·回溯
2401_8288906435 分钟前
正/余弦位置编码 Sinusoidal Encoding
python·自然语言处理·transformer·embedding
流烟默40 分钟前
Python爬虫之下载豆瓣电影图片到本地
爬虫·python
喵手1 小时前
Python爬虫实战:构建“时光机”——网站数据增量监控与差异分析系统!
爬虫·python·爬虫实战·差异分析·零基础python爬虫教学·网站数据增量·网站数据增量监控系统
Katecat996631 小时前
SAR图像火情与烟雾检测:Cascade-Mask-RCNN与RegNetX模型融合详解
python
禁默1 小时前
零基础全面掌握层次分析法(AHP):Python实现+论文加分全攻略
python·数学建模·matlab
深蓝电商API2 小时前
爬虫数据导出 Excel:openpyxl 高级用法
爬虫·python·openpyxl
reasonsummer2 小时前
【教学类-74-05】20260216剪影马(黑色填充图案转黑线条白填充)
python