Question

In python Count the frequency of each word in a text file. Let the user choose...

In python Count the frequency of each word in a text file. Let the user choose a filename to read.

1. The program will count the frequency with which each word appears in the text.

2. Words which are the spelled the same but differ by case will be combined.

3. Punctuation should be removed

4. If the file does not exist, use a ‘try-execption’ block to handle the error

5. Output will list the words alphabetically, with the word in the first column and the count in the second column … i.e. as : 1287

Psuedo Code

printWds(data)

## print each word in our data and the respective count

## but we need them sorted, dictionaries have a nice module, sorted

## and lists have sort. for x in sorted(data.keys()):

print() ## look at the formatting above

return

wordFreq(fptr)

## create empty data type, dictionary or list?

freq = {} or freq = []

## read in the first line

line = fptr.readline()

punctChars = (….create a tuple of punctuation characters to eliminate… )

while line:

for c in punctChars:

## use the string replace module to substitute an empty char

line = line.replace(c, “”)

## create a list of separate words that make up the string

words = line.split()

for each word in our split:

#create a temporary word converted to lower using the lower()

tmp = word.lower()

## add this to your database, this is where dictionaries have the

## advantage over dictionaries … the get() method

freq[tmp] = freq.get(tmp, 0) + 1

read another line

return freq

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: USE PYTHON3

def txt_list(path):
file = open(path, 'r')
file_arr = []
file_str = ''
punc = ['!','.','/',',',':',';','?','\'','"']
for word in file.read():
if(word not in punc):
file_str = file_str + word
file_arr = list(map(str,file_str.lower().split()))
return sorted(file_arr)
def word_freq(arr):
_dict = {}
for x in arr:
if x not in _dict:
_dict[x] = 1
else:
_dict[x]+= 1
return _dict

try:
path = input('Enter the file name: ')
txt = txt_list(path)
print(word_freq(txt))

except:
print("No such file exists.")

EXECUTION:

Add a comment
Know the answer?
Add Answer to:
In python Count the frequency of each word in a text file. Let the user choose...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • 9. A concordance is an alphabetical word list for a passage of text. Each word in...

    9. A concordance is an alphabetical word list for a passage of text. Each word in the concordance is mapped to an integer indicating the frequency of the word's occurrence. The constructor of Concordance has one String parameter that identifies the text file to be read. An incompleteConcordance class is below. public class Concordance public Concordance (String nameOfFileH concord new TreeMap<String, Integer 0 createConcordance (nameOfFile); //Constructor //nameOfFile the text file being read public void createConcordance (string filename)...) //Create a TreeMap...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Python Programming Write a program that counts how often a word occurs in a text file....

    Python Programming Write a program that counts how often a word occurs in a text file. Input: Ask the user for the name of an ASCII text file. Output: Display the numbers of top frequently appeared words and their frequencies. Pseudocode: 1) Read the file 2) Split the file into words 3) Count each word 4) Sort the words by frequencies, starting with the most frequent ones 5) Internally you should use some sort of data structure to keep track...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...

    Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1c Unique Words Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and a list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT