Question

In: Computer Science

In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...

In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.

Solutions

Expert Solution

#Program to get dictionary of words and its frequecy for input file

Feel free to ask any doubt. I will try to clear any doubt

import re

import string



#function to get dictionary of words and its frequecy for input file

def wordfreq(filename = "somefile.txt"):

  #create a dictionary

  word_freq_dict = {}



  #Open file

  document_text = open('Sample.txt', 'r')



  #read file and convert everything in lower

  text_string = document_text.read().lower()



  #regular expression that would return all the words with the number of characters in the range [1-15]

  match_pattern = re.findall(r'\b[a-z]{1,15}\b', text_string)



  #get the count of all words 

  for word in match_pattern:

    count = word_freq_dict.get(word,0)

    word_freq_dict[word] = count + 1



  return word_freq_dict



#Main program

if __name__ == "__main__":

  word_freq_dict = wordfreq("ample.txt")

  print(word_freq_dict)

Explanation:

1) Above program has a function wordfreq which returns dictionary of words and its frequecy for input file

2) It has a main program at the bottom, where this function is called with input file

3) Copy above code in python file and run,Make sure you have input file exist

4) Below is test output

{'department': 1, 'of': 1, 'computer': 1, 'science': 1, 'university': 1, 'major': 2, 'in': 2, 'information': 2, 'technology': 2, 'test': 2, 'my': 2, 'name': 2}


Related Solutions

In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
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 function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read...
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read from, the method then reads all the records from the file and returns a dictionary. Each record in the file has the following format CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address Etc A record always consists of 4 lines (civilid, name, telephone and address). You can find a sample input file on last page of this assignment, copy it...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the ignore character as well as any blank lines. """ return ['a', 'z'] How would I code this in Python?
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT