0

Hello again :) I'm trying to define a simple "spelling correction" function that takes a string and sees to it that 1) two or more occurrences of the space character is compressed into one, and 2) inserts an extra space after a period if the period is directly followed by a letter. My piece of code is not working. Please advise.

import re
def correct(s):
  rem = re.sub('\+','',s)    # hoping that this remove extra spaces.
  #rem = re.sub('\.','. ',rem)  # and this inserting extra spaces after a period.

  print rem
s = raw_input('input a weird string: ')
correct(s)
2
Contributors
1
Reply
28
Views
2 Years
Discussion Span
Last Post by Gribouillis
1

A regex to match more than 1 space character is

r' {2,}'

To match a dot followed by a letter without consuming the letter, you can use

r'\.(?=[a-zA-Z])'

Always use raw strings(r'...') with regexes, in order to preserve \ characters.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.