Skip to content

Instantly share code, notes, and snippets.

@Cilyan
Created March 27, 2015 00:55
Show Gist options
  • Save Cilyan/298ffe101398dade205d to your computer and use it in GitHub Desktop.
Save Cilyan/298ffe101398dade205d to your computer and use it in GitHub Desktop.
Count words given in a list inside sentences organised in another list. In reply to https://stackoverflow.com/questions/29290959/how-to-return-the-word-count-given-one-list-of-strings-and-one-list-of-lists
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import timeit
# http://www.gutenberg.org/cache/epub/1597/pg1597.txt
# Public Domain
text = """
Many years ago, there was an Emperor, who was so excessively fond of
new clothes, that he spent all his money in dress. He did not trouble
himself in the least about his soldiers; nor did he care to go either to
the theatre or the chase, except for the opportunities then afforded him
for displaying his new clothes. He had a different suit for each hour of
the day; and as of any other king or emperor, one is accustomed to say,
"he is sitting in council," it was always said of him, "The Emperor is
sitting in his wardrobe."
Time passed merrily in the large town which was his capital; strangers
arrived every day at the court. One day, two rogues, calling themselves
weavers, made their appearance. They gave out that they knew how to
weave stuffs of the most beautiful colors and elaborate patterns, the
clothes manufactured from which should have the wonderful property of
remaining invisible to everyone who was unfit for the office he held, or
who was extraordinarily simple in character.
"These must, indeed, be splendid clothes!" thought the Emperor. "Had I
such a suit, I might at once find out what men in my realms are unfit
for their office, and also be able to distinguish the wise from the
foolish! This stuff must be woven for me immediately." And he caused
large sums of money to be given to both the weavers in order that they
might begin their work directly.
So the two pretended weavers set up two looms, and affected to work very
busily, though in reality they did nothing at all. They asked for the
most delicate silk and the purest gold thread; put both into their own
knapsacks; and then continued their pretended work at the empty looms
until late at night.
"I should like to know how the weavers are getting on with my cloth,"
said the Emperor to himself, after some little time had elapsed; he was,
however, rather embarrassed, when he remembered that a simpleton, or
one unfit for his office, would be unable to see the manufacture. To be
sure, he thought he had nothing to risk in his own person; but yet, he
would prefer sending somebody else, to bring him intelligence about the
weavers, and their work, before he troubled himself in the affair. All
the people throughout the city had heard of the wonderful property the
cloth was to possess; and all were anxious to learn how wise, or how
ignorant, their neighbors might prove to be.
"I will send my faithful old minister to the weavers," said the Emperor
at last, after some deliberation, "he will be best able to see how the
cloth looks; for he is a man of sense, and no one can be more suitable
for his office than he is."
So the faithful old minister went into the hall, where the knaves were
working with all their might, at their empty looms. "What can be the
meaning of this?" thought the old man, opening his eyes very wide. "I
cannot discover the least bit of thread on the looms." However, he did
not express his thoughts aloud.
The impostors requested him very courteously to be so good as to come
nearer their looms; and then asked him whether the design pleased
him, and whether the colors were not very beautiful; at the same time
pointing to the empty frames. The poor old minister looked and looked,
he could not discover anything on the looms, for a very good reason,
viz: there was nothing there. "What!" thought he again. "Is it possible
that I am a simpleton? I have never thought so myself; and no one must
know it now if I am so. Can it be, that I am unfit for my office? No,
that must not be said either. I will never confess that I could not see
the stuff."
"Well, Sir Minister!" said one of the knaves, still pretending to work.
"You do not say whether the stuff pleases you."
"Oh, it is excellent!" replied the old minister, looking at the loom
through his spectacles. "This pattern, and the colors, yes, I will tell
the Emperor without delay, how very beautiful I think them."
"We shall be much obliged to you," said the impostors, and then they
named the different colors and described the pattern of the pretended
stuff. The old minister listened attentively to their words, in order
that he might repeat them to the Emperor; and then the knaves asked for
more silk and gold, saying that it was necessary to complete what
they had begun. However, they put all that was given them into their
knapsacks; and continued to work with as much apparent diligence as
before at their empty looms.
The Emperor now sent another officer of his court to see how the men
were getting on, and to ascertain whether the cloth would soon be
ready. It was just the same with this gentleman as with the minister;
he surveyed the looms on all sides, but could see nothing at all but the
empty frames.
"Does not the stuff appear as beautiful to you, as it did to my lord the
minister?" asked the impostors of the Emperor's second ambassador; at
the same time making the same gestures as before, and talking of the
design and colors which were not there.
"I certainly am not stupid!" thought the messenger. "It must be, that I
am not fit for my good, profitable office! That is very odd; however, no
one shall know anything about it." And accordingly he praised the stuff
he could not see, and declared that he was delighted with both colors
and patterns. "Indeed, please your Imperial Majesty," said he to his
sovereign when he returned, "the cloth which the weavers are preparing
is extraordinarily magnificent."
The whole city was talking of the splendid cloth which the Emperor had
ordered to be woven at his own expense.
And now the Emperor himself wished to see the costly manufacture, while
it was still in the loom. Accompanied by a select number of officers of
the court, among whom were the two honest men who had already admired
the cloth, he went to the crafty impostors, who, as soon as they were
aware of the Emperor's approach, went on working more diligently than
ever; although they still did not pass a single thread through the
looms.
"Is not the work absolutely magnificent?" said the two officers of the
crown, already mentioned. "If your Majesty will only be pleased to look
at it! What a splendid design! What glorious colors!" and at the same
time they pointed to the empty frames; for they imagined that everyone
else could see this exquisite piece of workmanship.
"How is this?" said the Emperor to himself. "I can see nothing! This
is indeed a terrible affair! Am I a simpleton, or am I unfit to be an
Emperor? That would be the worst thing that could happen--Oh! the cloth
is charming," said he, aloud. "It has my complete approbation." And he
smiled most graciously, and looked closely at the empty looms; for on no
account would he say that he could not see what two of the officers of
his court had praised so much. All his retinue now strained their eyes,
hoping to discover something on the looms, but they could see no more
than the others; nevertheless, they all exclaimed, "Oh, how beautiful!"
and advised his majesty to have some new clothes made from this splendid
material, for the approaching procession. "Magnificent! Charming!
Excellent!" resounded on all sides; and everyone was uncommonly gay. The
Emperor shared in the general satisfaction; and presented the impostors
with the riband of an order of knighthood, to be worn in their
button-holes, and the title of "Gentlemen Weavers."
The rogues sat up the whole of the night before the day on which the
procession was to take place, and had sixteen lights burning, so that
everyone might see how anxious they were to finish the Emperor's new
suit. They pretended to roll the cloth off the looms; cut the air with
their scissors; and sewed with needles without any thread in them.
"See!" cried they, at last. "The Emperor's new clothes are ready!"
And now the Emperor, with all the grandees of his court, came to the
weavers; and the rogues raised their arms, as if in the act of holding
something up, saying, "Here are your Majesty's trousers! Here is the
scarf! Here is the mantle! The whole suit is as light as a cobweb;
one might fancy one has nothing at all on, when dressed in it; that,
however, is the great virtue of this delicate cloth."
"Yes indeed!" said all the courtiers, although not one of them could see
anything of this exquisite manufacture.
"If your Imperial Majesty will be graciously pleased to take off your
clothes, we will fit on the new suit, in front of the looking glass."
The Emperor was accordingly undressed, and the rogues pretended to
array him in his new suit; the Emperor turning round, from side to side,
before the looking glass.
"How splendid his Majesty looks in his new clothes, and how well they
fit!" everyone cried out. "What a design! What colors! These are indeed
royal robes!"
"The canopy which is to be borne over your Majesty, in the procession,
is waiting," announced the chief master of the ceremonies.
"I am quite ready," answered the Emperor. "Do my new clothes fit well?"
asked he, turning himself round again before the looking glass, in order
that he might appear to be examining his handsome suit.
The lords of the bedchamber, who were to carry his Majesty's train felt
about on the ground, as if they were lifting up the ends of the mantle;
and pretended to be carrying something; for they would by no means
betray anything like simplicity, or unfitness for their office.
So now the Emperor walked under his high canopy in the midst of the
procession, through the streets of his capital; and all the people
standing by, and those at the windows, cried out, "Oh! How beautiful
are our Emperor's new clothes! What a magnificent train there is to
the mantle; and how gracefully the scarf hangs!" in short, no one would
allow that he could not see these much-admired clothes; because, in
doing so, he would have declared himself either a simpleton or unfit
for his office. Certainly, none of the Emperor's various suits, had ever
made so great an impression, as these invisible ones.
"But the Emperor has nothing at all on!" said a little child.
"Listen to the voice of innocence!" exclaimed his father; and what the
child had said was whispered from one to another.
"But he has nothing at all on!" at last cried out all the people.
The Emperor was vexed, for he knew that the people were right; but he
thought the procession must go on now! And the lords of the bedchamber
took greater pains than ever, to appear holding up a train, although, in
reality, there was no train to hold.
"""
# Get paragraphs, then sentences (no ...)
list_1 = [ par.split(".") for par in text.split("\n\n") ]
list_2 = (
"time large his day two appearance how most beautiful clothes from which "
"property everyone office extraordinarily simple years Emperor money "
"theatre then always new rogues night"
).split()
#list_1 = [["the guy was plaguy but unable to play football, but he was able to play tennis"],["That was absolute cool"],["This is an implicit living."]]
#list_2 = ['unable', 'unquestioning', 'implicit', 'living', 'relative', 'comparative']
def solution_re():
_regex = re.compile(r"(\b{}\b)".format(r"\b|\b".join(list_2)))
return [
sum(
sum(1 for occurence in _regex.findall(sentence))
for sentence in sublist
) for sublist in list_1
]
def solution_sum():
return [
sum(
sum(sentence.count(word) for word in list_2)
for sentence in sublist
) for sublist in list_1
]
print("solution_sum:", solution_sum())
print(timeit.timeit(
"solution_sum()",
setup="from __main__ import solution_sum",
number=1000
))
print("solution_re: ", solution_re())
print(timeit.timeit(
"solution_re()",
setup="from __main__ import solution_re",
number=1000
))
# solution_sum: [16, 20, 7, 5, 11, 4, 3, 5, 0, 5, 3, 5, 4, 5, 3, 4, 5, 20, 11, 5, 1, 2, 6, 6, 1, 4, 2, 13, 1, 3, 2]
# 0.9096970450009394
# solution_re: [16, 19, 6, 5, 9, 4, 2, 4, 0, 4, 3, 3, 4, 4, 3, 3, 3, 14, 11, 3, 0, 2, 6, 6, 1, 4, 2, 12, 1, 2, 1]
# 0.6919540519993461
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment