python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法
經(jīng)常需要通過python代碼來提取文本的關(guān)鍵詞,用于文本分析。而實(shí)際應(yīng)用中文本量又是大量的數(shù)據(jù),如果使用單進(jìn)程的話,效率會比較低,因此可以考慮使用多進(jìn)程。
python的多進(jìn)程只需要使用multiprocessing的模塊就行,如果使用大量的進(jìn)程就可以使用multiprocessing的進(jìn)程池--Pool,然后不同進(jìn)程處理時使用apply_async函數(shù)進(jìn)行異步處理即可。
實(shí)驗(yàn)測試語料:message.txt中存放的581行文本,一共7M的數(shù)據(jù),每行提取100個關(guān)鍵詞。
代碼如下:
#coding:utf-8 import sys reload(sys) sys.setdefaultencoding("utf-8") from multiprocessing import Pool,Queue,Process import multiprocessing as mp import time,random import os import codecs import jieba.analyse jieba.analyse.set_stop_words("yy_stop_words.txt") def extract_keyword(input_string): #print("Do task by process {proc}".format(proc=os.getpid())) tags = jieba.analyse.extract_tags(input_string, topK=100) #print("key words:{kw}".format(kw=" ".join(tags))) return tags #def parallel_extract_keyword(input_string,out_file): def parallel_extract_keyword(input_string): #print("Do task by process {proc}".format(proc=os.getpid())) tags = jieba.analyse.extract_tags(input_string, topK=100) #time.sleep(random.random()) #print("key words:{kw}".format(kw=" ".join(tags))) #o_f = open(out_file,'w') #o_f.write(" ".join(tags)+"\n") return tags if __name__ == "__main__": data_file = sys.argv[1] with codecs.open(data_file) as f: lines = f.readlines() f.close() out_put = data_file.split('.')[0] +"_tags.txt" t0 = time.time() for line in lines: parallel_extract_keyword(line) #parallel_extract_keyword(line,out_put) #extract_keyword(line) print("串行處理花費(fèi)時間{t}".format(t=time.time()-t0)) pool = Pool(processes=int(mp.cpu_count()*0.7)) t1 = time.time() #for line in lines: #pool.apply_async(parallel_extract_keyword,(line,out_put)) #保存處理的結(jié)果,可以方便輸出到文件 res = pool.map(parallel_extract_keyword,lines) #print("Print keywords:") #for tag in res: #print(" ".join(tag)) pool.close() pool.join() print("并行處理花費(fèi)時間{t}s".format(t=time.time()-t1))
運(yùn)行:
python data_process_by_multiprocess.py message.txt
message.txt是每行是一個文檔,共581行,7M的數(shù)據(jù)
運(yùn)行時間:
不使用sleep來掛起進(jìn)程,也就是把time.sleep(random.random())注釋掉,運(yùn)行可以大大節(jié)省時間。
以上這篇python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
pandas 對series和dataframe進(jìn)行排序的實(shí)例
今天小編就為大家分享一篇pandas 對series和dataframe進(jìn)行排序的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程
這篇文章主要介紹了哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Python數(shù)據(jù)結(jié)構(gòu)詳細(xì)
本文將詳細(xì)講解Python的數(shù)據(jù)結(jié)構(gòu),下面我們將講解Python關(guān)于關(guān)于列表更多的內(nèi)容以及del 語句和元組和序列等一些具體內(nèi)容,需要的下伙伴可以參考一下2021-09-09Python使用PyCrypto實(shí)現(xiàn)AES加密功能示例
這篇文章主要介紹了Python使用PyCrypto實(shí)現(xiàn)AES加密功能,結(jié)合具體實(shí)例形式分析了PyCrypto實(shí)現(xiàn)AES加密的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05Python進(jìn)制轉(zhuǎn)換與反匯編實(shí)現(xiàn)流程介紹
這篇文章主要介紹了Python進(jìn)制轉(zhuǎn)換與反匯編的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10