Welcoming IPL 2023 with Python

 Hello folks,

As like Dhoni addressing the CSK crowd, 'Vanakkam Chennai', VANAKKAM to all my Coding Aspirants!!

IPL Fever starts today. It was like 2023 has just began, one fourth of the year is over already. The next two months will compete Usain Bolt and run in thaaaat speed. Yes, its the treat to all Cricket lovers. a gang of friends turn into rival as CSK vs MI vs RCB vs soooo on. Hey Dhoni will rock. No, MI are the champions. Let's pray for RCB to win the cup this year. These are all the statements we hear all around. 

Along with the IPL interest, let's enrich our Coding knowledge too with little bit of Python and Data Science. Let's have a simple Word Cloud Program with the winning speeches of Namma Thala Dhoni and our very own Hitman Rohit! The heroes of IPL Winning Teams. There is a race between these two teams to win the maximum trophies. Both are heroes to millions of people in India. People from TN support MI and people all over India chant Dhoni Dhoni!! A different way of expressing Unity in Diversity!

Even my closest friends and family who are like kith and kin support MI whereas I'm a forever Yellow Girl - MSDian!! Proud MSDian. Even my sister from another mother and my bestest bestie are against CSK means they are the disciples of the great Sachin! Yes MI vs CSK. But today, it is not Chennai vs Mumbai, but CSK vs GT. Yes, I'm typing this blog seeing and admiring the players and enjoying the opening ceremony happening at Ahmedabad! 

Shall we dive into coding! Yes. Are you guys ready! 1.. 2.. 3...

A simple word cloud program with the winning speeches of the CSK and MI heart throbs Mahi and Rohit! 

import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import re

#IPL 2020(MI) and 2021(CSK) winning speeches

# Generate a word cloud for Dhoni
dhoni_wordcloud = WordCloud(width=800, height=800, background_color='white').generate(dhoni_text)

# Display the word cloud for Dhoni
plt.imshow(dhoni_wordcloud, interpolation='bilinear')
plt.title("Word Cloud for Dhoni")
plt.axis('off')
plt.show()

# Generate a word cloud for Rohit Sharma
rohit_wordcloud = WordCloud(width=800, height=800, background_color='white').generate(rohit_text)

# Display the word cloud for Rohit Sharma
plt.imshow(rohit_wordcloud, interpolation='bilinear')
plt.title("Word Cloud for Rohit Sharma")
plt.axis('off')
plt.show()

Let me give a simple explanation for the above coding.

We import the following libraries:

  • requests: This library is used to send HTTP requests to websites and retrieve HTML content.
  • BeautifulSoup: This library is used to parse HTML content and extract data from it.
  • WordCloud: This library is used to generate word clouds from text data.
  • matplotlib: This library is used to visualize data and create plots.
  • re: This library is used for regular expressions.
We define the text that we want to use for generating the word clouds. We have used the winning speeches of MS in the finals of 2021 IPL and Rohit in the year 2020. Let's see this year who is gonna win the trophy as well as the hearts of the people!

We have generated a word cloud for Dhoni using the WordCloud class from the wordcloud library. We pass the following arguments to the WordCloud class - width, height and the background_color.
We then generate the word cloud by calling the generate() method of the WordCloud class and passing the dhoni_text variable as the argument. The generate() method generates the word cloud based on the frequency of words in the text. 
Finally, we use the imshow() function from the matplotlib library to display the word cloud for Dhoni. We pass the following arguments to the imshow() function:
dhoni_wordcloud: The word cloud that we generated in the previous step.
interpolation: The interpolation method to use for displaying the image.
We then set the title of the plot using the title() function and disable the axis using the axis() function. Finally, we display the plot using the show() function.
The same process is followed for Rohit also.

I have added the word clouds below. People, find which is Dhoni's and which is Rohit's speech! I hope it is very easy for MSDians (Yes, a special soft corner for CSKians!)


Did you enjoy this simple program. Let's have more fun activites and coding in the upcoming days. Enjoy the night with CSK and Gujarat! Special Yellove to all my fellow coders! 💛


Comments