Question

In: Computer Science

Write a code to find the following in a text file (Letter). language: Python (a) Find...

Write a code to find the following in a text file (Letter). language: Python

(a) Find the 20 most common words
(b) How many unique words are used?
(c) How many words are used at least 5 times?
(d) Write the 200 most common words, and their counts, to a file.

text file:

Look in thy glass and tell the face thou viewest,
Now is the time that face should form another,
Whose fresh repair if now thou not renewest,
Thou dost beguile the world, unbless some mother.
For where is she so fair whose uneared womb
Disdains the tillage of thy husbandry?
Or who is he so fond will be the tomb,
Of his self-love to stop posterity?
Thou art thy mother's glass and she in thee
Calls back the lovely April of her prime,
So thou through windows of thine age shalt see,
Despite of wrinkles this thy golden time.
But if thou live remembered not to be,
Die single and thine image dies with thee.


4
Unthrifty loveliness why dost thou spend,
Upon thy self thy beauty's legacy?
Nature's bequest gives nothing but doth lend,
And being frank she lends to those are free:
Then beauteous niggard why dost thou abuse,
The bounteous largess given thee to give?
Profitless usurer why dost thou use
So great a sum of sums yet canst not live?
For having traffic with thy self alone,
Thou of thy self thy sweet self dost deceive,
Then how when nature calls thee to be gone,
What acceptable audit canst thou leave?
Thy unused beauty must be tombed with thee,
Which used lives th' executor to be.

5
Those hours that with gentle work did frame
The lovely gaze where every eye doth dwell
Will play the tyrants to the very same,
And that unfair which fairly doth excel:
For never-resting time leads summer on
To hideous winter and confounds him there,
Sap checked with frost and lusty leaves quite gone,
Beauty o'er-snowed and bareness every where:
Then were not summer's distillation left
A liquid prisoner pent in walls of glass,
Beauty's effect with beauty were bereft,
Nor it nor no remembrance what it was.
But flowers distilled though they with winter meet,
Leese but their show, their substance still lives sweet.

Solutions

Expert Solution

import collections
file = open('file1.txt', encoding="utf8")
b= file.read()
unique=0
time5=0
wc = {}
for w in b.lower().split():
w = w.replace(".","")
w = w.replace(",","")
w = w.replace(":","")
w = w.replace("\"","")
w = w.replace("!","")
w = w.replace("â??","")
w = w.replace("â??","")
w = w.replace("*","")
if w not in wc:
wc[w] = 1
else:
wc[w] += 1
n_print = int(input("How many most appeared words to print: "))
print("\nOK. The {} most counted words in the txt file are as follows\n".format(n_print))
word_counter = collections.Counter(wc)
for w, c in word_counter.most_common(n_print):
print(w, ": ", c)

print("\nOK. all unique words are as follows\n".format(n_print))
for w, c in word_counter.most_common():
if c==1:
print(w, ": ", c)
unique=unique+1
print("\nOK. Total number of unique words are ",unique )

print("\nOK. all words with count more than 5 are as follows\n".format(n_print))
for w, c in word_counter.most_common():
if c>=5:
print(w, ": ", c)
time5=time5+1
print("\nOK. Total number of words appeared equal or more than 5 times are ",time5 )

print("\nOK. The 200 most common words are in the file com200.txt\n".format(n_print))
word_counter = collections.Counter(wc)
f1= open("com200.txt","w+")
for w, c in word_counter.most_common(200):
f1.write(w)
f1.write("\n")
f1.close()

# Close the file
file.close()

#############################################################################

output:

##########################################################################

How many most appeared words to print: 20

OK. The 20 most counted words in the txt file are as follows

thou : 11
thy : 10
the : 10
and : 8
of : 8
to : 8
with : 7
dost : 5
be : 5
thee : 5
not : 4
so : 4
but : 4
self : 4
in : 3
glass : 3
is : 3
time : 3
that : 3
for : 3

OK. all unique words are as follows

ok : 1
tell : 1
viewest : 1
should : 1
form : 1
another : 1
fresh : 1
repair : 1
renewest : 1
beguile : 1
world : 1
.

..........

OK. Total number of unique words are 150

OK. all words with count more than 5 are as follows

thou : 11
thy : 10
the : 10
and : 8
of : 8
to : 8
with : 7
dost : 5
be : 5
thee : 5


OK. Total number of words appeared equal or more than 5 times are 10

OK. The 200 most common words are in the file:com200.txt


Related Solutions

Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), If I use my own file though, you should handle as many fortunes as I put in. Make each fortune its own line, •in your main function ask the user for the name of the fortunes file.•Create a function which takes the name of the fortunes file as a parameter, open that...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT