At a stage in my code, I have a function that receives problems of two or three nested lists In each nested list, I have [word_form : word_tag]. An example for the two sentences problems are like:
P1(input):
['italian:JJ', ['an:DT'], ['became:VB', ['world:NN', ['the:DT'],
['s:PO', ['tenor:NN', ['greatest:JJ']]]]], ['.:.']]
H(input):
['was:VX', ['there:EX'], ['an:DT'], ['italian:JJ'], ['became:VB',
['who:WP'], ['world:NN', ['the:DT'], ['s:PO', ['tenor:NN',
['greatest:JJ']]]]], ['.:.']]
Each nested list with tag in ['NN', 'VB', 'JJ'], I would like to replace their forms to Variables like X, Y, Z,... etc
If the sentence ( H ), has a word in common with P1 or P2, if exists, then they take the same Variable name. For instance, if ['italian' : 'JJ'] in H turned to ['X' : 'JJ'] then it must take 'X' in P1 or P2 (if exists).
what I do is only changing the forms into variables, and my variables are no X, Y, Z, I just did:
if tag in ['NN', 'VB', 'JJ']:
form = form.upper()+'-0'
Which turns the form 'italian' to 'ITALIAN-0', but I would prefer to make it [X, Y, Z, ... etc]
So the wanted output is something like:
P1(output):
['X:JJ', ['an:DT'], ['Y:VB', ['Z:NN', ['the:DT'],
['s:PO', ['A:NN', ['B:JJ']]]]], ['.:.']]
H(output):
['was:VX', ['there:EX'], ['an:DT'], ['X:JJ'], ['Y:VB',
['who:WP'], ['Z:NN', ['the:DT'], ['s:PO', ['A:NN',
['B:JJ']]]]], ['.:.']]
Similarly, the three sentences problems, like:
P1(input):
['want:VB', ['men:NN', ['every:DT'], ['italian:JJ']], ['be:VX',
['to:TO'], ['a:DT'], ['great:JJ', ['tenor:VB']]]]
P2(input):
['are:VX', ['men:NN', ['some:DT'], ['italian:JJ']], ['great:JJ'],
['tenor:VB']]
H(input):
['are:VX', ['there:EX'], ['italian:JJ'], ['Y:NN', ['want:VB',
['who:WP'], ['be:VX', ['to:TO'], ['a:DT'], ['great:JJ', ['tenor:VB']]]]]]
Becomes:
P1(output):
['Z:VB', ['Y:NN', ['every:DT'], ['X:JJ']], ['be:VX',
['to:TO'], ['a:DT'], ['A:JJ', ['B:VB']]]]
P2(output):
['are:VX', ['men:NN', ['some:DT'], ['X:JJ']], ['A:JJ'],
['B:VB']]
H(output):
['are:VX', ['there:EX'], ['X:JJ'], ['Y:NN', ['Z:VB',
['who:WP'], ['be:VX', ['to:TO'], ['a:DT'], ['A:JJ', ['B:VB']]]]]]
Assuming I understand your question, you could write a function
That maps an object to a string, uniquely.
next_var
increments each call dovar_map
.You could then call this on every instance of your string. The way that I update
next_var
can be changed, in case you have more than 26 variables.