Skip to main content

Counting words in a text with python



"""Let's count words in a text.
We got a text like this:"""



message = "How it happened that Mastro Cherry, carpenter, found a piece of wood that wept and laughed like a child. Centuries ago there lived-- \"A king!\" my little readers will say immediately. No, children, you are mistaken. Once upon a time there was a piece of wood. It was not an expensive piece of wood. Far from it. Just a common block of firewood, one of those thick, solid logs that are put on the fire in winter to make cold rooms cozy and warm. I do not know how this really happened, yet the fact remains that one fine day this piece of wood found itself in the shop of an old carpenter. His real name was Mastro Antonio, but everyone called him Mastro Cherry, for the tip of his nose was so round and red and shiny that it looked like a ripe cherry. As soon as he saw that piece of wood, Mastro Cherry was filled with joy. Rubbing his hands together happily, he mumbled half to himself: \"This has come in the nick of time. I shall use it to make the leg of a table.\"He grasped the hatchet quickly to peel off the bark and shape the wood. But as he was about to give it the first blow, he stood still with arm uplifted, for he had heard a wee, little voice say in a beseeching tone: \"Please be careful! Do not hit me so hard!\"What a look of surprise shone on Mastro Cherry's face! His funny face became still funnier."

# Let's put all the words in a list

wordlist = message.split()

# Now let's create a dictonary

count = {}

# Now let's count how many words are there in the wordlist list.

for word in message:
    count.setdefault(word, 0)

    count[word] = count[word] + 1

""" For every word: when we get the word we create the default value 0 for this keyword and we add a +1 to the value each time we find the key. Remember that we can't have keywords in a dictonary with the same name... so..."""

def decreasing():
ol = []
for words in count:
# avoid to put the 12 after 1 and 21 after 2...
if count[words]<10:
ol.append("0" + str(count[words]) + " " + words.lower())
else:
ol.append(str(count[words]) + " " + words.lower())
ol.sort()
for words in ol:

print(words)

decreasing()

"""
This function:
1) creates a list (ol)
2) for every word stored in "count" dictonary
3) if the word count is less than 10 it will append a "0" (zero) to the number of time the word is repeated in the text, so that it becomes 01, 02... and then it adds the word  putting it as a new item of the list "ol"
4) If the number is greater than 10 (two digits) it joins the number of repetitions and the word putting it as a new item of the list "ol"
5) The list is sorted
6) the list is printed
"""


# The complete code ----- (plus another function to print the words in alphabetical order ---

message = "How it happened that Mastro Cherry, carpenter, found a piece of wood that wept and laughed like a child. Centuries ago there lived-- \"A king!\" my little readers will say immediately. No, children, you are mistaken. Once upon a time there was a piece of wood. It was not an expensive piece of wood. Far from it. Just a common block of firewood, one of those thick, solid logs that are put on the fire in winter to make cold rooms cozy and warm. I do not know how this really happened, yet the fact remains that one fine day this piece of wood found itself in the shop of an old carpenter. His real name was Mastro Antonio, but everyone called him Mastro Cherry, for the tip of his nose was so round and red and shiny that it looked like a ripe cherry. As soon as he saw that piece of wood, Mastro Cherry was filled with joy. Rubbing his hands together happily, he mumbled half to himself: \"This has come in the nick of time. I shall use it to make the leg of a table.\"He grasped the hatchet quickly to peel off the bark and shape the wood. But as he was about to give it the first blow, he stood still with arm uplifted, for he had heard a wee, little voice say in a beseeching tone: \"Please be careful! Do not hit me so hard!\"What a look of surprise shone on Mastro Cherry's face! His funny face became still funnier."

message2 = str(message)

count = {} # we create an empty dictonary

message = message.split()


for word in message:
    count.setdefault(word, 0)
    count[word] = count[word] + 1

def orderedList():
ol = []
for words in count:
ol.append(words)
ol.sort()
for words in ol:
print(count[words],words)

def decreasing():
ol = []
for words in count:
# avoid to put the 12 after 1 and 21 after 2...
if count[words]<10:
ol.append("0" + str(count[words]) + " " + words.lower())
else:
ol.append(str(count[words]) + " " + words.lower())
ol.sort()
for words in ol:
print(words)

# Print the list in alphabetic order
#orderedList()

# Print list from more to less used
decreasing()

------------------- The output (it is not perfect... there should be some code adjust the text ----

01 "a
01 "please
01 "this
01 about
01 ago
01 antonio,
01 arm
01 as
01 bark
01 be
01 became
01 beseeching
01 block
01 blow,
01 but
01 but
01 called
01 careful!
01 carpenter,
01 carpenter.
01 centuries
01 cherry
01 cherry's
01 cherry.
01 child.
01 children,
01 cold
01 come
01 common
01 cozy
01 day
01 do
01 do
01 everyone
01 expensive
01 face
01 face!
01 fact
01 far
01 filled
01 fine
01 fire
01 firewood,
01 first
01 from
01 funnier.
01 funny
01 give
01 grasped
01 had
01 half
01 hands
01 happened
01 happened,
01 happily,
01 hard!"what
01 has
01 hatchet
01 heard
01 him
01 himself:
01 hit
01 how
01 how
01 immediately.
01 it
01 it.
01 itself
01 joy.
01 just
01 king!"
01 know
01 laughed
01 leg
01 lived--
01 logs
01 look
01 looked
01 me
01 mistaken.
01 mumbled
01 my
01 name
01 nick
01 no,
01 nose
01 off
01 old
01 once
01 peel
01 put
01 quickly
01 readers
01 real
01 really
01 red
01 remains
01 ripe
01 rooms
01 round
01 rubbing
01 saw
01 shall
01 shape
01 shiny
01 shone
01 shop
01 solid
01 soon
01 stood
01 surprise
01 table."he
01 thick,
01 those
01 time
01 time.
01 tip
01 together
01 tone:
01 uplifted,
01 upon
01 use
01 voice
01 warm.
01 wee,
01 wept
01 will
01 winter
01 wood,
01 yet
01 you
02 an
02 are
02 as
02 cherry,
02 for
02 found
02 his
02 his
02 i
02 like
02 little
02 make
02 on
02 one
02 say
02 so
02 still
02 there
02 this
02 with
02 wood
03 not
03 wood.
04 in
04 it
05 and
05 he
05 mastro
05 piece
05 to
06 that
06 was
10 a
10 the
12 of

Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.