Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is being done in python. What do I need to change? The errors That I get are Traceback (most recent call last): File C:/Users/OneDrive/Desktop/New

This is being done in python.

What do I need to change?

The errors That I get are Traceback (most recent call last): File "C:/Users/OneDrive/Desktop/New folder (4)/Project.py", line 10, in contents = input_file.read() File "C:\Users\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1472: character maps to

My Coded

import string

# ask user for filename filename = input("Enter the filename: ")

# open input file for reading with open(filename, 'r') as input_file:

# read contents of file contents = input_file.read()

# count number of lines, words, and characters num_lines = contents.count(' ') + 1 num_words = len(contents.split()) num_chars = len(contents)

# output line, word, and character counts to console print(f"Number of lines: {num_lines}") print(f"Number of words: {num_words}") print(f"Number of characters: {num_chars}")

# create list of unique words and their frequency counts words = contents.translate(str.maketrans('', '', string.punctuation + string.digits)) words = words.lower().split() word_counts = {} for word in words: if word not in word_counts: word_counts[word] = 1 else: word_counts[word] += 1

# sort word counts alphabetically and output to output file output_filename = "Analysis-" + filename with open(output_filename, 'w') as output_file: output_file.write(f"Number of lines: {num_lines} ") output_file.write(f"Number of words: {num_words} ") output_file.write(f"Number of characters: {num_chars} ")

output_file.write("Unique words: ") for word, count in sorted(word_counts.items()): output_file.write(f"{word} ({count}) ")

output_file.write(" Repeating word pairs: ") repeating_pairs = {} words = contents.translate(str.maketrans('', '', string.punctuation + string.digits)).lower().split() for i in range(len(words)-1): pair = (words[i], words[i+1]) if pair not in repeating_pairs: repeating_pairs[pair] = 1 else: repeating_pairs[pair] += 1

for pair, count in repeating_pairs.items(): if count > 1: output_file.write(f"{' '.join(pair)} ({count}) ")

output_file.write(" Analysis Summary: ") output_file.write(f"Total number of words: {num_words} ") output_file.write(f"Average word length: {num_chars/num_words:.2f} ") output_file.write(f"Number of unique words: {len(word_counts)} ") unique_word_lengths = [len(word) for word in word_counts.keys()] output_file.write(f"Average length of unique words: {sum(unique_word_lengths)/len(unique_word_lengths):.2f} ") output_file.write(f"Number of repeating word pairs: {len(repeating_pairs)} ")

# output repeating word pairs to console print(" Repeating word pairs:") for pair, count in repeating_pairs.items(): if count > 1: print(f"{' '.join(pair)} ({count})")

# output analysis summary to console print(" Analysis Summary:") print(f"Total number of words: {num_words}") print(f"Average word length: {num_chars/num_words:.2f}") print(f"Number of unique words: {len(word_counts)}") unique_word_lengths = [len(word) for word in word_counts.keys()] print(f"Average length of unique words: {sum(unique_word_lengths)/len(unique_word_lengths):.2f}")

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

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions