aliicemill's picture
Upload 2 files
ffd01b6 verified
raw
history blame
2.65 kB
# Importing Libraries
# author:@aliicemill
import nltk
from PIL import Image
import matplotlib.pyplot as plt
import streamlit as st
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string
import stylecloud
# Downloading word clusters
nltk.download('stopwords')
nltk.download('punkt')
def preprocess_and_create_stylecloud(file_path, output_name='stylecloud.png',
icon_name='fas fa-laptop', lang='english'):
# read file
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
# represent stopwords
stop_words = set(stopwords.words(lang))
# Remove punctuation marks
translator = str.maketrans('', '', string.punctuation)
text = text.translate(translator)
# Tokenize the text and convert to lowercase
tokens = word_tokenize(text.lower(), language=lang)
# Filter out stopwords
filtered_tokens = [word for word in tokens if word not in stop_words]
# Join filtered tokens
processed_text = ' '.join(filtered_tokens)
# Create StyleCloud
stylecloud.gen_stylecloud(text=processed_text,
icon_name=icon_name,
output_name=output_name)
# Show the generated StyleCloud
im = Image.open(output_name)
plt.figure(figsize=(10, 10))
plt.imshow(im)
plt.axis('off') # Hide axes
plt.show()
def create_stylecloud(text, language, icon):
output_file = "stylecloud.png"
stylecloud.gen_stylecloud(text=text,
icon_name=icon,
output_name=output_file)
return output_file
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","fr","de"])
with open(file = "style.txt",mode="r",encoding="utf-8") as file :
icon_options = ",".split(file.read())
icon_options = list(map(lambda x : "fas "+x ,icon_options))
icon = st.selectbox("Icon Selection", icon_options, index=1)
if st.button("Create"):
output_file = create_stylecloud(text, language, icon)
st.success(f"Here is your file: {output_file}")
image = Image.open(output_file)
st.image(image, caption='WordCloud', use_column_width=True)
with open(output_file, "rb") as file:
btn = st.download_button(
label="Download WordCloud",
data=file,
file_name=output_file,
mime="image/png"
)