Question

Write a python program that will perform text analysis on an input text using all of...

Write a python program that will perform text analysis on an input text using all of
the following steps:
1. Take the name of an input file as a command line argument.
For example, your program might be called using
python3 freq.py example1
to direct you to process a plain text file called example. More information is given on
how to do this below.
2. Read the contents of the file into your program and divide the text into a list of
individual, space-separated words
3. Analyze the word list to count the number of times each word appears in the text.
Hint: You may want to use a dictionary to store and track the counts.
4. When the counting is finished, your program should write a frequency table of each
word, its count, and its relative frequency to an output file. The frequency table must
be sorted in lexicographic order (alphabetical with uppercase words first)
by word in the text. You may want to look up the sorted function to help you with
this

each line in the table should be like

word counted_value frequency

where
word is the word in the text
counted_value is the number of times the word mentioned occurs
frequency is a number in the range [0,1] that is the ratio of the count for the word to the
total number of words found in the text. You will need to calculate this frequency
once you have counted the number of times each word appears

You must write this frequency table to an output file.

Homework Answers

Answer #1

CODE -

import sys

# Creating a string for file name to open

filename = sys.argv[1] + ".txt"

# Opening file

file = open(filename)

# Reading the contents of the file and splitting the text into a list of individual space-seperated words.

list_words = file.read().split()

# Closing the file

file.close()

# Creating an empty dictionary to store the count of each words.

dict_words = {}

# Iterating over each word in the list

for word in list_words:

    # Adding word to the dictionary and a count of 1 if the word is not already in the dictionary

    if word not in dict_words:

        dict_words[word] = 1

    # Increasing count of word in the dictionary by 1 if the word is already in the dictionary

    else:

        dict_words[word] += 1

# Opening the output file for writing

file = open("output.txt", "w")

# Writing the header for the frequency table in the file

file.write("{:<15}{:<15}{:<15}\n" .format("Word", "Counted_value", "Frequency"))

# Writing the frequency of each word in the file in lexicographic order

for word in sorted(dict_words.keys()):

    file.write("{:<15}{:<15}{:<15.4f}\n" .format(word, dict_words[word], dict_words[word] / len(dict_words)))

# Closing the file

file.close()

SCREENSHOTS -

INPUT TEXT FILE -

CODE -

OUTPUT TEXT FILE -

If you have any doubt regarding the solution, then do comment.
Do upvote.

Know the answer?
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
Similar Questions
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
Write a function that accepts a line of text and a single letter as input (case...
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 first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
python program on sonnets to Count the occurrences of each letter in the text.,. Print the...
python program on sonnets to Count the occurrences of each letter in the text.,. Print the number of one-letter, two-letter, three-letter words and so on. and Print the number of occurrences of each different word in the text using  the text from the sonnets.txt file and apply these three metrics
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
C++ Part B: (String) Program Description: Write a word search program that searches an input data...
C++ Part B: (String) Program Description: Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)...
Write a python program that reads a word list and prints out all the anagrams in...
Write a python program that reads a word list and prints out all the anagrams in the word list. In earlier exercises you saw how to read a word list. Also, in earlier examples we saw how the sorted characters of a word are a useful canonical representation of an anagram (Hint: Therefore, useful as a key).
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT