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

相关推荐
dreams_dream33 分钟前
Python 的 GIL 是什么?有什么影响?
开发语言·python
小白菜又菜37 分钟前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
python·算法·leetcode
多恩Stone41 分钟前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
Loo国昌1 小时前
【AI应用开发实战】09_Prompt工程与模板管理:构建可演进的LLM交互层
大数据·人工智能·后端·python·自然语言处理·prompt
遨游xyz2 小时前
Trie树(字典树)
开发语言·python·mysql
重启编程之路2 小时前
AlphaLens Pro V14.0 商业级量化推演终端 | 功能白皮书
python
二十雨辰2 小时前
[python]-生成器和正则
python
Loo国昌2 小时前
【AI应用开发实战】06_向量存储与EmbeddingProvider设计
人工智能·后端·python·语言模型·自然语言处理·prompt
两万五千个小时2 小时前
构建mini Claude Code:07 - 一切皆文件:持久化任务系统
人工智能·python·架构
番茄去哪了2 小时前
python基础入门(一)
开发语言·数据库·python