Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit ed8711a

Browse files
authored
Merge pull request #204 from namanshah01/master
Generate Wordcloud from Wikipedia Article
2 parents d4cef45 + 101ddd4 commit ed8711a

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Wordcloud Images for Wikipedia Article
2+
Python script that prompts the user for an input, searches for the corresponding article on wikipedia and generates a wordcloud based on the searched article.
3+
4+
### Prerequisites
5+
`pip install` the models in `requirements.txt` from your command prompt.
6+
7+
### How to run the script
8+
Run like any other python file. Upon executing, the wordcloud image will be saved to the current directory. The script will also prompt a y/n if the user wants to see the generated image during execution.
9+
![script execution](script_execution.jpg)
10+
11+
## *Author Name*
12+
[Naman Shah](https://github.com/namanshah01)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
beautifulsoup4==4.9.1
2+
certifi==2020.6.20
3+
chardet==3.0.4
4+
cycler==0.10.0
5+
idna==2.10
6+
kiwisolver==1.2.0
7+
matplotlib==3.3.1
8+
numpy==1.19.1
9+
Pillow==7.2.0
10+
pyparsing==2.4.7
11+
python-dateutil==2.8.1
12+
requests==2.24.0
13+
six==1.15.0
14+
soupsieve==2.0.1
15+
urllib3==1.25.10
16+
wikipedia==1.4.0
17+
wordcloud==1.8.0
115 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
2+
import matplotlib.pyplot as plt
3+
import wikipedia
4+
import sys
5+
import warnings
6+
# supressing unnecessary warnings
7+
warnings.filterwarnings("ignore")
8+
9+
10+
# function to search the wikipedia article and generate the wordcloud
11+
def gen_cloud(topic):
12+
try:
13+
content = str(wikipedia.page(topic).content)
14+
except:
15+
print("Error, try searching something else...")
16+
sys.exit()
17+
STOPWORDS.add('==')
18+
stopwords = set(STOPWORDS)
19+
wordcloud = WordCloud(stopwords=stopwords, max_words=200, background_color="black", width=600, height=350).generate(content)
20+
return wordcloud
21+
22+
23+
# function to save the wordcloud to current directory
24+
def save_cloud(wordcloud):
25+
wordcloud.to_file("./wordcloud.png")
26+
27+
28+
# function to display the wordcloud with matplotlib
29+
def show_cloud(wordcloud):
30+
plt.imshow(wordcloud, interpolation='bilinear')
31+
plt.axis("off")
32+
plt.show()
33+
34+
35+
# driver code
36+
if __name__ == '__main__':
37+
topic = input("What do you want to search: ").strip()
38+
wordcloud = gen_cloud(topic)
39+
save_cloud(wordcloud)
40+
print("Wordcloud saved to current directory as wordcloud.png")
41+
desc = input("Do you wish to see the output(y/n): ")
42+
if desc == 'y':
43+
show_cloud(wordcloud)
44+
sys.exit()
142 KB
Loading

0 commit comments

Comments
 (0)