I am searching for a python script that can translate multiple text fragments.
On 1 condition: when a text part is translated, it should NOT be translated again..
I thought of first creating a mask array of 0s that indicates if a given index/letter has already been replaced or not
then search the original text for each instance of words to be replaced, and if its mask-indexes are all zeroes, then replace it and keep on going
this should work fine as long as there are no common strings/portion of strings in the search list… but just case keep in mind that the original text is processed left to right, and replacement words are applied in the very same order they are written
tested on your example (credits to ChatGPT for implementing the logic into code)
Goal:
The more characters the ‘search’ key has, the more ‘priority’ it has.
Meaning that longer words should be translated first, after which the rest follows.
This to avoid that a single (or part) of a bigger search keyh has already been translated befor the bigger word is ‘searched/translated’.
Currently:
Noticed it almost goes correctly: See example below is perfect!
However, when the first Letter is the same as a ‘search’ text above it goes wrong, see below..
Wanted logic:
all → has the most characters so it should be translated first.
Hereafter the search key with second most characters has to be translated etc..
if you want longer words to be transformed first, then they must be placed ahead in the lists
you can even just put there a Sort with inputs SearchList_TextLength, SearchList, ReplacementList and connect the Reversed last ones to the Python component
it was not stated that the search and replacement words could have different lengths, so that is not handled at all, like in this case:
When seeing it again I didn’t explained it clearly.
Good to know: The ‘search’ and ‘rep’ input indexes álways below to eachother.
so this is not an issue.
Search : rep example
0 = 0 a = b
1 = 1 base = foot
2 = 2 all = House
etc
The thing is, that the items in the ‘Search’ list, have to be sorted based on the number of characters. (and offcourse the ‘rep’ list, has to be sorted 1:1 the same, so the ‘Search’ and ‘rep’ List are still belonging together)..
so Result should be:
(item) Search : rep
(0) base : foot
(1) all : House
(2) a : b
Preferable this sorting is done inside the ‘python’ component, this way its a genereic solution, so I dont have to bother how the inputs look like .