《使用python進行自然語言理解(Nltk)》1.2
阿新 • • 發佈:2018-12-10
直接上程式碼:
1、例項測試1
# -*- coding: UTF-8 -*- # !/usr/python/bin # Filename:NltkTest42 '''一些關於文字的統計資訊的測試''' from __future__ import division import nltk from nltk.book import * import time import datetime class NltkTest42: def __init__(self, text, sent): self.text = text self.sent = sent print self.text print self.sent def SomeTests(self): '''簡單的邏輯關係的測試''' print self.sent print [w for w in self.sent if len(w) < 4] print [w for w in self.sent if len(w) <= 4] print [w for w in self.sent if len(w) == 4] print [w for w in self.sent if len(w) != 4] print sorted([w for w in set(self.text) if w.endswith('ableness')]) print sorted([term for term in set(self.text) if 'gnt' in term]) sorted([item for item in set(self.text) if item.istitle()]) sorted([item for item in set(self.sent) if item.isdigit()]) [len(w) for w in self.text] [w.upper() for w in self.text] tricky = sorted([w for w in set(self.text) if 'cie' in w or 'cei' in w]) for word in tricky: if (len(word) > 10): print word nt42 = NltkTest42(text1, sent7) starttime = datetime.datetime.now() print 'Start at:' print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) nt42.SomeTests() endtime = datetime.datetime.now() print 'Finish at:' print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print '程式才運行了%d秒' % (endtime - starttime).seconds print '聰明的你告訴我,其他的時間都去哪了?' # 一些詞比較運算子 # s.startswith(t) 測試 s 是否以 t 開頭 # s.endswith(t) 測試 s 是否以 t 結尾 # t in s 測試 s 是否包含 t # s.islower() 測試 s 中所有字元是否都是小寫字母 # s.isupper() 測試 s 中所有字元是否都是大寫字母 # s.isalpha() 測試 s 中所有字元是否都是字母 # s.isalnum() 測試 s 中所有字元是否都是字母或數字 # s.isdigit() 測試 s 中所有字元是否都是數字 # s.istitle() 測試 s 是否首字母大寫(s 中所有的詞都首字母大寫)
2、例項測試2
# -*- coding: UTF-8 -*- #!/user/python/bin #filename:Nltk_test091902 //一些關於文字統計資訊的測試 import nltk from nltk.book import * class NltkTest38: def __init__(self,text): self.text=text print self.text def FreqAnalyse(self,queryStr): '''統計高頻和低頻詞並對TOP50的高頻詞畫圖''' fdist=FreqDist(self.text) vocabulary =fdist.keys() hapaxesWord = fdist.hapaxes() #單頻詞 print hapaxesWord[:50] #高頻詞 print vocabulary[:50] #畫圖,False看的舒服一些 fdist.plot(50,cumulative=False) print fdist[queryStr] def LongWord(self): '''找出長度大於15的詞彙''' voc=set(self.text) #長度大於15的詞 longWords=[word for word in voc if len(word)>15] print 'longword:' print sorted(longWords) def CheckUseless(self): '''找出高頻詞和長低頻次''' fdist =FreqDist(self.text) print '高頻詞和長低頻' print sorted([word for word in set(self.text) if len(word)>7 and fdist[word]>7]) def BigramsCheck(self): '''提取文字詞彙中的詞對也就是雙連詞''' #指定詞查詢雙連詞 print '雙連詞' print bigrams(['more','is','said','than','done']) #提取文字詞彙中的詞對,也就是雙連詞 #全域性找雙連詞 print(self.text.collocations()) #找到比我們基於單個詞的頻率預期得到的更頻繁出現的雙連詞 def Others(self): '''計數其他東西''' fdist=FreqDist([len(word) for word in self.text]) print fdist.keys() print(fdist.items()) print fdist[fdist.max()] print fdist.freq(fdist.max()) fdist.tabulate() fdist.plot() nt38=NltkTest38(text1) nt38.FreqAnalyse('whale') nt38.LongWord() nt38.CheckUseless() nt38.BigramsCheck() nt38.Others()