How to get return of the amount of a word existing inside a .txt file? [duplicate]

0

I'm trying to get the number of low-level words (swear words) back in a text, using as base a different .txt file containing the words (profanity). The code I made only returns 1 (one) occurrence, and there are 3 (three). What could I do to improve?

def read_file():
    with open(r'C:\movie_quotes.txt') as file: # Abertura do arquivo a ser analisado. 
        contents = file.read()
    print(contents)
    file.close()
    check_file(contents)


def check_file(text_check):
    bad_words = open(r'C:\palavroes_bloqueio.txt') # Palavras a serem procuradas.
    contents = list(bad_words)
    # print(contents)
    for name in contents:
        if name in text_check:
            print('Bad words found.')
            print(text_check.count(name))
    bad_words.close()


read_file()
    
asked by anonymous 23.05.2018 / 00:12

1 answer

0

I think the problem is that your second file has one word per line, and then when you go looking in the text for that word, it looks for the word and the line break and can not find.

For example, file_framework.txt :

foo
bar

When searching the text, it will look for foo \ n and not just foo

What you can try is to change this snippet:

bad_words = open(r'C:\palavroes_bloqueio.txt') # Palavras a serem procuradas.
contents = list(bad_words)

By this stretch:

with open(r'C:\palavroes_bloqueio.txt') as bad_words:
    contents = bad_words.read()

# Divide as palavras usando o delimitador \n
contents = contents.split('\n')

Note: The file is already automatically closed if you use the with open('..') as filename directive, so you do not need to call filename.close() then

    
23.05.2018 / 14:24