We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def split_sentences(text):
# Define the regular expression pattern to match sentence boundaries
pattern = r"""(?<=\w.)\s+"""
# Split the text into sentences using the pattern
sentences = re.split(pattern, text)
# Clean up the sentences by removing leading and trailing whitespace and empty strings
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
# Handle sentences ending with common abbreviations (e.g., Dr., Mr., Mrs.)
sentences = [sentence[:-1] if sentence.endswith(".") and sentence[-2:] in [".D", ".M", ".M"] else sentence for sentence in sentences]
return sentences
Get the input text from the user
input_text = input("Enter your text: ")
Split the text into sentences
sentences = split_sentences(input_text)
Print the sentences
for sentence in sentences:
print(sentence)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
From Paragraphs to Sentences
You are viewing a single comment's thread. Return to all comments →
import re
def split_sentences(text): # Define the regular expression pattern to match sentence boundaries pattern = r"""(?<=\w.)\s+"""
Get the input text from the user
input_text = input("Enter your text: ")
Split the text into sentences
sentences = split_sentences(input_text)
Print the sentences
for sentence in sentences: print(sentence)