I need to change a dictionary when it is equal to a text I don't know how to do it. There are two dictionaries that store dictionaries.
conceptos_de_mi={}
sobre_ti={}
are composed in this way: {'I': {'I am': {'Lola': {}}}}
There is a name.
tunombre="Carlos"
The text is sent to the function and the dictionary is created
aprender("Yo soy Lola")
This code receives the text that creates dictionaries within a dictionary.
def aprender(oraciones):
#enviar_palabras=oraciones
recordar_pal = sobre_ti
for palabra in oraciones.split(): #dividir palabras para rellenar diccionarios
if palabra not in recordar_pal: #si la primera palabra no esta en el diccionario principal
recordar_pal[palabra] = {} #añadir palabra
recordar_pal = recordar_pal[palabra] #guardar posicion dentro del subdicionario
return aprender_conceptos_mi(oraciones) #llamar funcion intercambio
Then it sends the text to this other function that is supposed to change a specific dictionary named Lola for Carlos but it doesn't, (change a specific dictionary not all) I have already tried in many ways and I can't get it.
def aprender_conceptos_mi(enviar_palabras):
recordar_pals=conceptos_de_mi
sunombres=tunombre
#recordar_sobre_ti=sobre_ti
for palabra in enviar_palabras.split():
if palabra not in recordar_pals:
recordar_pals[palabra] = {}
if palabra == sunombres:
recordar_pals[palabra] = recordar_pals[sunombres]
else:
pass
recordar_pals = recordar_pals[palabra]
Creating another function example failed.
tunombre="Lola"
sunombre="Carlos"
def aprender(oraciones):
sobre_ti_dic = sobre_ti
tunombres=tunombre
sunombres=sunombre
recordar_pal = sobre_ti
recordar_pals = conceptos_de_mi
for palabra in oraciones.split(): #dividir palabras para rellenar diccionarios
if palabra not in recordar_pal: #si la primera palabra no esta en el diccionario principal
recordar_pal[palabra] = {} #añadir palabra
#guardar posicion dentro del subdicionario
if palabra not in sobre_ti_dic:
sobre_ti_dic[palabra] = {}
if tunombres in sobre_ti_dic[tunombres]:
recordar_pals[palabra] = sobre_ti_dic[sunombres]
print("Cambiando nombre")
else:
print("Palabra no esta en sobre ti")
sobre_ti_dic = sobre_ti_dic[palabra]
recordar_pal = recordar_pal[palabra]
return
The default dictionary is
{'yo': {'soy': {'Lola':{} }}
}
The output dictionary
{'yo': {'soy': {'Carlos':{} }}
}
Let's see if I understood. The user enters phrases that are converted into nested dictionaries that somehow record the order in which the words appeared in the phrase. As phrases beginning with the same words are entered, the structure of nested dictionaries creates a kind of tree. For example:
generate a nested dictionary:
that in the background reflects the tree:
That's what your function does
aprender()
.What you now want is that if the user enters another phrase, it will be added to the previous dictionary, but with a variant. If the user now entered "I am Carlos Lopez", with the
aprender()
previous function "Carlos" would be added to the same level as "Lola" and the resulting tree would be:But you don't want this. You want if, as the sentence is parsed and traversed through the tree, a place is reached where the word was "Lola", then everything after that in the sentence replaces what was in the original tree. That is, you want the resulting tree to be this:
Response
If this is what you were asking, I think the following function would do what you ask:
In addition to the phrase to be parsed, this function receives the argument,
reemplazar
which in our example would be the word"Lola"
. Note that if we find that word at the level of the dictionary that we have to process, we eliminate that key , and therefore the entire sub-tree that it contained. The rest of the function is the same, so the key"Carlos"
will be inserted at that point, with the result that "Lola" has disappeared and "Carlos" has appeared, so it is as if one has been substituted for the other.demonstrative examples
Let's see an example of use and the result obtained:
The result that comes out of the first
print()
is:And in the second
print()
:Note that before sentences that do not have "Lola" in the appropriate place, this substitution does not occur, but is "learned" in the usual way. For example:
I guess this was what you expected. It is not clear to me, however, what the other dictionary is for
conceptos_de_mi
.Update
In a comment the OP indicates that she does not want the whole tree to be substituted under "Lola", as I had understood, but only the word "Lola".
That is, I understand that if the input tree is:
And the phrase "I am Carlos" is registered, with
reemplazar="Lola"
only the word "Lola" should be replaced by "Carlos", respecting the rest of the tree. And in this case the output should be:This case is also relatively simple. But there's a problem. It might look like it's just replacing the "Lola" key with the "Carlos" key, but that can't be done. The keys of a dictionary are immutable. What can be done is to insert a new key "Carlos", put as value what the key "Lola" previously had, and then delete the key "Lola".
So:
demonstration:
Result: