Question

In: Computer Science

Write a python program function to check the frequency of the words in text files. Make...

Write a python program function to check the frequency of the words in text files. Make sure to remove any punctuation and convert all words to lower case.

If my text file is like this:

Hello, This is Python Program? thAt chEcks% THE freQuency of the words!

When is printed it should look like this:

hello 1

this 1

is 1

python 1

program 1

that 1

checks 1

the 2

frequency 1

of 1

words 1

Solutions

Expert Solution

Explanation:

Here is the code which takes the filename from the user in which the lines are stored.

Then the file is opened and the words are stored inside a list.

Then, all the words are converted to lowercase and all the punctuations are removed from the words.

Then a dictionary maintaining the count of the words is created named d.

Then it is printed at the end using a for loop.

Code:


filename = input("Enter filename: ")

f = open(filename, 'r')

words = f.read().split(" ")

f.close()

d = {}

for i in range(len(words)):
words[i] = words[i].lower()
  
if(not words[i][-1].isalpha()):
words[i] = words[i][:-1]
  
if words[i] not in d:
d[words[i]] = 1
else:
d[words[i]] = d[words[i]] + 1
  
for k, v in d.items():
print(k, v)
  

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Write a Python program that will process the text file, Gettysburg.txt, by calculating the total words...
Write a Python program that will process the text file, Gettysburg.txt, by calculating the total words and output the number of occurrences of each word in the file. The program needs to open the file and process each line. You need to add each word to the dictionary with a frequency of 1 or update the word’s count by 1. You need to print the output from high to low frequency. The program needs 4 functions. The first function is...
a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
Python Text processing/masks Write a function called unique words that takes a phrase as an input...
Python Text processing/masks Write a function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order. Scriipt for ATOM or REPL, NOT PYTHON SHELL
Write a program in Python that walks through a folder tree and searches for files with...
Write a program in Python that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder. The user can enter an absolute path for the start folder, or if the user does not enter a folder, the current directory is used. Likewise, the user can enter extensions to copy but if the user does not enter an extension,...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
In c++ please. Write a program that reads the contents of two text files and compares...
In c++ please. Write a program that reads the contents of two text files and compares them in the following ways: It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in the second file,...
2. working with databases and files in python a) Write a function with prototype “def profound():”...
2. working with databases and files in python a) Write a function with prototype “def profound():” that will prompt the user to type something profound. It will then record the date and time using the “datetime” module and then append the date, time and profound line to a file called “profound.txt”. Do only one line per function call. Use a single write and f-string such that the file contents look like: 2020-10-27 11:20:22 -- Has eighteen letters does 2020-10-27 11:20:36...
Write a python program that asks the user about their emails and then check the following:...
Write a python program that asks the user about their emails and then check the following: the entered email is a valid email address (email format is [USERNAME]@[ORGANIZATION].[DOMAIN]. i.e. it should have '@' followed by organization name then at least one '.'and domain) determine if the entered email is a KFUPM email or not (KFUPM emails ends with @kfupm.edu.sa) if the entered email is a valid KFUPM email, determine if it is a freshman student email or not (assume all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT