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

相关推荐
GHZhao_GIS_RS4 分钟前
python中的sort和sorted用法汇总
python·排序·列表
永恒的溪流9 分钟前
环境出问题,再修改
pytorch·python·深度学习
ruxshui9 分钟前
Python多线程环境下连接对象的线程安全管理规范
开发语言·数据库·python·sql
大模型玩家七七16 分钟前
向量数据库实战:从“看起来能用”到“真的能用”,中间隔着一堆坑
数据库·人工智能·python·深度学习·ai·oracle
2301_7634724620 分钟前
使用PyQt5创建现代化的桌面应用程序
jvm·数据库·python
爱学习的阿磊21 分钟前
Web开发与API
jvm·数据库·python
qq_1927798726 分钟前
Python多线程与多进程:如何选择?(GIL全局解释器锁详解)
jvm·数据库·python
naruto_lnq29 分钟前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
工程师老罗40 分钟前
Pytorch中的优化器及其用法
人工智能·pytorch·python
2301_822365031 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python