Question

In: Computer Science

Write a function in Python to take in a piece of sample text via a long...

Write a function in Python to take in a piece of sample text via a long string (you pick the string) and to output a dictionary with each word as a key and it’s frequency (the number of times it occurred in the original string) as the value. Show an example of your function in use. Be sure to strip out any punctuation.

Solutions

Expert Solution

Solution : The code works totally fine with large strings as well. I have checked it.

import re #importing regular expression module

test_string='''Nory was a Catholic because her mother was a Catholic, and the mother was a Catholic because her father was a Catholic, and her father was a Catholic because his mother was a Catholic, or had been. '''

res = re.findall(r'\w+', test_string) #extracting words from string and also it ignores the punctuations
frequency = {} #empty dictionary

for item in res: #looping through list 'res' and creating the required dictionary
  if (item in frequency):
    frequency[item]+=1
  else:
    frequency[item]=1
print("The required dictionary content :-\n")
for key, value in frequency.items(): #loop to print the value and key from the dictionary
  print ("% s : % d"%(key, value))

Output :


Related Solutions

Write a function in Python which removes a set of common words from a long piece...
Write a function in Python which removes a set of common words from a long piece of text. Be sure to strip out any punctuation. The common words to be removed are: a, an, as, at, by, for, in, is, it, of, that, this, to, was, will, the These are typical words that are considered to have low semantic value. Process each paragraph provided below individually. Your end result for each paragraph should be a string or list containing the...
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
(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.
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
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
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
Python 3 Write the definition of a function that take one number, that represents a temperature...
Python 3 Write the definition of a function that take one number, that represents a temperature in Fahrenheit and prints the equivalent temperature in degrees Celsius. Write the definition of another function that takes one number, that represents speed in miles/hour and prints the equivalent speed in meters/second. Write the definition of a function named main. It takes no input, hence empty parenthesis, and does the following: - prints Enter 1 to convert Fahrenheit temperature to Celsius - prints on...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!) for example: hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT