本文介绍了'.'、'!' 后的大写字母和 '?'在 Python 中签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在搜索 Stack Overflow,但找不到正确的代码来纠正例如
你好!你累了吗?不,一点都不累!"
进入:
你好!你累了吗?不,一点都不累!"
解决方案
- 使用正则表达式在标点符号处拆分
- 在句子的开头用大写字母连接所有内容.
例如像这样:
导入重新text = '你好!你累了吗?一点都不!'punc_filter = re.compile('([.!?]\s*)')split_with_punctuation = punc_filter.split(text)final = ''.join([i.capitalize() for i in split_with_punctuation])打印(最终)
输出:
>>>你好!你累了吗?一点都不!I have been searching Stack Overflow but cannot find the proper code for correcting e.g.
"hello! are you tired? no, not at all!"
Into:
"Hello! Are you tired? No, not at all!"
解决方案
- Use regular expressions to split at punctuation characters
- Join everything back with capitalized letters at the start of the sentences.
For example like this:
import re
text = 'hello! are you tired? no, not at all!'
punc_filter = re.compile('([.!?]\s*)')
split_with_punctuation = punc_filter.split(text)
final = ''.join([i.capitalize() for i in split_with_punctuation])
print(final)
Output:
>>> Hello! Are you tired? No, not at all!
这篇关于'.'、'!' 后的大写字母和 '?'在 Python 中签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!