Python 3中字符串中每个句子的大写

时间:2014-10-12 00:38:33

标签: python string capitalize

这应该很容易,但不知怎的,我不太明白。

我的任务是:

  

编写一个函数sentenceCapitalizer,它有一个string类型的参数。该函数返回一个   每个句子的第一个字符大写的字符串的副本。该函数应该返回   “你好。我的名字是乔。你的名字是什么?“如果函数的参数是”你好。我的名字是   乔。你的名字是什么?“假设一个句子被句号分隔,后跟一个空格。”

到目前为止我所拥有的是:

def sentenceCapitalizer (string1: str):
    words = string1.split(". ")
    words2=words.capitalize()
    string2=words2.join()
    return (string2)

print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))

执行后我收到错误:

Traceback (most recent call last):
  File "C:\Users\Andrew\Desktop\lab3.py", line 83, in <module>
    print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))
  File "C:\Users\Andrew\Desktop\lab3.py", line 79, in sentenceCapitalizer
    words2=words.capitalize()
AttributeError: 'list' object has no attribute 'capitalize'"

这是什么告诉我,我该如何解决这个问题?我尝试按照列为python软件基础的页面上的说明进行操作,所以我想我已经有了这个。

5 个答案:

答案 0 :(得分:2)

您正在尝试对错误的对象使用字符串方法; words是包含字符串的列表对象。改为在每个单独的元素上使用该方法:

words2 = [word.capitalize() for word in words]

但这会应用错误的转换;你不想将整个句子大写,但只是第一个字母。 str.capitalize()会小写其他所有内容,包括J中的Joe

>>> 'my name is Joe'.capitalize()
'My name is joe'    

仅限第一个字母,然后将字符串的其余部分添加回原位:

words2 = [word[0].capitalize() + word[1:] for word in words]

接下来,列表对象也没有.join()方法;这也是一个字符串方法:

string2 = '. '.join(words2)

这会将words2中的字符串与'. '(句号和空格)连接符连接起来。

你可能想在这里使用更好的变量名;你的字符串是句子,而不是单词,所以你的代码可以更好地反映出来。

一起使你的功能:

def sentenceCapitalizer (string1: str):
    sentences = string1.split(". ")
    sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
    string2 = '. '.join(sentences2)
    return string2

演示:

>>> def sentenceCapitalizer (string1: str):
...     sentences = string1.split(". ")
...     sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
...     string2 = '. '.join(sentences2)
...     return string2
... 
>>> print (sentenceCapitalizer("hello. my name is Joe. what is your name?"))
Hello. My name is Joe. What is your name?

答案 1 :(得分:1)

这可以胜任。由于它提取所有句子,包括它们的尾随空格,如果你有多个段落,句子之间有换行符,这也有效。

import re

def sentence_case(text):
    # Split into sentences. Therefore, find all text that ends
    # with punctuation followed by white space or end of string.
    sentences = re.findall('[^.!?]+[.!?](?:\s|\Z)', text)

    # Capitalize the first letter of each sentence
    sentences = [x[0].upper() + x[1:] for x in sentences]

    # Combine sentences
    return ''.join(sentences)

这是working example

答案 2 :(得分:0)

允许点后的任意空格。或者大写完整的单词(它可能会对Unicode文本产生影响),你可以use regular expressions -- re module

#!/usr/bin/env python3
import re

def sentenceCapitalizer(text):
    return re.sub(r"(\.\s+|^)(\w+)",
                  lambda m: m.group(1) + m.group(2).capitalize(),
                  text)

s = "hEllo. my name is Joe. what is your name?"
print(sentenceCapitalizer(s))
# -> 'Hello. My name is Joe. What is your name?'

注意:pep8建议使用小写名称,例如capitalize_sentence()而不是sentenceCapitalizer()

要接受更大范围的文本,您可以use nltk package

# $ pip install nltk
from nltk.tokenize import sent_tokenize, word_tokenize 

def sent_capitalize(sentence):
    """Capitalize the first word in the *sentence*."""
    words = word_tokenize(sentence)
    if words:
       words[0] = words[0].capitalize()
    return " ".join(words[:-1]) + "".join(words[-1:]) # dot

text = "hEllo. my name is Joe. what is your name?"
# split the text into a list of sentences
sentences = sent_tokenize(text)
print(" ".join(map(sent_capitalize, sentences)))
# -> Hello. My name is Joe. What is your name?

答案 3 :(得分:0)

我没有使用'split'而是使用while循环。这是我的代码。

my_string = input('Enter a string: ')
new_string = ''
new_string += my_string[0].upper()
i = 1

while i < len(my_string)-2:
    new_string += my_string[i]
    if my_string[i] == '.' or my_string[i] == '?' or my_string[i] == '!':
        new_string += ' '
        new_string += my_string[i+2].upper()
        i = i+3
    else:
        if i == len(my_string)-3:
            new_string += my_string[len(my_string)-2:len(my_string)]
        i = i+1

print(new_string)

以下是它的工作原理:

Enter a string: hello. my name is Joe. what is your name?
Hello. My name is Joe. What is your name

答案 4 :(得分:0)

因为我在这里找不到这个解决方案。

您可以使用nltk中的'sent_tokenize'方法。

import nltk
string = "hello. my name is Joe. what is your name?"
sentences = nltk.sent_tokenize(string)
print (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]) )

输出

Hello. My name is Joe. What is your name?