如何将每个句子的第一个字母大写?

11
我正在尝试编写一个程序,将每个句子的第一个字母大写。目前为止,这是我的代码,但我无法想出如何在句子之间再次添加句号。例如,如果我输入:

你好。再见

输出结果是

你好 再见

并且句号已经消失了。

string=input('Enter a sentence/sentences please:')
sentence=string.split('.')
for i in sentence:
    print(i.capitalize(),end='')
15个回答

13

您可以使用nltk进行句子分割:

#!/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)

输出

请输入一个或多个句子:
一个句子中可能会有句点,例如: see! 而且有时候句子可能没有以句点结尾。
['一个句子中可能会有句点,例如: see!', '而且有时候句子可能没有以句点结尾。']

7
你可以使用正则表达式。 定义一个匹配句子第一个单词的regex:
import re
p = re.compile(r'(?<=[\.\?!]\s)(\w+)')

这个正则表达式包含一个正向回顾断言 (?<=...),它匹配一个句号.、问号?或感叹号!,后面跟着一个空格字符\s。然后是一个匹配一个或多个字母数字字符\w+的组。实际上,这个正则表达式匹配句子结束后的下一个单词。
您可以定义一个函数来将正则表达式匹配对象大写,并将此函数提供给sub()函数:
def cap(match):
    return(match.group().capitalize())

p.sub(cap, 'Your text here. this is fun! yay.')

您可能希望对另一个匹配字符串开头单词的正则表达式执行相同操作:
p2 = re.compile(r'^\w+')

或者通过组合正则表达式,使原始正则表达式更难读懂:

p = re.compile(r'((?<=[\.\?!]\s)(\w+)|(^\w+))')

4

您可以使用以下方法:

In [25]: st = "this is first sentence. this is second sentence. and this is third. this is fourth. and so on"

In [26]: '. '.join(list(map(lambda x: x.strip().capitalize(), st.split('.'))))
Out[26]: 'This is first sentence. This is second sentence. And this is third. This is fourth. And so on'

In [27]:

OP想要将每个句子的第一个字母大写,而不是每个单词。 - austin-schick
“.” 被视为句子的结尾。因此,“this is first. second .third”是三个不同的句子。 - Nishant Nawarkhede
1
OP想要的是"This is the first sentence.",而不是"This Is The First Sentence."。 - voneiden

3
也许可以这样做:
print('.'.join(i.capitalize() for i in sentence))

capitalize() 只将句子中的第一个字母大写,并将所有其他单词转换为小写。是的,甚至包括名词。 - Tanmay Bairagi

2
x = 'hello. goodbye. and how are you doing.'
print( '. '.join(map(lambda s: s.strip().capitalize(), x.split('.'))))

# Hello. Goodbye. And how are you doing. 

2

如果您想让句子的第一个字母大写,而不改变句子的其余部分,那么您可以获取第一个字符,然后将其转换为大写并与句子的其余部分连接起来,例如以下方式:

desc="please make only the first letter Upper Case, and do not change the rest!"
desc = desc[0].upper()+desc[1:]
print(desc)

输出结果将为:
Please make only the first letter Upper Case, and do not change the rest!

1
这只将第一个单词大写。 - Nightforce2

1
这应该可以工作:

import re
text = raw_input("Enter text: ")
rtn = re.split('([.!?] *)', text)
final = ''.join([i.capitalize() for i in rtn])
print final

1

你只需要改变一行:

string=input('Enter a sentence/sentences please:')
sentence=string.split('.')
for i in sentence:
    print (i.strip().capitalize()+". ",end='')

0

好的,我的第一个答案完全错误了。这里是另一个你可以使用的答案,并且它还展示了一些 Python 更强大的特性。假设你的字符串存储在s中,其中所有的句子都在一个由逗号分隔的单个字符串中。以下代码返回相同的字符串,用句号分隔,但每个句子的第一个字符大写。

'.'.join(map((lambda x: x[0].upper()+x[1:]), s.replace('. ','.').split('.')))

很棒,对吧?


1
我不这么认为。str.title会将每个单词都大写,而不是每个句子。 - Elias Zamaria
如果在句末的句点后面没有字母,那么这会导致索引超出范围的错误。 - Nightforce2

0
我曾经遇到过同样的问题,花了几个小时搜索和调整。最终我找到了一个几乎完美的解决方案,虽然它解决了手头的问题。
    original_data = raw_input("Enter text: ")
    list = original_data.split(".")
    if original_data.endswith('.'):
        list.remove('')

    for w in list:
        stripper= w.strip().capitalize() +"."
        print stripper,

这段代码的功能是将输入作为字符串获取,并使用split()函数将其转换为字符串数组。然后遍历该数组以提取每个字符串,并在句号后将第一个字符大写。
比方说,你输入了一些内容:
hello stackoverflow. hi robot. we're here, devmike.

它将输出:

Hello stackoverflow. Hi robot. We're here, devmike.

注意:我只在python2.7+中测试过这个,但你可以修改它使其适用于3+。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接