-3
I have a problem that when I try to return the words I intend it also returns the words that have a comma or any character.
For example,
"I have this phrase that serves, to serve and that serves[1] of serves."
And when I try to print it back
{'serve':(4,1)}
and wanted you to return {'serve':(1,1)}
because I don’t want the words with , and [1] and .
I have these four functions:
- parsePalavras serves to count how many times the word appears in a . txt
- the ler_words function is used to print the list with the words that are in a . txt
- function parse lines prints the number of the line where the word is
- the found function takes a file . txt and uses those words to check where they are in the other file
def analisarPalavras(nomeficheiro,string):
with open(nomeficheiro,'r') as f:
quantas = 0
for linha in f:
if string in linha:
quantas = quantas + 1
return quantas
def ler_palavras(nomeficheiro):
with open(nomeficheiro,'r') as f:
listaFinal = []
listaIndividual = [linha.split() for linha in f]
listaJunta = sum(listaIndividual,[])
for i in listaJunta:
if i not in listaFinal:
listaFinal.append(i)
return listaFinal
def analisarLinhas(nomeficheiro,palavra):
linhaNumero = 0
listaResultado = []
with open(nomeficheiro,'r') as f:
for linha in f:
linhaNumero = linhaNumero + 1
if palavra in linha:
listaResultado.append(linhaNumero)
return listaResultado
def encontra_ocorrencias(nomeficheiro1,nomeficheiro2):
with open (nomeficheiro1,'r') as textoAnalisar, open(nomeficheiro2,'r') as palavrasInteresse:
palavrasLidas = ler_palavras(nomeficheiro2)
dicionario = {}
for i in range(len(palavrasLidas)):
palavraImprimir = (palavrasLidas[i])
palavraLinha = (analisarPalavras(nomeficheiro1,palavraImprimir))
numeroLinhas = (analisarLinhas(nomeficheiro1,palavraImprimir))
dicionario.update({palavraImprimir: (palavraLinha,numeroLinhas)})
return dicionario
Could you explain why I voted no so that I can improve the answer?
– Augusto Vasques