Making a single line of fcommand to correct paragraph upper/lowwer cases

37 Views Asked by At

I've been solving a challenge of fixing the input paragraph into properly capitalized letters. This is my code:

return ''.join([in_text[0].upper()] + [in_text[i].lower() if in_text[i-1] != '.' or in_text[i-2] != ' ' or in_text[i].islower() else in_text[i].upper() for i in range(1, len(in_text))])

However, it was only able to capitalize the first letter of the paragraph and not the first letter of the sentence.

1

There are 1 best solutions below

0
ScottC On

Let's say you had the following paragraph:

text_example = "australia is a country and continent located in the southern hemisphere.it is the world's sixth-largest country by total area and has the world's largest coral reef system, the Great Barrier Reef. australia is known for its diverse landscapes and unique wildlife, such as kangaroos and koalas."

As can be seen, the starting word for each sentence is in lowercase.
We can fix this by:

  • Splitting the paragraphs into sentences (using regex)
  • using the capitalize function to capitalize the first word of each sentence
  • Joining the sentences back into a paragraph

The regex (?<=[.!?])[\s]* will split when it finds either

  • a period .,
  • an exclamation mark !
  • or a question mark ?
  • followed by zero or more spaces [\s]*

Here is the code:

Code:

import re

def fix_paragraph(pgraph):
    # split the paragraph into sentences
    sentences = re.split(r"(?<=[.!?])[\s]*", pgraph)
    
    # capitalize each sentence
    sentences = [sentence[0].capitalize() + sentence[1:] if len(sentence) > 0 else "" for sentence in sentences]
    
    # join the sentences back into a single paragraph and return
    text = " ".join(sentences)
    return text


text_example = "australia is a country and continent located in the southern hemisphere.it is the world's sixth-largest country by total area and has the world's largest coral reef system, the Great Barrier Reef. australia is known for its diverse landscapes and unique wildlife, such as kangaroos and koalas."

fixed_text = fix_paragraph(text_example)
print(fixed_text)

Output:

Australia is a country and continent located in the southern hemisphere. It is the world's sixth-largest country by total area and has the world's largest coral reef system, the Great Barrier Reef. Australia is known for its diverse landscapes and unique wildlife, such as kangaroos and koalas.