Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """keyword_extraction""" | |
| import requests | |
| import jieba | |
| from keybert import KeyBERT | |
| from sklearn.feature_extraction.text import CountVectorizer | |
| import streamlit as st | |
| import matplotlib.pyplot as plt | |
| from matplotlib.font_manager import FontProperties | |
| # 下載字體 | |
| def download_font(url, save_path): | |
| response = requests.get(url) | |
| with open(save_path, 'wb') as f: | |
| f.write(response.content) | |
| # 字體URL和保存路徑 | |
| font_url = 'https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download' | |
| font_path = 'TaipeiSansTCBeta-Regular.ttf' | |
| # 下載字體 | |
| download_font(font_url, font_path) | |
| # 設置字體 | |
| font_prop = FontProperties(fname=font_path) | |
| # 讀取繁體中文詞典 | |
| # jieba.set_dictionary('path_to_your_dict.txt') # 繁體中文詞典的實際路徑,若需要繁體字典請取消註解並設置正確路徑 | |
| # 2. 定義斷詞函數 | |
| def jieba_tokenizer(text): | |
| return jieba.lcut(text) | |
| # 3. 初始化CountVectorizer並定義KeyBERT模型 | |
| vectorizer = CountVectorizer(tokenizer=jieba_tokenizer) | |
| kw_model = KeyBERT() | |
| # 4. 提取關鍵詞的函數 | |
| def extract_keywords(doc): | |
| keywords = kw_model.extract_keywords(doc, vectorizer=vectorizer) | |
| return keywords | |
| # 5. 畫圖函數 | |
| def plot_keywords(keywords, title): | |
| words = [kw[0] for kw in keywords] | |
| scores = [kw[1] for kw in keywords] | |
| plt.figure(figsize=(10, 6)) | |
| plt.barh(words, scores, color='skyblue') | |
| plt.xlabel('分數', fontproperties=font_prop) | |
| plt.title(title, fontproperties=font_prop) | |
| plt.gca().invert_yaxis() # 反轉Y軸,使得分數最高的關鍵詞在最上面 | |
| plt.xticks(fontproperties=font_prop) | |
| plt.yticks(fontproperties=font_prop) | |
| st.pyplot(plt) | |
| # 6. 建立Streamlit網頁應用程式 | |
| st.title("中文關鍵詞提取工具") | |
| doc = st.text_area("請輸入文章:") | |
| if st.button("提取關鍵詞"): | |
| if doc: | |
| keywords = extract_keywords(doc) | |
| st.write("關鍵詞提取結果:") | |
| for keyword in keywords: | |
| st.write(f"{keyword[0]}: {keyword[1]:.4f}") | |
| plot_keywords(keywords, "關鍵詞提取結果") | |
| # 使用另一個模型進行關鍵詞提取 | |
| kw_model_multilingual = KeyBERT(model='distiluse-base-multilingual-cased-v1') | |
| keywords_multilingual = kw_model_multilingual.extract_keywords(doc, vectorizer=vectorizer) | |
| st.write("多語言模型關鍵詞提取結果:") | |
| for keyword in keywords_multilingual: | |
| st.write(f"{keyword[0]}: {keyword[1]:.4f}") | |
| plot_keywords(keywords_multilingual, "多語言模型關鍵詞提取結果") | |
| else: | |
| st.write("請輸入文章內容以進行關鍵詞提取。") |