File size: 3,657 Bytes
f736779
a4c52ed
 
 
 
 
 
 
 
c7f4293
34c9ab5
a4c52ed
 
 
 
 
1931174
 
 
 
 
 
a4c52ed
1931174
a4c52ed
 
 
 
 
 
1931174
a4c52ed
 
 
 
 
 
 
 
1931174
 
 
 
 
 
 
 
 
 
 
9d4affc
34c9ab5
9d4affc
 
 
 
 
a4c52ed
 
 
 
 
 
 
 
 
 
 
adbab52
 
a4c52ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbab52
a4c52ed
 
 
 
 
 
 
9d4affc
 
 
 
 
 
 
 
 
1931174
9d4affc
1931174
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#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

# 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_file = "static/stylecloud.png"
        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)
        # Ensure the file is deleted after display
        if os.path.exists(output_file):
            os.remove(output_file)