简体   繁体   English

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

[英]How to capitalize the first letter of every sentence?

I'm trying to write a program that capitalizes the first letter of each sentence.我正在尝试编写一个将每个句子的第一个字母大写的程序。 This is what I have so far, but I cannot figure out how to add back the period in between sentences.这是我到目前为止所拥有的,但我无法弄清楚如何在句子之间添加句点。 For example, if I input:例如,如果我输入:

hello.你好。 goodbye再见

the output is输出是

Hello Goodbye你好再见

and the period has disappeared.并且这个时期已经消失了。

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

You could use nltk for sentence segmentation :您可以使用 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)

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!']

You could use regular expressions.你可以使用正则表达式。 Define a regex that matches the first word of a sentence:定义一个匹配句子第一个单词的正则表达式:

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

This regex contains a positive lookbehind assertion (?<=...) which matches either a .此正则表达式包含一个肯定的后向断言(?<=...) ,它与. , ? , ? or !! , followed by a whitespace character \s . ,后跟一个空格字符\s This is followed by a group that matches one or more alphanumeric characters \w+ .后面是一个匹配一个或多个字母数字字符\w+的组。 In effect, matching the next word after the end of a sentence.实际上,匹配句子结尾后的下一个单词。

You can define a function that will capitalise regex match objects, and feed this function to sub() :您可以定义一个将大写正则表达式匹配对象的函数,并将此函数提供给sub()

def cap(match):
    return(match.group().capitalize())

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

You might want to do the same for another regex that matches the word at the beginning of a string:您可能希望对与字符串开头的单词匹配的另一个正则表达式执行相同的操作:

p2 = re.compile(r'^\w+')

Or make the original regex even harder to read, by combining them:或者通过组合它们使原始的正则表达式更难阅读:

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

You can use,您可以使用,

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]:

也许是这样的:

print('.'.join(i.capitalize() for i in sentence))
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. 

You just have to change one line:您只需要更改一行:

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

This should work:这应该有效:

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

If you want to get only the first letter of a sentence to be capitalised, and do not change the rest of sentence, then you can get the first char, and then convert it to upper case and join it with the rest of sentence, like the following:如果您只想将句子的第一个字母大写,并且不更改句子的其余部分,则可以获取第一个字符,然后将其转换为大写并将其与句子的其余部分连接,例如以下:

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

The output will be:输出将是:

Please make only the first letter Upper Case, and do not change the rest!

Okay, so my first answer was totally wrong.好的,所以我的第一个答案是完全错误的。 Here's another answer you can use, and it shows you some of the more powerful features of python, too.这是您可以使用的另一个答案,它也向您展示了 python 的一些更强大的功能。 Suppose you have your string stored in s , where all your sentences are in a single string delimited by a comma.假设您将字符串存储在s中,其中所有句子都在一个以逗号分隔的字符串中。 The following code returns that same exact string, separated by periods, but with the first characters of each sentence capitalized.以下代码返回完全相同的字符串,用句点分隔,但每个句子的第一个字符大写。

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

Slick, right?光滑,对吧?

maybe you can do this:也许你可以这样做:

string=input('Enter a sentence/sentences please:')
sentence='.'.join([i.capitalize() for i in string.split('.')])
print(sentence)

You can use end='.'你可以使用end='.' in your print function.在您的打印功能中。

print(i.capitalize(),end='.')

It seems that many folks don't bother to check indentation or code by running it first to check for errors.似乎很多人都懒得检查缩进或代码,而是先运行它来检查错误。 Concerning capitalization of the first word in a sentence that has OTHER WORDS in the sentence that are to REMAIN capitalized, the question must have been lost to others who responded.关于句子中第一个单词的大写,而句子中的其他单词要保持大写,这个问题肯定已经被其他回答的人弄丢了。 If you want to accomplish this try the following code which will run on a repeating menu until exit is selected:如果您想完成此操作,请尝试以下代码,该代码将在重复菜单上运行,直到选择退出:

# Purpose: Demonstrate string manipulation.
#
# ---------------------------------------------------------------
# Variable          Type        Purpose
# ---------------------------------------------------------------
# strSelection      string      Store value of user selection.
# strName           string      Store value of user input.
# words             string      Accumulator for loop.


def main():
    print()
    print("-----------------------------------------------------")
    print("|             String Manipulation                   |")
    print("-----------------------------------------------------")
    print()
    print("1: String Manipulation")
    print("X: Exit application")
    print()
    strSelection = input("Enter your menu selection: ")
    if strSelection == "1":
        strName = input("Enter sentence(s) of your choosing:  ")
        strSentences = ""
        words = list(strName.split(". ")) # Create list based on each sentence.
        for i  in range(len(words)): # Loop through list which is each sentence.
            words[i] = words[i].strip() # Remove any leading or trailing spaces.
            words[i] = words[i].strip(".") # Remove any periods.

            words[i] = words[i][:1].upper() + words[i][1:] # Concatenate string with first letter upper.
            strSentences += words[i] + ". " # Concatenate a final string with all sentences.

        # Print results.
        print("Sentences with first word capitalized, \
and other caps left intact: ", strSentences) 
        print()
        main() # Redisplay menu.

    # Bid user adieu.
    elif strSelection.upper() == "X":
        print("Goodbye")
    else:
        print ("Invalid selection")
        main() # Redisplay menu.

main()

This program uses to capitalize first word of each new sentence.该程序用于将每个新句子的第一个单词大写。

def sentenceCapitalizer():

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

I was having this same issue, after searching and tweaking for hours.在搜索和调整了几个小时后,我遇到了同样的问题。 I finally find an almost perfect solution, however, it solves the problem in hand.我终于找到了一个几乎完美的解决方案,但是,它解决了手头的问题。

    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,

What this code does is that it take an input as a string and convert it to string array using the split() function.这段代码所做的是将输入作为字符串并使用split()函数将其转换为字符串数组。 And then iterate through that array to extract every strings and capitalize the first character after a full stop.然后遍历该数组以提取每个字符串并将句号后的第一个字符大写。

Lets say you input something, like:假设您输入了一些内容,例如:

hello stackoverflow. hi robot. we're here, devmike.

It would output:它会输出:

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

Note: I only tested this with python2.7+, but you could modify it to work for 3+.注意:我只用 python2.7+ 对此进行了测试,但您可以修改它以适用于 3+。

Try this:尝试这个:

x = 'hello. how are you doing. nice to see. you'
print '.'.join(map(lambda x: x.title(), x.split('.')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3-从摩尔斯电码翻译时如何大写每个句子的首字母 - Python 3 - How to capitalize first letter of every sentence when translating from morse code Python中每个句子的首字母大写 - Capitalize first letter in each sentence in Python 如何在python中将每个单词的第一个字母大写而不使用.capitalize or.upper or.title - how to capitalize first letter of every word without .capitalize or .upper or .title in python 如何在 PySpark 中将数据集的第一个字母大写? (简单的大写/句子大小写) - How do you capitalize just the first letter in PySpark for a dataset? (Simple capitalization/sentence case) 如何提取句子中每个第 n 个单词的首字母? - How to extract first letter of every nth word in a sentence? 获取句子中每个单词的第一个字母? - Getting first letter of every word in a sentence? 如何为句子的每个字母获取字母索引? - How to get for every letter of sentence an index of letter? 如何仅将dict值中的第一个字母大写 - How to capitalize the first letter in dict values only 如何在 python 中使用 for 循环获取列表,并且我们必须将每个元素的第一个字母大写? - How to get list using for loop in python and we have to capitalize the first letter of the every element? 如何将 python 中每个单词的首字母大写? - How to capitalize the First letter of each word in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM