自然語言任務經常使用jieba分詞,數據量大時怎麼加速,jieba分詞不支持使用asyncio異步加速,使用multiprocessing還是可以的
import jieba import jieba.analyse import multiprocessing # 加載自定義詞典 jieba.load_userdict("user_dic.txt") jieba.load_userdict("cate_group.txt") jieba.analyse.set_stop_words('stopwords_v1.txt') def process_text(text): # 分詞 words = jieba.cut(text, cut_all=True) # 過濾長度小於2或大於10的詞和純數字的詞 filtered_words = [w for w in words if len(w) >= 2 and len(w) <= 10 and not w.isdigit()] # 返回分詞結果 return filtered_words # 創建進程池 pool = multiprocessing.Pool() # 處理文本列表 # texts = ["這是一段測試文本", "這是另一段測試文本"] texts = data["new_text"] results = pool.map(process_text, texts) # 輸出結果 results
結果:
[['估值', '有待', '修復', '煤炭', '平均', '市盈率', '美元'], ['國產', '醫療', '醫療器械', '器械', '行業', '發展', '迅速', '作爲', '國內', '最大', '醫療', '醫療器械', '器械', '企業', '基本', '一枝', '一枝獨秀', '獨秀'], ['今日', '上海', '現貨'], ['消息', '準備'],