我正在尝试编写一个程序,将每个句子的第一个字母大写。这是我到目前为止所拥有的,但我不知道如何在句子之间添加句号。例如,如果我输入:
你好。再见
输出是
你好再见
并且期间已经消失。
string=input('Enter a sentence/sentences please:')
sentence=string.split('.')
for i in sentence:
print(i.capitalize(),end='')
你可以使用nltk进行句子切分 http://www.nltk.org/book/ch03.html#sentence-segmentation:
#!/usr/bin/env python3
import textwrap
from pprint import pprint
import nltk.data # $ pip install http://www.nltk.org/nltk3-alpha/nltk-3.0a3.tar.gz
# python -c "import nltk; nltk.download('punkt')"
sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
text = input('Enter a sentence/sentences please:')
print("\n" + textwrap.fill(text))
sentences = sent_tokenizer.tokenize(text)
sentences = [sent.capitalize() for sent in sentences]
pprint(sentences)
Output
Enter a sentence/sentences please:
a period might occur inside a sentence e.g., see! and the sentence may
end without the dot!
['A period might occur inside a sentence e.g., see!',
'And the sentence may end without the dot!']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)