1
Well I’m asking this question, because I’ve tried several ways, but I can’t find the error in logic or the wrong use of some syntax in python.
It would be as follows, I own this class:
class PalavraIndices(object):
def __init__(self,palavra):
self.palavra = palavra
self.items_in_Indices = {}
def __repr__(self):
return str(self.palavra)
def add_item(self, arquivo_indice, qtd_repeticao):
if bool(self.items_in_Indices) == False: # dicionario estiver vazio
self.items_in_Indices[arquivo_indice] = qtd_repeticao
else:
if not arquivo_indice in self.items_in_Indices: #nome do arquivo base não existir
self.items_in_Indices[arquivo_indice] = qtd_repeticao
else:
self.items_in_Indices[arquivo_indice] += qtd_repeticao
def imprime_indices(self):
conteudo = self.palavra + ":"
arquivos = list( self.items_in_Indices.keys() )
arquivos.sort()
for arq in arquivos:
conteudo = conteudo + " %s,%s" %(arq, self.items_in_Indices[arq])
print(conteudo)
I’m adding words like objects to a list of objects to mark the amount and in which file the word appears, having something like this at the end:
Trolley: (arq1,2),(arq2,1),(arq3,4)
I store in this format in the class, when I instate an object(word), inside this object I use a dictionary.
I have three lists:
ListaA = ['modo', 'consulta', 'diversos', 'militantes', 'acarreta',
'processo', 'reformulacao', 'modernizacao', 'fluxo', 'informacoes',
'diversos', 'poderes', 'mundo']
ListaB = ['evidentemente', 'determinacao', 'clara', 'objetivos', 'promove',
'alavancagem', 'niveis', 'motivacao', 'departamental']
ListaC = ['gostaria', 'enfatizar', 'percepcao', 'dificuldades', 'cumpre',
'papel', 'essencial', 'formulacao', 'diretrizes', 'desenvolvimento',
'futuro', 'papel', 'arvore']
This function checks if the object already exists in the object list
def busca_index(lista_objetos,palavra):
contador = 0
aux = 0
for objeto in lista_objetos:
if(objeto.palavra == palavra):
return contador
contador += 1
return -1
Function where I create the list of objects:
def guarda_word(lista_palavras,lista_objeto,indice):
existe = -1
for palavra in lista_palavras:
objetoPalavra = PalavraIndices(palavra)
if bool(lista_objeto) == False: # a list de objeto esta vazia
objetoPalavra.add_item(indice,1)
lista_objeto.append(objetoPalavra)
else:
existe = busca_index(lista_objeto,palavra)
if(existe != -1):
lista_objeto[existe].add_item(indice,1)
existe = -1
else:
objetoPalavra.add_item(indice,1)
lista_objeto.append(objetoPalavra)
and finally the calls:
lista_objeto = []
guarda_word(ListaA,lista_objeto,'arquivoA')
guarda_word(ListaB,lista_objeto,'arquivoB')
guarda_word(ListaC,lista_objeto,'arquivoC')
print("lista de objeto")
print(lista_objeto)
for objeto in lista_objeto:
objeto.imprime_indices()
The generated output is:
consulta
{'arquivoA': 13, 'arquivoB': 9, 'arquivoC': 13}
but it is not counting correctly... Anyway.. I do not know if I could understand, and I’m sorry if it became giant this question... But if someone is interested in trying to find out why each object word is not correctly computing the amounts of times it repeats.
I think you’re making a simple problem too difficult, you necessarily want to do it by POO or it can be otherwise?
– user92257
then @Ruyneto is because using this object, the ways I thought, would be the easiest way to record the information of each word in that output format. and in this case I do not see the object as a complicator but a facilitator.. the problem is in this function that I list the objects, because by my tests the problem is not in the class, but in this function
guarda_word
that I think there must be some wrong reference. for example... instance this class and add items to it and then printobjeto.imprime_indices()
you will notice that this accounting correctly– William Henrique
@Ruyneto discovered the problem! the error is in
def __init__(self,palavra):
I’ll update td for you to see– William Henrique
Good morning. The output of the program here https://repl.it/MQXR/2 is nothing like the one you put in the question.
– Miguel
@Miguel is because as I mentioned in the comments above.. I managed to solve the problem and updated the code in the question to what is working
– William Henrique
@Williamhenrique ok, I get it
– Miguel