halimbahae commited on
Commit
8549fcb
·
verified ·
1 Parent(s): b86c850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -52,8 +52,6 @@ def main():
52
  st.sidebar.subheader("Actions")
53
  if st.sidebar.button("Home"):
54
  display_home()
55
- if st.sidebar.button("Word Cloud"):
56
- display_word_cloud()
57
 
58
  st.sidebar.title("Books")
59
  selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()))
@@ -87,38 +85,35 @@ def display_table(csv_df):
87
  )
88
  st.dataframe(styled_df)
89
 
90
- def display_word_cloud():
91
- st.title("Word Cloud")
 
 
 
92
  st.write("Select the number of words to display and exclude words.")
93
 
94
- selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()), key="word_count_book_select")
95
- selected_files = book_files[selected_book]
96
- selected_file = st.sidebar.selectbox("Select a File", selected_files, key="word_count_file_select")
97
-
98
- file_url = f"https://raw.githubusercontent.com/halimbahae/Hadith/main/{selected_book}/{selected_file}"
99
- csv_df = pd.read_csv(file_url, header=None)
100
-
101
- if csv_df is not None:
102
- word_counts = {}
103
- exclude_words = st.sidebar.text_input("Exclude Words (comma-separated)", "", key="exclude_words_input")
104
- exclude_words = [word.strip() for word in exclude_words.split(",")]
105
-
106
- for row in csv_df.itertuples(index=False):
107
- for word in str(row[1]).split():
108
- if word not in exclude_words:
109
- if word in word_counts:
110
- word_counts[word] += 1
111
- else:
112
- word_counts[word] = 1
113
-
114
- num_words = st.slider("Select Number of Words", min_value=10, max_value=50, value=10, key="word_count_slider")
115
- top_words = sorted(word_counts, key=word_counts.get, reverse=True)[:num_words]
116
-
117
- wordcloud_data = {'word': top_words, 'count': [word_counts[word] for word in top_words]}
118
- wordcloud_df = pd.DataFrame(wordcloud_data)
119
-
120
- fig = px.bar(wordcloud_df, x='word', y='count', title="Word Cloud")
121
- st.plotly_chart(fig)
122
 
123
  if __name__ == "__main__":
124
  main()
 
52
  st.sidebar.subheader("Actions")
53
  if st.sidebar.button("Home"):
54
  display_home()
 
 
55
 
56
  st.sidebar.title("Books")
57
  selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()))
 
85
  )
86
  st.dataframe(styled_df)
87
 
88
+ if st.button("Word Count"):
89
+ display_word_count(filtered_df)
90
+
91
+ def display_word_count(df):
92
+ st.title("Word Count")
93
  st.write("Select the number of words to display and exclude words.")
94
 
95
+ word_count = st.slider("Select Number of Words", min_value=10, max_value=50, value=10, key="word_count_slider")
96
+
97
+ words_to_exclude = st.text_input("Words to Exclude (comma-separated)", "", key="exclude_words_input")
98
+ words_to_exclude = [word.strip() for word in words_to_exclude.split(",")]
99
+
100
+ word_counts = {}
101
+ for row in df.itertuples(index=False):
102
+ for word in str(row[1]).split():
103
+ if word not in words_to_exclude:
104
+ if word in word_counts:
105
+ word_counts[word] += 1
106
+ else:
107
+ word_counts[word] = 1
108
+
109
+ sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:word_count]
110
+ words, counts = zip(*sorted_word_counts)
111
+
112
+ wordcloud_data = {'word': words, 'count': counts}
113
+ wordcloud_df = pd.DataFrame(wordcloud_data)
114
+
115
+ fig = px.bar(wordcloud_df, x='word', y='count', title="Word Cloud")
116
+ st.plotly_chart(fig)
 
 
 
 
 
 
117
 
118
  if __name__ == "__main__":
119
  main()