Don't use a list of tuples. Use a dictionary:
ref_map = dict(ref_set)
for line in x:
line = ' '.join([ref_map.get(word, word) for word in line.split()])
otherwise you have a NxM loop; for every extra word in your text or in your ref_set
you double the number of iterations you need to do.
Your code only rebinds word
, not replace the word in the line
; the list comprehension above produces a new line
value instead. This doesn't replace the line in x
though, you need another list comprehension for that:
x = [' '.join([ref_map.get(word, word) for word in line.split()]) for line in x]
It appears from the comments that x
is not a list of sentences but rather one sentence. In which case you use just process that one line with one list comprehension, as in the loop iteration over x
above:
def corrected(line):
return ' '.join([ref_map.get(word, word) for word in line.split()])