Question
def word_frequency(item): return item[1] def top_ten_words(): punctuations = '''!()-[]{};:', ./?@#$%^&*_~''' content= stop_word= stop_words=[] number_of_words=[] frequency_table=[] words_dictionary={} file=rC:UsersleeweDocumentsdsag6101128.txt stop_words_english=rC:UsersleeweDocumentsdsag6101235.txt with open(file,'r',encoding='utf8') as f: for line in
def word_frequency(item): return item[1] def top_ten_words(): punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' content="" stop_word="" stop_words=[] number_of_words=[] frequency_table=[] words_dictionary={}
file=r"C:\Users\leewe\Documents\dsag\6101128.txt" stop_words_english=r"C:\Users\leewe\Documents\dsag\6101235.txt"
with open(file,'r',encoding='utf8') as f: for line in f: if line==" ": continue else: content+=line.strip().lower()+" "
for character in punctuations: if character in content: content=content.replace(character,"") with open(stop_words_english,'r',encoding='utf8') as s: for line in s: line=line.rstrip() for character in punctuations: if character in line: line=line.replace(character,"") stop_words.append(line.lower()) for word in stop_words: stop_word=" "+word+" " if stop_word in content: content=content.replace(stop_word," ")
number_of_words = content.split(" ")
for word in number_of_words: words_dictionary[word] = words_dictionary.get(word,0)+1
frequency_table=list(words_dictionary.items())
frequency_table.sort(reverse=True,key=word_frequency)
for i in range(10): print(frequency_table[i][0],frequency_table[i][1])
top_ten_words()
Hi, I need help with changing the code for these to a simpler and shorter one. However, the output will still be the same! Please write #comments between each code for me to understand too! a good review will be given if answered correctly! I have been asking this question a few times and the answer are not of any help to me! it's python by the way and thanks in advance!
this is the question
Create a dictionary to count the number of words in the book. Use the stop_words_english to remove any stop words found in the book. Remove all punctuations using the following punctuations variable.
The book: The Project Gutenberg eBook of The Adventures of Sherlock Holmes, by Arthur Conan Doyle
This eBook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United States, you
will have to check the laws of the country where you are located before
using this eBook.
Title: The Adventures of Sherlock Holmes
Author: Arthur Conan Doyle
Release Date: November 29, 2002 [eBook #1661]
[Most recently updated: May 20, 2019]
Language: English
Character set encoding: UTF-8
Produced by: an anonymous Project Gutenberg volunteer and Jose Menendez
* START OF THE PROJECT GUTENBERG EBOOK THE ADVENTURES OF SHERLOCK HOLMES *
cover
The Adventures of Sherlock Holmes
by Arthur Conan Doyle
Contents
I. A Scandal in Bohemia
II. The Red-Headed League
III. A Case of Identity
IV. The Boscombe Valley Mystery
V. The Five Orange Pips
VI. The Man with the Twisted Lip
VII. The Adventure of the Blue Carbuncle
VIII. The Adventure of the Speckled Band
IX. The Adventure of the Engineers Thumb
X. The Adventure of the Noble Bachelor
XI. The Adventure of the Beryl Coronet
XII. The Adventure of the Copper Beeches
I. A SCANDAL IN BOHEMIA
I.
To Sherlock Holmes she is always the woman. I have seldom heard him
mention her under any other name. In his eyes she eclipses and
predominates the whole of her sex. It was not that he felt any emotion
akin to love for Irene Adler. All emotions, and that one particularly,
were abhorrent to his cold, precise but admirably balanced mind. He
was, I take it, the most perfect reasoning and observing machine that
the world has seen, but as a lover he would have placed himself in a
false position. He never spoke of the softer passions, save with a gibe
and a sneer. They were admirable things for the observerexcellent for
drawing the veil from mens motives and actions. But for the trained
reasoner to admit such intrusions into his own delicate and finely
adjusted temperament was to introduce a distracting factor which might
throw a doubt upon all his mental results. Grit in a sensitive
instrument, or a crack in one of his own high-power lenses, would not
be more disturbing than a strong emotion in a nature such as his. And
yet there was but one woman to him, and that woman was the late Irene
Adler, of dubious and questionable memory.
I had seen little of Holmes lately. My marriage had drifted us away
from each other. My own complete happiness, and the home-centred
interests which rise up around the man who first finds himself master
of his own establishment, were sufficient to absorb all my attention,
while Holmes, who loathed every form of society with his whole Bohemian
soul, remained in our lodgings in Baker Street, buried among his old
books, and alternating from week to week between cocaine and ambition,
the drowsiness of the drug, and the fierce energy of his own keen
nature. He was still, as ever, deeply attracted by the study of crime,
and occupied his immense faculties and extraordinary powers of
observation in following out those clues, and clearing up those
mysteries which had been abandoned as hopeless by the official police.
From time to time I heard some vague account of his doings: of his
summons to Odessa in the case of the Trepoff murder, of his clearing up
of the singular tragedy of the Atkinson brothers at Trincomalee, and
finally of the mission which he had accomplished so delicately and
successfully for the reigning family of Holland. Beyond these signs of
his activity, however, which I merely shared with all the readers of
the daily press, I knew little of my former friend and companion.
One nightit was on the twentieth of March, 1888I was returning from a
journey to a patient (for I had now returned to civil practice), when
my way led me through Baker Street. As I passed the well-remembered
door, which must always be associated in my mind with my wooing, and
with the dark incidents of the Study in Scarlet, I was seized with a
keen desire to see Holmes again, and to know how he was employing his
extraordinary powers. His rooms were brilliantly lit, and, even as I
looked up, I saw his tall, spare figure pass twice in a dark silhouette
against the blind. He was pacing the room swiftly, eagerly, with his
head sunk upon his chest and his hands clasped behind him. To me, who
knew his every mood and habit, his attitude and manner told their own
story. He was at work again. He had risen out of his drug-created
dreams and was hot upon the scent of some new problem. I rang the bell
and was shown up to the chamber which had formerly been in part my own.
His manner was not effusive. It seldom was; but he was glad, I think,
to see me. With hardly a word spoken, but with a kindly eye, he waved
me to an armchair, threw across his case of cigars, and indicated a
spirit case and a gasogene in the corner. Then he stood before the fire
and looked me over in his singular introspective fashion.
Wedlock suits you, he remarked. I think, Watson, that you have put
on seven and a half pounds since I saw you.
Seven! I answered.
Indeed, I should have thought a little more. Just a trifle more, I
fancy, Watson. And in practice again, I observe. You did not tell me
that you intended to go into harness.
Then, how do you know?
I see it, I deduce it. How do I know that you have been getting
yourself very wet lately, and that you have a most clumsy and careless
servant girl?
My dear Holmes, said I, this is too much. You would certainly have
been burned, had you lived a few centuries ago. It is true that I had a
country walk on Thursday and came home in a dreadful mess, but as I
have changed my clothes I cant imagine how you deduce it. As to Mary
Jane, she is incorrigible, and my wife has given her notice, but there,
again, I fail to see how you work it out.
He chuckled to himself and rubbed his long, nervous hands together.
It is simplicity itself, said he; my eyes tell me that on the inside
of your left shoe, just where the firelight strikes it, the leather is
scored by six almost parallel cuts. Obviously they have been caused by
someone who has very carelessly scraped round the edges of the sole in
order to remove crusted mud from it. Hence, you see, my double
deduction that you had been out in vile weather, and that you had a
particularly malignant boot-slitting specimen of the London slavey. As
to your practice, if a gentleman walks into my rooms smelling of
iodoform, with a black mark of nitrate of silver upon his right
forefinger, and a bulge on the right side of his top-hat to show where
he has secreted his stethoscope, I must be dull, indeed, if I do not
pronounce him to be an active member of the medical profession.
I could not help laughing at the ease with which he explained his
process of deduction. When I hear you give your reasons, I remarked,
the thing always appears to me to be so ridiculously simple that I
could easily do it myself, though at each successive instance of your
reasoning I am baffled until you explain your process. And yet I
believe that my eyes are as good as yours.
Quite so, he answered, lighting a cigarette, and throwing himself
down into an armchair. You see, but you do not observe. The
distinction is clear. For example, you have frequently seen the steps
which lead up from the hall to this room.
Frequently.
How often?
Well, some hundreds of times.
Then how many are there?
How many? I dont know.
Quite so! You have not observed. And yet you have seen. That is just
my point. Now, I know that there are seventeen steps, because I have
both seen and observed. By the way, since you are interested in these
little problems, and since you are good enough to chronicle one or two
of my trifling experiences, you may be interested in this. He threw
over a sheet of thick, pink-tinted notepaper which had been lying open
upon the table. It came by the last post, said he. Read it aloud.
The note was undated, and without either signature or address.
There will call upon you to-night, at a quarter to eight oclock, it
said, a gentleman who desires to consult you upon a matter of the very
deepest moment. Your recent services to one of the royal houses of
Europe have shown that you are one who may safely be trusted with
matters which are of an importance which can hardly be exaggerated.
This account of you we have from all quarters received. Be in your
chamber then at that hour, and do not take it amiss if your visitor
wear a mask.
This is indeed a mystery, I remarked. What do you imagine that it
means?
stop_words_english:
immediately
importance
important
index
information
invention
itd
keys
kg
km
largely
lets
line
'll
means
mg
million
ml
mug
na
nay
necessarily
nos
noted
obtain
obtained
omitted
ord
owing
page
pages
poorly
possibly
potentially
pp
predominantly
present
previously
primarily
promptly
proud
quickly
ran
readily
ref
refs
related
research
resulted
resulting
results
run
sec
section
shed
shes
showed
shown
showns
shows
significant
significantly
similar
similarly
slightly
somethan
specifically
state
states
stop
strongly
substantially
successfully
sufficiently
suggest
thered
thereof
therere
thereto
theyd
theyre
thou
thoughh
thousand
throug
til
tip
ts
ups
usefully
usefulness
've
vol
vols
wed
whats
wheres
whim
whod
whos
widely
words
world
youd
youre
mr
mr.
mrs
mrs.
ms
ms.
Gutenberg-tm
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
Print the top 10 most frequent words in the book assigned to you by your register numbers.
The expected output is as shown below
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started