The purpose of this function is the following: first it receives a text string identical to the nesting of dictionaries to be changed (to go directly to the root and the sub-dictionaries to be changed), second it will receive a second string that it must replace multiple dictionaries from a root without deleting their subdictionaries, it should work like this, split both entries, if ( Yo
) is in the dic, and ( Yo
) is different from ( Ella
) then the dictionary ( Yo
) is copied into ( Ella
) and then ( Yo
) is eliminated leaving ( Ella
) with its content, the position is saved in the subdictionary at the end with ( dic=dic[palabrac]
) to continue replacing the following, it returns in the For now it is verified if ( soy
) is in the dic and if (soy
) is different from ( es
) replacement will occur but a strange error occurs.
Traceback (most recent call last):
File "F:\PYTHON\Python Full\CODIGOS GUARDADOS\CODIGOS INFO\Alice 1.10.11.py", line 258, in <module>
corregir(entrada1,corregido1)
File "F:\PYTHON\Python Full\CODIGOS GUARDADOS\CODIGOS INFO\Alice 1.10.11.py", line 226, in corregir
print("Final dic palabrac: ",dic[palabrac])
KeyError: 'es'
The function that gets the error is this.
def corregir(oracione, oracionc):
"""
Esta funcion reemplaza palabras, busca todas las palabras de un diccionario
en especifico usando una cadena de texto que especifica la ruta compara si
es igual y remplaza el viejo nombre por la diferencia
"""
dic = conceptos_de_mi
for palabrae in oracione.split():
for palabrac in oracionc.split():
if palabrae in dic and palabrae != palabrac:
dic[palabrac] = dic[palabrae]
del dic[palabrae]
dic[palabrac] = dic[palabrac]
dic = dic[palabrac]
Ignore this function ( aprender
) it only serves to create nested dictionaries
def aprender(oraciones):
recordal_palsu = conceptos_de_mi
recordar_pal = sobre_ti
for palabra in oraciones.split():
if palabra not in recordar_pal:
recordar_pal[palabra] = {}
if palabra not in recordal_palsu:
recordal_palsu[palabra] = {}
recordar_pal = recordar_pal[palabra]
recordal_palsu = recordal_palsu[palabra]
We create some nested dictionaries, a string of the path to change( entrada1
) and the corrected word( corregido1
), we send that data to the function.
conceptos_de_mi = {}
sobre_mi = {} # ignoren esto
aprender(
"Yo soy Lola una persona."
) # Se crea un diccionario con lo siguiente
entrada1 = "Yo soy Lola una persona." # palabrae/ para identificar la raiz y mantener orden
corregido1 = "Ella es Alice una maquina." # palabrac/ se reemplaza las palabras diferentes
corregir(entrada1, corregido1)
I don't quite understand the expected result after the execution of
corregir()
, but I see a couple of things wrong:You have two nested loops , one to iterate through the words of
oracione
and one for the words oforacionc
. I understand that you don't really want to nest them , but rather that both sentences go through "in parallel", so there would be only one loop instead of two, but in each iteration of that single loop you would go through both sentences at the same time. On the first iteration you would getpalabrae="Yo"
andpalabrac="Ella"
and so on.For these things (going through two lists at the same time in a single loop) there is a function
zip(lista1, lista2)
that returns pairs of data taken from each of the lists. By the way, both lists are assumed to have the same length. If one is shorter, the loop will end when the shorter one runs out. This may pose problems in your algorithm, but I'm not entirely clear on it.You have a statement that doesn't do anything:
dic[palabrac] = dic[palabrac]
You're obviously copying a variable to itself, so it doesn't change.I don't know what you meant here. Maybe it's that this instruction is superfluous, or maybe you wanted to do
dic[palabrae] = dic[palabrac]
. Obviously the result is very different in each of the cases, but I don't quite understand what is intended.This would be the proposed code, which shows how to use
zip()
:When executing this function as it is (with the instruction commented and therefore not executed), using the following lines:
You get the result:
It seems to be what you wanted, but of course, with this simple example, it cannot be known.
If, on the other hand, we uncomment the line with the dubious assignment, the result is very different:
Which has all the earmarks of being wrong.
Additional notes
It is not a good idea to have global variables like
sobre_mi
,sobre_ti
,conceptos_de_mi
, that are manipulated from those functions. The best way to avoid getting involved is that you pass the dictionary that it must manipulate as a parameter to each function. For example:and invoke it for example like this:
Similarly, for the function
corregir
, it should receive as a parameter the dictionary on which it must operate.There is a problem when changing one root dictionary for another and that is that if there is a root key equal to the first key of the corrected word, the old root key will be deleted with all its content, so it is necessary to save everything from the root dictionary. old root and then store it back in the newly created dictionary.
It's new correct function works like this, do a For for the sentences to compare, the first if takes the first word that will be used to send the prefix to a function called printx, this function takes all the branches of a dictionary and prints, but in this case we are not going to print it but once we copy the dictionary before it is eliminated by the bug we will send it to obtain its branches, once we finish changing all the words and the new dictionary exists, then the function printx is activated when completing the For, the copied old dictionary and the prefix, the first word of that old dictionary, are sent to printx.
The printx function forms the branches of the old dictionary into strings, and each string is then sent to another function called learng.
The function learng stores the string in its respective order inside the new dictionary (The old dictionary is stored in the new one), now let's do the experiment.
We create a nested dictionary of (Yo)
We created a pre-existing nested dictionary of (Her) which is the one that was removed by the bug and will now only be added to the new one.