I have a string: "y, i agree with u."
And I have array dictionary [(word_will_replace, [word_will_be_replaced])]
:
[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
i want to replace 'y' with 'yes' and 'u' with 'you' according to the array dictionary.
So the result i want: "yes, i agree with you."
I want to keep the punctuation there.
Extending @gnibbler's answer from Replacing substrings given a dictionary of strings-to-be-replaced as keys and replacements as values. Python with the tips implemented from Raymond Hettinger in the comments.
That's not a dictionary -- it's a list, but it could be converted to a
dict
pretty easily. In this case, however, I would make it a little more explicit:Now you have the dictionary mapping responses to what you want to replace them with:
once you have that, you can pop in my answer from here using regular expressions: https://stackoverflow.com/a/15324369/748858
Output