Using list comprehension to replace a string containing space

47 views Asked by At

I am trying to build a simple correction-function for some grammatically wrong sentences I randomly generated elsewhere. I would like to use a dictionary and list comprehension as there are only a few options where the program will not generate a correct sentence, ie "JEDER KIRCHE" which needs to be corrected to "JEDE KIRCHE".

So I built my dic and defined a variable with list comprehension, but it doesn't work. I'm guessing the problem is the whitespace in the strings, as the same thing works fine if I try to replace strings without it.

text = 'JEDER KIRCHE'
ersetzungen = {'EIN KIRCHE': 'EINE KIRCHE', 'KEIN KIRCHE': 'KEINE KIRCHE', 'JEDER KIRCHE': 'JEDE KIRCHE', 'JEDER SCHLOSS': 'JEDES SCHLOSS', 'JEDER BILD': 'JEDES BILD', 'JEDER AUGE': 'JEDES AUGE','JEDER DORF': 'JEDES DORF', 'JEDER HAUS': 'JEDES HAUS'}
korrektur = ' '.join([ersetzungen.get(x, x) for x in text.split()])
print(korrektur)

Thanks in advance!

0

There are 0 answers