3

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.

Kblo55
  • 65
  • 1
  • 6

2 Answers2

0

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

Musa K.
  • 24
  • 2
  • I attempted to do this but wasn't able to get it to work. Could you explain how I would do that, if you don't mind? – Kblo55 Nov 15 '16 at 23:46
  • 1
    http://stackoverflow.com/questions/2378962/returning-the-lowest-index-for-the-first-non-whitespace-character-in-a-string-in – Musa K. Nov 15 '16 at 23:51
  • that helped me find the index, but the part I can't figure out is how to replace the index with a capital version. – Kblo55 Nov 16 '16 at 00:02
  • 1
    I think this helps you. http://stackoverflow.com/questions/15858065/python-how-to-capitalize-nth-letter-of-a-string – Musa K. Nov 16 '16 at 00:06
  • Is it always 4 spaces before `maybe`? if yes, then when you split them into lists, just index it as `string[4]` which would give you the word `maybe`. – Imtiaz Raqib Nov 16 '16 at 00:09
0

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
Skycc
  • 3,496
  • 1
  • 12
  • 18