0
I want to check the frequency of words returned in a book (format .txt
) and divide this frequency by the number of chapters found, thus obtaining the average word appearance per chapter.
The point is that I want to return this result within a dictionary. Example of expected return:
{'casa': 4.9000090}
{'homem': 2.256535434}
I already managed to get the expected result without using the dictionary as a return, but I would like to concatenate the information to stay the way above.
Someone to point out the best way?
My code is like this:
#Código do meu arquivo auxiliar.py que contém as funções de leitura de texto
def abre_texto():
return open('livroExemplo.txt', 'r')
def consulta_novo_capitulo(line)
return bool(re.search(r'^Capítulo\s\d\d', line))
#Função principal
import auxiliares as aux
def media_palavras(palavra):
thisdict = {}
newChapter = 0
for line in aux.abre_texto():
if aux.consulta_capitulo(line) == True:
newChapter += 1
for word in line.split():
if word not in thisdict:
thisdict[word] = 1
else:
thisdict[word] += 1
print("Existem",newChapter,"capítulos.")
print("A palavra consultada {",palavra,"} apareceu",thisdict[palavra],"vezes no livro.")
print("Média de aparição da palavra consultada:",(thisdict[palavra])/newChapter)
media_palavras('casa')