এই সমস্যায় একটি ফাইল আছে কিছু টেক্সটের সাথে। আমাদের সেই টেক্সটগুলো থেকে ওয়ার্ড ক্লাউড (Word Cloud) তৈরি করতে হবে এবং একটি মাস্কিং ইমেজ (masking image) ব্যবহার করতে হবে। প্রোগ্রামটি ওয়ার্ড ক্লাউড ইমেজকে png ফরম্যাটে স্টোর করবে।
এই সমস্যা ইমপ্লিমেন্ট করতে পাইথনের কিছু লাইব্রেরি ব্যবহার করতে হবে। লাইব্রেরিগুলো হলো matplotlib, wordcloud, numpy, tkinter এবং PIL।
এই লাইব্রেরিগুলো ইনস্টল করতে নিচের কমান্ডগুলো ফলো করতে হবে:
লাইব্রেরি সেটআপ
$ sudo pip3 install matplotlib $ sudo pip3 install wordcloud $ sudo apt-get install python3-tk
লাইব্রেরিগুলো অ্যাড করার পর আমরা পাইথন কোড লিখতে পারি কাজটি করার জন্য।
অ্যালগরিদম
ধাপ ১: ফাইল থেকে ডেটা পড়ুন এবং 'dataset'-এ স্টোর করুন। ধাপ ২: মাস্ক ইমেজ থেকে পিক্সেল অ্যারে তৈরি করুন। ধাপ ৩: ডেটাসেট থেকে ওয়ার্ড ক্লাউড তৈরি করুন। ব্যাকগ্রাউন্ড কালার, মাস্ক, এবং স্টপ-ওয়ার্ডস সেট করুন। ধাপ ৪: ফাইনাল ইমেজ ডিস্কে স্টোর করুন।
ইনপুট: sampleWords.txt ফাইল
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still holds a vital role in directing its progress.
আরেকটি ইনপুট হলো মাস্ক ইমেজ (cloud.png)। ফাইনাল রেজাল্ট ডান পাশে।
উদাহরণ কোড
import matplotlib.pyplot as pPlot from wordcloud import WordCloud, STOPWORDS import numpy as npy from PIL import Image dataset = open("sampleWords.txt", "r").read() defcreate_word_cloud(string): maskArray = npy.array(Image.open("cloud.png")) cloud = WordCloud(background_color = "white", max_words = 200, mask = maskArray, stopwords = set(STOPWORDS)) cloud.generate(string) cloud.to_file("wordCloud.png") dataset = dataset.lower() create_word_cloud(dataset) আউটপুট