Spaces:
Sleeping
Sleeping
#Gerekli kütüphaneleri içe aktar | |
import nltk | |
from nltk.corpus import stopwords | |
from nltk.tokenize import word_tokenize | |
import string | |
import stylecloud | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
import streamlit as st | |
import os | |
import base64 | |
import os | |
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
STATIC_DIR = os.path.join(BASE_DIR, 'static') | |
# NLTK kütüphanesinden gerekli bileşenleri indir | |
nltk.download('stopwords') | |
nltk.download('punkt') | |
def preprocess_and_create_stylecloud(file_path, output_name='stylecloud.png', | |
icon_name='fas fa-laptop', lang='english'): | |
# Metni dosyadan oku | |
with open(file_path, 'r', encoding='utf-8') as f: | |
text = f.read() | |
# Dil için stopwords listesini yükle | |
stop_words = set(stopwords.words(lang)) | |
# Noktalama işaretlerini kaldır | |
translator = str.maketrans('', '', string.punctuation) | |
text = text.translate(translator) | |
# Metni tokenlere ayır ve küçük harfe çevir | |
tokens = word_tokenize(text.lower(), language=lang) | |
# Stopwords'ü filtrele | |
filtered_tokens = [word for word in tokens if word not in stop_words] | |
# Filtrelenmiş tokenleri birleştir | |
processed_text = ' '.join(filtered_tokens) | |
# StyleCloud oluştur | |
stylecloud.gen_stylecloud(text=processed_text, | |
icon_name=icon_name, | |
output_name=output_name) | |
# Oluşturulan StyleCloud'u göster | |
im = Image.open(output_name) | |
plt.figure(figsize=(10, 10)) | |
plt.imshow(im) | |
plt.axis('off') # Eksenleri gizle | |
plt.show() | |
def create_stylecloud(text, language, icon): | |
output_filename = "stylecloud.png" | |
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static") | |
# Kontrol et, yoksa oluştur | |
if not os.path.exists(static_dir): | |
os.makedirs(static_dir) | |
output_file = os.path.join(static_dir, output_filename) | |
stylecloud.gen_stylecloud(text=text, icon_name=icon, output_name=output_file) | |
# Dosyayı Base64 olarak kodla | |
with open(output_file, "rb") as file: | |
data = file.read() | |
encoded = base64.b64encode(data).decode() | |
return encoded | |
st.title("WordCloud Creator") | |
file = st.file_uploader("Import txt file", type=["txt"]) | |
if file is not None: | |
text = file.getvalue().decode("utf-8") | |
language = st.radio("Language", ["tr", "en"]) | |
icon_options = [ | |
("fas fa-car", "Araba"), | |
("fas fa-star-and-crescent", "Ay Yıldız"), | |
("fas fa-star", "Yıldız"), | |
("fas fa-trophy", "Kupa"), | |
("fas fa-heart", "Kalp"), | |
("fas fa-wifi", "WiFi"), | |
("fas fa-laptop", "Dizüstü Bilgisayar"), | |
("fas fa-coffee", "Kahve"), | |
("fas fa-radio", "Radyo"), | |
("fas fa-snowflake", "Kar Tanesi"), | |
("fas fa-apple-alt", "Elma"), | |
("fas fa-bell", "Zil"), | |
("fas fa-bicycle", "Bisiklet"), | |
("fas fa-bolt", "Yıldırım"), | |
("fas fa-book", "Kitap"), | |
("fas fa-bug", "Böcek"), | |
("fas fa-camera", "Kamera"), | |
("fas fa-crown", "Taç"), | |
("fas fa-futbol", "Futbol"), | |
("fas fa-gift", "Hediye") | |
] | |
icon_labels = [label for _, label in icon_options] | |
icon_selection = st.selectbox("İkon Seçimi", icon_labels, index=1) | |
icon = [icon for icon, label in icon_options if label == icon_selection][0] | |
if st.button("Create"): | |
encoded_image = create_stylecloud(text, language, icon) | |
st.markdown( | |
f""" | |
<a href="data:application/octet-stream;base64,{encoded_image}" download="stylecloud.png"> | |
<button>Download WordCloud</button> | |
</a> | |
""", | |
unsafe_allow_html=True | |
) | |
image = Image.open("static/stylecloud.png") | |
st.image(image, caption='WordCloud', use_column_width=True) | |