Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- intext = "mary , had a little lamb!!isn't it cute ??!! it followed her to school;; ate all her sandwiches. oh dear"
- newtext = ""
- start_sentence = True
- spctr = 1
- lastch = " "
- lastnonsp = ""
- for ch in intext:
- if start_sentence:
- if ch in [".", "!", "?"]:
- if ch == lastch:
- continue
- if lastch == " ":
- newtext = newtext.rstrip(' ')
- lastch = ch
- newtext += ch
- newtext += " "
- spctr = 1
- continue
- if ch == ' ':
- spctr += 1
- if spctr == 1:
- newtext += ch
- lastch = ch
- continue
- if not spctr:
- newtext += " "
- ch = ch.upper()
- newtext += ch
- start_sentence = False
- lastch = ch
- continue
- if ch in [".", "!", "?"]:
- if lastch == " ":
- newtext = newtext.rstrip()
- start_sentence = True
- if ch in [',', ':', ';']:
- if lastnonsp == ch:
- continue
- newtext = newtext.rstrip()
- newtext += ch
- newtext += " "
- lastnonsp = ch
- lastch = " "
- continue
- if ch == " " and lastch == " ":
- continue
- if ch != " ":
- spctr = 0
- lastch = ch
- newtext += ch
- # ensure sentence ends with a sentence terminator
- newtext = newtext.rstrip()
- if newtext[-1] not in [".", "!", "?"]:
- newtext += "."
- print(newtext)
- # in = "mary , had a little lamb!!isn't it cute ??!! it followed her to school;; ate all her sandwiches. oh dear"
- # Out: "Mary, had a little lamb! Isn't it cute?! It followed her to school; ate all her sandwiches. Oh dear."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement