This is an extension of this previous question.
I have a python dictionary, made like this
a = {"animal": [ "dog", "cat", "dog and cat"], "XXX": ["I've been", "asp*", ":)"]}
I want to find a solution to replace, as fast as possible, all the words in the dictionary values, with their keys. Solution should be scalable for large text. If words end with asterisk, it means that all words in the text that start with that prexif should be replaced.
So the following sentence "I've been bad but I aspire to be a better person, and behave like my dog and cat :)" should be transformed into "XXX bad but I XXX to be a better person, and behave like my animal XXX".
I am trying to use trrex for this, thinking it should be the fastest option. Is it? However I cannot succeed. Moreover I find problems:
- in handling words which include punctuation (such as ":)" and "I've been");
- when some string is repeated like "dog" and "dog and cat".
Can you help me achieve my goal with a scalable solution?