soojeongcrystal commited on
Commit
35bdcd6
·
verified ·
1 Parent(s): de8d584

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import streamlit as st
2
- from wordcloud import WordCloud
3
  import matplotlib.pyplot as plt
4
  from sklearn.feature_extraction.text import CountVectorizer
5
  from sklearn.decomposition import LatentDirichletAllocation
@@ -42,12 +41,16 @@ def topic_modeling(texts, n_components):
42
  topics[f"Topic {topic_idx + 1}"] = [features[i] for i in topic.argsort()[:-21:-1]]
43
  return topics
44
 
45
- def generate_wordcloud(text, color):
46
- wordcloud = WordCloud(width=800, height=400, background_color=color,
47
- font_path='/usr/share/fonts/truetype/nanum/NanumGothic.ttf').generate(text)
48
- fig, ax = plt.subplots(figsize=(10, 5))
49
- ax.imshow(wordcloud, interpolation='bilinear')
50
- ax.axis("off")
 
 
 
 
51
  return fig
52
 
53
  def get_top_trigrams(text, n=10):
@@ -103,9 +106,9 @@ if uploaded_file is not None:
103
  for trigram, count in top_trigrams:
104
  st.write(f"{trigram}: {count}")
105
 
106
- st.subheader("워드 클라우드")
107
- color = st.color_picker("배경색 선택", "#ffffff")
108
- fig = generate_wordcloud(preprocessed_text, color)
109
  st.pyplot(fig)
110
 
111
  except Exception as e:
@@ -117,5 +120,5 @@ st.sidebar.markdown("""
117
  2. 텍스트 파일(.txt)을 업로드하세요.
118
  3. 토픽 모델링의 토픽 수를 선택하세요.
119
  4. 상위 10개 Trigram을 확인하세요.
120
- 5. 워드클라우드배경색을 선택할 수 있습니다.
121
  """)
 
1
  import streamlit as st
 
2
  import matplotlib.pyplot as plt
3
  from sklearn.feature_extraction.text import CountVectorizer
4
  from sklearn.decomposition import LatentDirichletAllocation
 
41
  topics[f"Topic {topic_idx + 1}"] = [features[i] for i in topic.argsort()[:-21:-1]]
42
  return topics
43
 
44
+ def generate_word_frequency_chart(text, color, n=20):
45
+ words = text.split()
46
+ word_freq = Counter(words)
47
+ top_words = dict(word_freq.most_common(n))
48
+
49
+ fig, ax = plt.subplots(figsize=(12, 6))
50
+ ax.barh(list(top_words.keys()), list(top_words.values()), color=color)
51
+ ax.invert_yaxis() # 가장 빈도가 높은 단어를 위쪽에 표시
52
+ ax.set_title("Top {} Words".format(n))
53
+ plt.tight_layout()
54
  return fig
55
 
56
  def get_top_trigrams(text, n=10):
 
106
  for trigram, count in top_trigrams:
107
  st.write(f"{trigram}: {count}")
108
 
109
+ st.subheader("단어 빈도 차트")
110
+ color = st.color_picker("막대 선택", "#1f77b4")
111
+ fig = generate_word_frequency_chart(preprocessed_text, color)
112
  st.pyplot(fig)
113
 
114
  except Exception as e:
 
120
  2. 텍스트 파일(.txt)을 업로드하세요.
121
  3. 토픽 모델링의 토픽 수를 선택하세요.
122
  4. 상위 10개 Trigram을 확인하세요.
123
+ 5. 단어 빈도 차트막대 을 선택할 수 있습니다.
124
  """)