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

相关推荐
傻啦嘿哟1 小时前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
B站计算机毕业设计超人1 小时前
计算机毕业设计SparkStreaming+Kafka旅游推荐系统 旅游景点客流量预测 旅游可视化 旅游大数据 Hive数据仓库 机器学习 深度学习
大数据·数据仓库·hadoop·python·kafka·课程设计·数据可视化
IT古董2 小时前
【人工智能】Python在机器学习与人工智能中的应用
开发语言·人工智能·python·机器学习
湫ccc2 小时前
《Python基础》之pip换国内镜像源
开发语言·python·pip
hakesashou2 小时前
Python中常用的函数介绍
java·网络·python
菜鸟的人工智能之路2 小时前
极坐标气泡图:医学数据分析的可视化新视角
python·数据分析·健康医疗
菜鸟学Python2 小时前
Python 数据分析核心库大全!
开发语言·python·数据挖掘·数据分析
小白不太白9502 小时前
设计模式之 责任链模式
python·设计模式·责任链模式
喜欢猪猪2 小时前
Django:从入门到精通
后端·python·django
糖豆豆今天也要努力鸭2 小时前
torch.__version__的torch版本和conda list的torch版本不一致
linux·pytorch·python·深度学习·conda·torch