Roberta2024 commited on
Commit
260393f
·
verified ·
1 Parent(s): 3cab341

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -1,25 +1,32 @@
1
  # -*- coding: utf-8 -*-
2
  """keyword_extraction"""
3
 
4
- # 1. 安裝必要的套件
5
- #!pip install jieba
6
- #!pip install keybert
7
- #!pip install streamlit
8
- #!pip install matplotlib
9
-
10
  import jieba
11
  from keybert import KeyBERT
12
  from sklearn.feature_extraction.text import CountVectorizer
13
  import streamlit as st
14
  import matplotlib.pyplot as plt
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- !wget -O TaipeiSansTCBeta-Regular.ttf https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download
17
- import matplotlib as mpl
18
- import matplotlib.font_manager as fm
19
- #字型
20
- fm.fontManager.addfont('TaipeiSansTCBeta-Regular.ttf')
21
- mpl.rc('font', family='Taipei Sans TC Beta')
22
- [f.name for f in fm.fontManager.ttflist]
 
23
 
24
  # 2. 定義斷詞函數
25
  def jieba_tokenizer(text):
@@ -41,9 +48,11 @@ def plot_keywords(keywords, title):
41
 
42
  plt.figure(figsize=(10, 6))
43
  plt.barh(words, scores, color='skyblue')
44
- plt.xlabel('Score')
45
- plt.title(title)
46
  plt.gca().invert_yaxis() # 反轉Y軸,使得分數最高的關鍵詞在最上面
 
 
47
  st.pyplot(plt)
48
 
49
  # 6. 建立Streamlit網頁應用程式
@@ -69,7 +78,3 @@ if st.button("提取關鍵詞"):
69
  plot_keywords(keywords_multilingual, "多語言模型關鍵詞提取結果")
70
  else:
71
  st.write("請輸入文章內容以進行關鍵詞提取。")
72
-
73
- # 7. 運行Streamlit應用程式
74
- # 在終端或命令提示字元中輸入以下命令來運行應用程式:
75
- # streamlit run your_script_name.py
 
1
  # -*- coding: utf-8 -*-
2
  """keyword_extraction"""
3
 
4
+ import requests
 
 
 
 
 
5
  import jieba
6
  from keybert import KeyBERT
7
  from sklearn.feature_extraction.text import CountVectorizer
8
  import streamlit as st
9
  import matplotlib.pyplot as plt
10
+ from matplotlib.font_manager import FontProperties
11
+
12
+ # 下載字體
13
+ def download_font(url, save_path):
14
+ response = requests.get(url)
15
+ with open(save_path, 'wb') as f:
16
+ f.write(response.content)
17
+
18
+ # 字體URL和保存路徑
19
+ font_url = 'https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download'
20
+ font_path = 'TaipeiSansTCBeta-Regular.ttf'
21
 
22
+ # 下載字體
23
+ download_font(font_url, font_path)
24
+
25
+ # 置字體
26
+ font_prop = FontProperties(fname=font_path)
27
+
28
+ # 讀取繁體中文詞典
29
+ # jieba.set_dictionary('path_to_your_dict.txt') # 繁體中文詞典的實際路徑,若需要繁體字典請取消註解並設置正確路徑
30
 
31
  # 2. 定義斷詞函數
32
  def jieba_tokenizer(text):
 
48
 
49
  plt.figure(figsize=(10, 6))
50
  plt.barh(words, scores, color='skyblue')
51
+ plt.xlabel('分數', fontproperties=font_prop)
52
+ plt.title(title, fontproperties=font_prop)
53
  plt.gca().invert_yaxis() # 反轉Y軸,使得分數最高的關鍵詞在最上面
54
+ plt.xticks(fontproperties=font_prop)
55
+ plt.yticks(fontproperties=font_prop)
56
  st.pyplot(plt)
57
 
58
  # 6. 建立Streamlit網頁應用程式
 
78
  plot_keywords(keywords_multilingual, "多語言模型關鍵詞提取結果")
79
  else:
80
  st.write("請輸入文章內容以進行關鍵詞提取。")