-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordcloud.py
More file actions
135 lines (95 loc) · 3.8 KB
/
Wordcloud.py
File metadata and controls
135 lines (95 loc) · 3.8 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
# Importing the StringIO module.
from io import StringIO
from collections import Counter
import re
import altair as alt
st.write("## Analyzing shakespeare texts")
st.sidebar.header("Word Cloud Settings")
max_word = st.sidebar.slider("Max Words",10,200,100,10)
max_font = st.sidebar.slider("Size of largest word",50,350,60)
image_size = st.sidebar.slider("Image width",100,800,400,10)
random = st.sidebar.slider("Random State",30,100,42)
stop_words_bool = st.sidebar.checkbox("Remove stop words?")
st.sidebar.header("Word Count Settings")
min_Count_Word=st.sidebar.slider("Minimum count of words",5,100,40,1)
file=st.selectbox("slect your book:",("","A Mid Summers Night's Dream","The Merchant of Venice","Romeo and Juliet"))
if file == "A Mid Summers Night's Dream":
file = "data/summer.txt"
elif file == "The Merchant of Venice":
file = "data/merchant.txt"
elif file == "Romeo and Juliet":
file = "data/romeo.txt"
else:
file=None
if file is not None:
with open(file, 'r') as file:
data = file.read()
uploaded_file=file
if uploaded_file is not None:
# To convert to a string based IO:
#stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
stringio = StringIO(data)
dataset=stringio.read()
stopwords = set(STOPWORDS)
if stop_words_bool:
stopwords.update(['us', 'one', 'will', 'said', 'now', 'well', 'man', 'may',
'little', 'say', 'must', 'way', 'long', 'yet', 'mean',
'put', 'seem', 'asked', 'made', 'half', 'much',
'certainly', 'might', 'came'])
tab1, tab2, tab3 = st.tabs(["Word Cloud","Bar Chart","View Text"])
with tab1:
if uploaded_file is not None:
cloud = WordCloud(background_color = "white",
max_words = max_word,
max_font_size=max_font,
stopwords = stopwords,
random_state=random)
wc = cloud.generate(dataset)
word_cloud = cloud.to_file('wordcloud.png')
st.image(wc.to_array(),width=image_size)
display=[]
with tab2:
if uploaded_file is not None:
data2 = re.sub(r'[^\w\s]', '', data)
#data2.replace("19901993","1990 1993")
#insert the fix for copyright label here
data2=data2.lower()
stop_words = ['us', 'one', 'will', 'said', 'now', 'well', 'man', 'may',
'little', 'say', 'must', 'way', 'long', 'yet', 'mean',
'put', 'seem', 'asked', 'made', 'half', 'much',
'certainly', 'might', 'came']
#x axis is word count
#y is the word
#if stopwords is true if word in the list skip
x = sorted(Counter(data2.split()).items())
new = pd.DataFrame.from_dict(x)
new.columns=["Word","Count"]
new=new.sort_values(["Count"], ascending=False)
display = new[new["Count"]>=min_Count_Word]
if stop_words_bool:
for i in stop_words:
display.drop(display[display['Word'] == i].index, inplace = True)
#sort df by max# sort by desc
c = alt.Chart(display).mark_bar().encode(
x='Count:Q',
y=alt.Y('Word:N', sort='-x'),
tooltip=["Count"]
)
# text = c.mark_text(
# align='left',
# baseline='middle',
# dx=3 # Nudges text to right so it doesn't appear on top of the bar
# ).encode(
# text='Count:Q'
# )
st.altair_chart(c, use_container_width=True)
#st.bar_chart(data=display, x="Count",y="Word")
with tab3:
if uploaded_file is not None:
st.write(dataset)