soojeongcrystal commited on
Commit
9b4047d
·
verified ·
1 Parent(s): 40ab04f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -35,6 +35,24 @@ def preprocess_text(text, stop_words):
35
  # Streamlit 앱 설정
36
  st.title('한국어 토픽 모델링 앱')
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # 사이드바 설정
39
  st.sidebar.header('설정')
40
 
@@ -86,19 +104,24 @@ if uploaded_file is not None:
86
  for idx, topic in enumerate(lda.components_):
87
  st.subheader(f"토픽 {idx + 1}")
88
 
89
- # LDA 상위 단어 테이블
90
- lda_top_words = [(feature_names[i], topic[i]) for i in topic.argsort()[:-11:-1]]
91
- df_lda = pd.DataFrame(lda_top_words, columns=['단어', 'LDA 점수'])
92
- st.subheader("LDA 상위 단어")
93
- st.table(df_lda.style.format({'LDA 점수': '{:.4f}'}))
 
 
 
 
94
 
95
- # TF-IDF 상위 단어 테이블
96
- tfidf_scores = tfidf_matrix.sum(axis=0).A1
97
- tfidf_top_words = sorted([(feature_names[i], tfidf_scores[i]) for i in range(len(feature_names))],
98
- key=lambda x: x[1], reverse=True)[:10]
99
- df_tfidf = pd.DataFrame(tfidf_top_words, columns=['단어', 'TF-IDF'])
100
- st.subheader("TF-IDF 상위 단어")
101
- st.table(df_tfidf.style.format({'TF-IDF': '{:.4f}'}))
 
102
 
103
  # 토픽 비중 그래프
104
  st.header("토픽 비중 그래프")
 
35
  # Streamlit 앱 설정
36
  st.title('한국어 토픽 모델링 앱')
37
 
38
+ # CSS를 사용하여 테이블 스타일 정의
39
+ st.markdown("""
40
+ <style>
41
+ .stDataFrame {
42
+ width: 100%;
43
+ }
44
+ .stDataFrame table {
45
+ width: 100%;
46
+ }
47
+ .stDataFrame th {
48
+ font-size: 14px;
49
+ }
50
+ .stDataFrame td {
51
+ font-size: 12px;
52
+ }
53
+ </style>
54
+ """, unsafe_allow_html=True)
55
+
56
  # 사이드바 설정
57
  st.sidebar.header('설정')
58
 
 
104
  for idx, topic in enumerate(lda.components_):
105
  st.subheader(f"토픽 {idx + 1}")
106
 
107
+ # LDA 상위 단어 TF-IDF 상위 단어를 나란히 표시
108
+ col1, col2 = st.columns(2)
109
+
110
+ with col1:
111
+ # LDA 상위 단어 테이블
112
+ lda_top_words = [(feature_names[i], topic[i]) for i in topic.argsort()[:-11:-1]]
113
+ df_lda = pd.DataFrame(lda_top_words, columns=['단어', 'LDA 점수'])
114
+ st.subheader("LDA 상위 단어")
115
+ st.dataframe(df_lda.style.format({'LDA 점수': '{:.4f}'}), height=400)
116
 
117
+ with col2:
118
+ # 토픽별 TF-IDF 계산
119
+ topic_docs = lda_output[:, idx].argsort()[::-1][:100] # 상위 100개 문서 선택
120
+ topic_tfidf = tfidf_matrix[topic_docs].mean(axis=0).A1
121
+ tfidf_top_words = [(feature_names[i], topic_tfidf[i]) for i in topic_tfidf.argsort()[:-11:-1]]
122
+ df_tfidf = pd.DataFrame(tfidf_top_words, columns=['단어', 'TF-IDF'])
123
+ st.subheader("TF-IDF 상위 단어")
124
+ st.dataframe(df_tfidf.style.format({'TF-IDF': '{:.4f}'}), height=400)
125
 
126
  # 토픽 비중 그래프
127
  st.header("토픽 비중 그래프")