How can I be able to do the following:
I have list of sentences and list of dictionaries.
I want to be able to replace each pair in a dictionary to 'NAME' to hilight the pair in the string.
For example: 'word P0 word P1 word P2 word' this sentence has 3 (P)s I want to choose (P0,P1) to be the first pair(so the sentence will be like this:'word NAME word NAME word OTHER word' then to choose (P0,P2) to be the second pair,(the sentence will be like this:'word NAME word OTHER word NAME word'), finally,(P1,P2) will be chosen to be the last pair('word OTHER word NAME word NAME word'.
If a sentence has a pair (P0,P0) where P0 = P0 then it is impossible to have pair with the same value so I will replace the pair to OTHER, for instance,word P0 word P0 word => will be word OTHER word OTHER word.
If a sentence has 3 (p)s 2 of them are equal then we need to replace one of them as OTHER.
For example: word word P0 word P0 word P1 word => will be word word OTHER word NAME word NAME word or word word NAME word OTHER word NAME word
I appreciate any help or hints.
I have list of sentences and list of dictionaries.
I want to be able to replace each pair in a dictionary to 'NAME' to hilight the pair in the string.
For example: 'word P0 word P1 word P2 word' this sentence has 3 (P)s I want to choose (P0,P1) to be the first pair(so the sentence will be like this:'word NAME word NAME word OTHER word' then to choose (P0,P2) to be the second pair,(the sentence will be like this:'word NAME word OTHER word NAME word'), finally,(P1,P2) will be chosen to be the last pair('word OTHER word NAME word NAME word'.
If a sentence has a pair (P0,P0) where P0 = P0 then it is impossible to have pair with the same value so I will replace the pair to OTHER, for instance,word P0 word P0 word => will be word OTHER word OTHER word.
If a sentence has 3 (p)s 2 of them are equal then we need to replace one of them as OTHER.
For example: word word P0 word P0 word P1 word => will be word word OTHER word NAME word NAME word or word word NAME word OTHER word NAME word
S = [ ‘ word word word P0 word word P1 word’,
‘ word word P0 word word P1 , P2 , P3, word word’,
‘word word P0 word P0 word P1 word ’,
‘word P0 word word word P0’]
P_dic = [{‘id’: s1 , ‘first_p’: p0, ‘second_p’ : p1},
{‘id’: s2 , ‘first_p’: p0, ‘second_p’ : p1},
{‘id’: s2 , ‘first_p’: p0, ‘second_p’ : p2},
{‘id’: s2 , ‘first_p’: p0, ‘second_p’ : p3},
{‘id’: s2 , ‘first_p’: p0, ‘second_p’ : p2},
{‘id’: s2 , ‘first_p’: p1, ‘second_p’ : p2},
{‘id’: s2 , ‘first_p’: p1, ‘second_p’ : p3},
{‘id’: s2 , ‘first_p’: p2, ‘second_p’ : p3},
{‘id’: s2 , ‘first_p’: p0, ‘second_p’ : p2},
{‘id’: s3 , ‘first_p’: p0, ‘second_p’ : p0},
{‘id’: s3 , ‘first_p’: p0, ‘second_p’ : p1},
{‘id’: s4 , ‘first_p’: p0, ‘second_p’ : p0}]]
def replace_word(s, word, replacement, n ):
index = -1
for _ in range(n):
try:
index = s.index(word, index + 1)
except ValueError:
return s
return s[:index] + s[index:].replace(word, replacement, 1)
for i in S:
for p in P_dic:
for key,value in i.items():
if key == p['id']:
if p['first_p'].lower() != p['second_p'].lower():
v = p['first_p']
v2 = p['second_p']
s1 = ''.join(i[key])
v_count = s1.count(v)
v2_count = s1.count(v2)
s1 = replace_word(s1 ,v,'NAME',v_count)
s1 = replace_nth(s1 ,v2 ,'NAME',v2_count)The above code is my attempt but it does not work as I want. I do not know how can I replace other words in the dictionary to OTHER. Also, it just replace the last word in case of duplication. I appreciate any help or hints.
