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

相关推荐
岁月宁静38 分钟前
驾驭 AI 这匹野马:深入解析智能体 Harness 工程
vue.js·python
星恒随风2 小时前
Python 基础语法详解(一):从表达式、变量到数据类型
开发语言·笔记·python·学习
888CC++2 小时前
java 并发编程
java·开发语言·python
Dxy12393102162 小时前
python缩放图片如何保证图片质量
python
ZHW_AI课题组2 小时前
腾讯云调用IP定位
人工智能·python·机器学习
zhaoshuzhaoshu3 小时前
Python文件操作详细解析带例子
python
醒醒该学习了!3 小时前
Anaconda安装教程+第一个python例子
开发语言·python
linyanRPA3 小时前
影刀RPA+Python店群自动化实战:自研环境隔离引擎,200店铺并发不卡不串号
python·自动化·rpa
郑洁文5 小时前
面向Web安全的Python渗透测试系统设计与实现
python·安全·web安全