dimanche 26 juillet 2020

Getting incorrect result in list comprehension python

What am i doing wrong in the following code. I have if statement to avoid any digit but they still appear in the text and empty string also included in the text. The last one i want is to avoid words from uninteresting_words list.

def calculate_frequencies(file_contents):
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
        
    # Split the file_contents into list of words
    words = file_contents.split()
    # Remove the punctuations from the text
    table = str.maketrans("", "", string.punctuation)
    
    stripped = [word.translate(table) 
                for word in words 
                    if word.isdigit() != True # Word should not be a digit
                    and word # There shouldn't be any space
                    and (word.lower() not in uninteresting_words) # check on each word if it's not present in un_interesting words
                    
               ]
    # Count words
    word_count = {}
    for word in stripped:
        if word in word_count.keys():
            word_count[word] += 1
        else:
            word_count[word] = 1

Aucun commentaire:

Enregistrer un commentaire