Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DO NOT ANSWER IF YOU CANNOT USE THE TEMPLATE!!! Your task is to write a Python program that opens and reads a very large text

DO NOT ANSWER IF YOU CANNOT USE THE TEMPLATE!!!

Your task is to write a Python program that opens and reads a very large text file. The program p...

Your task is to write a Python program that opens and reads a very large text file.

The program prompts the user to enter the file name. The program then computes some language statistics based on the contents of the file.

The longest word used in the file. If there is more than one, just print one of the longest. There is no need to find all the longest words.

The five most common words in the file with the number of times they appear in the file.

The word count of all the words in the file, sorted alphabetically this last output has to be written to a file in the current working directory with the name out.txt.

Open this file for writing in w mode. Your program must work with any input text file that uses 'UTF-8' encoding.

The program must also be efficient, computing statistics for a large file (pride.txt) in seconds.

Dont have to worry about validating the input at this point. Assume that the file name provided by the user exists.

However you only want to open the file once and read each line once.

Make sure that you ignore capitalization when you process the file.

Also make sure that you take out leading and trailing punctuation characters and numbers from your words.

__________________________________________________________________________________________________________________________________

Template I have to use

import string

#The following imports are needed for the draw_cloud function.

import tkinter

import tkinter.font

import random

# The draw_cloud function is only needed for the optional part:

# to generate a word cloud.

# You don't need to change it.

def draw_cloud(input_count, min_length=0):

"""

Generate a word cloud based on the input count dictionary specified.

Parameters:

input_count (dict): represents words and their corresponding counts.

min_length (int): optional - defaults to 0.

minimum length of the words that will appearin the cloud representation.

Only the 20 most common words (that satisfy the minimum length criteria)are included in the generated cloud.

"""

root = tkinter.Tk()

root.title("Word Cloud Fun")

# filter the dictionary by word length

filter_count = {

word: input_count[word] for word in input_count

if len(word) >= min_length}max_count = max(filter_count.values())

ratio = 100 / max_count

frame = tkinter.Frame(root)

frame.grid()

current_row = 0

for word in sorted(filter_count, key=filter_count.get, reverse=True)[0:20]:

color = '#' + str(hex(random.randint(256, 4095)))[2:]

word_font = tkinter.font.Font(size=int(filter_count[word] * ratio))

label = tkinter.Label(frame, text=word, font=word_font, fg=color)

label.grid(row=current_row % 5, column=current_row // 5)current_row += 1

root.

mainloop()

# Enter your own helper function definitions here

def count_words(filename):

""" Enter your function docstring here """

# build and return the dictionary for the given filename

def report(word_dict):

""" Enter your function docstring here """

# report on various statistics based on the given word count

dictionary

def main():

# get the input filename and save it in a variable

# call count_words to build the dictionary for the given file

# save the dictionary in the variable word_count

# call report to report on the contents of the dictionary word_count

# If you want to generate a word cloud, uncomment the line below.

# draw_cloud(word_count)

if __name__ == '__main__':

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions