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

相关推荐
曾阿伦2 分钟前
Python 时间格式化指南
python
The_Ticker3 分钟前
日股实时行情接口使用指南
java·经验分享·笔记·python·算法·区块链
m0_560396477 分钟前
用Python创建一个Discord聊天机器人
jvm·数据库·python
m0_569881479 分钟前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
2401_873204659 分钟前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
2301_7765087210 分钟前
定时任务专家:Python Schedule库使用指南
jvm·数据库·python
2301_7638919515 分钟前
使用Python控制Arduino或树莓派
jvm·数据库·python
2401_8747325322 分钟前
实战:用Python分析某电商销售数据
jvm·数据库·python
全栈凯哥22 分钟前
26.Python os.path 完全指南
python
2301_7938046931 分钟前
Python内存管理机制:垃圾回收与引用计数
jvm·数据库·python