简体   繁体   中英

Capitalizing the first letter of each sentence in Python

Here is my code:

def fix_capitalization(usrStr):
    newString = ''
    wordList = []
    numLetters = 0
    for s in usrStr.split('. '):
        if s[0].isupper():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
        if s.islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
            numLetters += 1

        if s[0].islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
            numLetters += 1 



    newString = '. '.join(wordList)
    return newString, numLetters

The string being passed in is:

i want some water. he has some. maybe he can give me some. i think I will ask.

Note that there are 4 spaces before maybe . The result that I want is:

I want some water. He has some. Maybe he can give me some. I think I will ask.

but I get:

I want some water. He has some. maybe he can give me some. I think I will ask.

I know that maybe isn't being capitalized because I split on . and that sentence has more than one space after the period, but I'm not sure how I can fix this or if there's a better way to go about what I'm doing. Any help would be greatly appreciated.

In for loop: First find the index of non-space character. Then replace s[0] with s[index].

Solution using regex sub method

def fix_capitalization(mystr):
    numLettets = 0
    newstr = []
    for s in mystr.split('. '):
        tmp = re.sub('^(\s*\w+)', lambda x:x.group(1).title(), s)
        newstr.append(tmp)
        # num of letters
        if s.lstrip()[0] != tmp.lstrip()[0]:
            numLetters += 1          
    return '. '.join(newstr).replace(' i ', ' I '), numLetters

fix_capitalization( 'i want some water. he has some.    maybe he can give me some. i think I will ask.')
# return ('I want some water. He has some.    Maybe he can give me some. I think I will ask.', 4)

Simple fix to original code as below

def fix_capitalization(usrStr):
    newString = ''
    wordList = []
    numLetters = 0
    for s in usrStr.split('. '):
        # check for leading space
        lspace = len(s) - len(s.lstrip())
        s = s.lstrip()

        if s[0].isupper():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
        if s.islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
            numLetters += 1
        if s[0].islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
            numLetters += 1 

    newString = '. '.join(wordList)
    return newString, numLetters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM