日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

词干提取器索引文本

發布時間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 词干提取器索引文本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IndexedText.py文件:

import nltk class IndexedText(object):def __init__(self,stemmer,text):self._text=textself._stemmer=stemmerself._index=nltk.Index((self._stem(word),i) for (i,word) in enumerate(text))def concordance(self,word,width=40):key=self._stem(word)wc=width/4for i in self._index[key]:lcontext=' '.join(self._text[i-wc:i])rcontext=' '.join(self._text[i:i+wc])ldisplay='%*s' % (width,lcontext[-width:])rdisplay='%-*s'% (width,rcontext[:width])print ldisplay,rdisplaydef _stem(self,word):return self._stemmer.stem(word).lower()

%-*s 代表輸入一個字符串,-號代表左對齊、后補空白,*號代表對齊寬度由輸入時確定
%*s 代表輸入一個字符串,右對齊、前補空白,*號代表對齊寬度由輸入時確定 輸出的特點是,以lie這個關鍵詞為中心,向左和向右延伸width/4(也就是wc)長度,以此長度來截取文章中的句子,最終輸出到終端。



[root@appleyuchi NLTK]# python >>> import nltk >>> from IndexedText import * >>> porter=nltk.PorterStemmer() >>> grail=nltk.corpus.webtext.words('grail.txt') >>> text=IndexedText(porter,grail) >>> text.concordance('lie')! DENNIS : Listen , strange women lying in ponds distributing swords very brave retreat . ROBIN : All lies ! MINSTREL : [ singing ] Bra y . Nay . Come . Come . You may lie here . Oh , but you are wounimmediately ! No , no , please ! Lie down . [ clap clap ] PIGLET much danger , for beyond the cave lies the Gorge of Eternal Peril , Oh ... TIM : To the north there lies a cave -- the cave of Caerba d lived ! Bones of full fifty men lie strewn about its lair . So , our fight ' til each one of you lies dead , and the Holy Grail re >>>

代碼的作用是,在grail.txt中找出包含有關鍵詞lie的句子。



總結

以上是生活随笔為你收集整理的词干提取器索引文本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。