NLP:基于nltk和jieba库对文本实现提取文本摘要(两种方法实现:top_n_summary和mean_scored_summary)
生活随笔
收集整理的這篇文章主要介紹了
NLP:基于nltk和jieba库对文本实现提取文本摘要(两种方法实现:top_n_summary和mean_scored_summary)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
NLP:基于nltk和jieba庫對文本實現(xiàn)提取文本摘要(兩種方法實現(xiàn):top_n_summary和mean_scored_summary)
?
?
?
?
目錄
輸出結(jié)果
設(shè)計思路
核心代碼
?
?
?
?
輸出結(jié)果
1、測試文本
今天一大早,兩位男子在故宮抽煙對鏡頭炫耀的視頻在網(wǎng)絡(luò)上傳播,引發(fā)網(wǎng)友憤怒。有人感到后怕,600年的故宮真要這兩個人給點了,萬死莫贖。也有評論稱,把無知當成炫耀的資本,丟人! 視頻中兩位男子坐在故宮公共休息區(qū)的遮陽傘下,面對鏡頭問出:“誰敢在故宮抽煙?”語氣極其囂張,表情帶有挑釁意味。話音剛落,另外一位男子面向鏡頭吸了一口煙。而視頻中兩人也表示知道有故宮禁止吸煙的規(guī)定。 事實上,2013年5月18日是國際博物館日,故宮從這一天開始至今一直實行全面禁煙。根據(jù)規(guī)定,故宮博物院全體員工在院合作單位和個人不管在室內(nèi)和室外,也不分開放區(qū)與工作區(qū),一律禁止吸煙,對違反禁止吸煙規(guī)定的人員將進行嚴格處罰并通報全院。 此外,在2015年6月1日起北京全市也開始了《控制吸煙條例》,規(guī)定公共場所工作場所室內(nèi)環(huán)境室外排隊等場合禁止吸煙,違者將最高被罰200元,全市統(tǒng)一設(shè)立舉報電話12320。 視頻在網(wǎng)絡(luò)上傳播開來,不少網(wǎng)友擔心故宮的安危,稱一旦發(fā)生火情,后果不堪設(shè)想,有網(wǎng)友表示,這樣的行為應(yīng)該被旅游景區(qū)拉近黑名單,建議終身禁止進入任何景區(qū)和各種場館。?
設(shè)計思路
后期更新……
?
?
核心代碼
def sent_tokenizer(texts):start=0i=0#每個字符的位置sentences=[]punt_list='.!?。!?'.encode('utf-8') #',.!?:;~,。!?:;~'.decode('utf8')# punt_lists='.!?。!?'.decode()for text in texts:if text in punt_list and token not in punt_list: #檢查標點符號下一個字符是否還是標點sentences.append(texts[start:i+1])#當前標點符號位置start=i+1#start標記到下一句的開頭i+=1else:i+=1#若不是標點符號,則字符位置繼續(xù)前移token=list(texts[start:i+2]).pop()#取下一個字符if start<len(texts):sentences.append(texts[start:])#這是為了處理文本末尾沒有標點符號的情況return sentencesdef load_stopwordslist(path):print('load stopwords...')stoplist=[line.strip() for line in codecs.open(path,'r',encoding='utf8').readlines()]stopwrods={}.fromkeys(stoplist)return stopwrodsdef summarize(text):stopwords=load_stopwordslist('stopwords.txt')sentences=sent_tokenizer(text)words=[w for sentence in sentences for w in jieba.cut(sentence) if w not in stopwords if len(w)>1 and w!='\t']wordfre=nltk.FreqDist(words)topn_words=[w[0] for w in sorted(wordfre.items(),key=lambda d:d[1],reverse=True)][:N]scored_sentences=_score_sentences(sentences,topn_words)#approach 1,利用均值和標準差過濾非重要句子avg=numpy.mean([s[1] for s in scored_sentences])#均值std=numpy.std([s[1] for s in scored_sentences])#標準差mean_scored=[(sent_idx,score) for (sent_idx,score) in scored_sentences if score>(avg+0.5*std)]#approach 2,返回top n句子top_n_scored=sorted(scored_sentences,key=lambda s:s[1])[-TOP_SENTENCES:]top_n_scored=sorted(top_n_scored,key=lambda s:s[0])return dict(top_n_summary=[sentences[idx] for (idx,score) in top_n_scored],mean_scored_summary=[sentences[idx] for (idx,score) in mean_scored])def _score_sentences(sentences,topn_words):scores=[]sentence_idx=-1for s in [list(jieba.cut(s)) for s in sentences]:sentence_idx+=1word_idx=[]for w in topn_words:try:word_idx.append(s.index(w))#關(guān)鍵詞出現(xiàn)在該句子中的索引位置except ValueError:#w不在句子中password_idx.sort()if len(word_idx)==0:continue#對于兩個連續(xù)的單詞,利用單詞位置索引,通過距離閥值計算族clusters=[]cluster=[word_idx[0]]i=1while i<len(word_idx):if word_idx[i]-word_idx[i-1]<CLUSTER_THRESHOLD:cluster.append(word_idx[i])else:clusters.append(cluster[:])cluster=[word_idx[i]]i+=1clusters.append(cluster)#對每個族打分,每個族類的最大分數(shù)是對句子的打分max_cluster_score=0for c in clusters:significant_words_in_cluster=len(c)total_words_in_cluster=c[-1]-c[0]+1score=1.0*significant_words_in_cluster*significant_words_in_cluster/total_words_in_clusterif score>max_cluster_score:max_cluster_score=scorescores.append((sentence_idx,max_cluster_score))return scores;?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的NLP:基于nltk和jieba库对文本实现提取文本摘要(两种方法实现:top_n_summary和mean_scored_summary)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NLP:基于snownlp库对文本实现提
- 下一篇: 成功解决WARNING:tensorfl