1

I am using Alko's code from Expanding English language contractions in Python

I added a bit code. I do not understand why I am getting this =>KeyError: "Didn't"

import re
import csv
fileLocation = 'test.csv'
cList = {
  "ain't": "am not",
  "aren't": "are not",
  "can't": "cannot",
  "can't've": "cannot have",
  "'cause": "because",
  "could've": "could have",
  "couldn't": "could not",
  "couldn't've": "could not have",
  "didn't": "did not",
  "doesn't": "does not",

        }
R = re.compile('(%s)' % '|'.join(cList.keys()),re.IGNORECASE)
read = csv.reader(open(fileLocation))
aList = []
def expandContractions(text,R=R):
    def replace(match):
        return cList[match.group(0)]
    return R.sub(replace, text)    
for i in read:
    aList.append(i[5])
for j in aList:
    print(expandContractions(j))
Community
  • 1
  • 1
Daniel
  • 478
  • 3
  • 16
  • 32

1 Answers1

0

Instead of using re.IGNORECASE and using .lower() on your keys, either always lowercase your input strings:

  • return R.sub(replace, text.lower()) in expandContractions; or
  • print(expandContractions(j.lower()))

or add the upper-case and title-case possibilities to your cList dictionary:

cList = {
...
'DID NOT': "DIDN'T"
'Did not': "Didn't"
...
}

Which choice you make depends on what you want to do with the resulting contraction, and how you want to handle totally messed-up cases, such as "dId NOt".

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237