Write a function wordcount() that takes the name of a text file as input and prints the number of occurrences of every word in the file. You function should be case-insensitive so 'Hello' and 'hello' are treated as the same word. You should ignore words of length 2 or less. Test your implementation on file great_expectations.txt¶

Respuesta :

Answer:

I am writing a Python program:

import string  # here is this is used in removing punctuation from file

def wordcount(filename):  #definition of wordcount method that takes file name as argument

   file  = open(filename, "r")  #open method opens the file whose name is specified and r is for read mode. Object "file" is used to open a file in read mode

   count = dict()  #creates a dictionary

   for strings in file:  # iterates through every line of the text file

       strings = strings.strip()  #strip() method removes the white spaces from every line of the text file

       strings = strings.lower()  #converts all the characters of the text file to lower case to make function case insensitive

       strings = strings.translate(strings.maketrans("", "", string.punctuation))  # deletes punctuation each line of the text file

       words = strings.split(" ")  #split the lines of file to a list of words

       for word in words:  #iterates through each words of the list of words

           if len(word)>2:  #ignores words of length 2 or less

               if word in count:  #if word is present in dictionary count already

                   count[word] = count[word] + 1  #then add 1 to the existing word count

               else:  #if the word is not present already in the dictionary

                   count[word] = 1  # set the count of the word that is not already present to 1 in order to add that word to dictionary

   for key in list(count.keys()):  # iterates through each key (words) in the list of words and print the contents of dict counts i.e. words along with their no. of occurrences

       print(key, ":", count[key])  # print the words and their occurences in word:occurence format for example hello:1 means hello exists once in text file

       file.close()  #closes the file

#two statements first ask the user to enter the name of the file and then call the function wordcount() by passing the name of that file in order to print the number of occurrences of every word in the file.

name = input('Enter a filename: ')

wordcount(name)

Explanation:

The program is well explained in the comments above. The program has a function wordcount() that takes a text file name as its parameter. First it opens that file in read mode. Then the first for loop moves through each line of the text file and removes the white spaces from every line of the text file using split() method, converts all the characters of the text file to lower case to make function case insensitive using lower() method, deletes punctuation each line of the text file  using maketras() method and translate methods. maketrans() method specify the list of punctuation that need to be  remove from the each line of the file. This method then returns this list in the form of a table which is passed to translate() method. Then translate() method uses this table to modify lines in order to remove the punctuation from lines. Next the lines are split into list of words using split() method. Then the second for loop moves through each word and counts the number of occurrences of each word in the text file. len(word)>2: len function returns the length of the word and if the length of the word is less than 2 than that word is ignored. count is set to 1, every time a new word is added to dictionary and if the word is already present in the dictionary then the count of word is incremented by 1. At the end the contents of dictionary are displayed in key: count[key] format in which key is each word and value is the number of occurrences of that word. Since the great_explectations.txt file is not attached so i have used my own file. You can use this code for you file.

Ver imagen mahamnasir
Ver imagen mahamnasir