標籤:common future similar python 標示符 div 詞彙 otl 上下
from __future__ import divisionimport nltknltk.download()from nltk.book import *#搜尋文本text1.concordance("monstrous")#出現在相似上下文中德詞彙text1.similar("monstrous")#兩個或兩個以上的詞共同的上下文text2.common_contexts(["monstrous","very"])import matplotlib#離散圖判斷詞彙在文本中的位置,從文本開頭算起在它前面有多少詞text4.dispersion_plot(["citizens","democracy","freedom","duties","American"])#產生和該文本風格相近的段落text3.generate()#返回所有標識符的個數len(text3)#為每個標示符計數,set表示集合,集合中元素只出現一次sorted(set(text3))len(set(text3))len(text3)/len(set(text3))#計算一個詞在文本中出現次數,佔據的百分比text3.count("smote")100*text4.count("a")/len(text4)
fdist1=FreqDist(text1)#計算text1中的詞頻vocabulary=fdist1.keys()#關鍵字資訊fdist1[‘whale‘]#‘whale’詞出現的頻率fdist1.plot(50,cumulative=True)#詞頻前50的詞彙進行繪圖V = set(text1)#text1 輸出詞彙集合中詞長超過15的詞彙long_words=[w for w in V if len(w) > 15]text4.collocations()#搭配頻繁出現的雙連詞[len(w) for w in text1]#text1中每個詞的詞長fdist=FreqDist([len(w) for w in text1])#每個詞長對應出現的頻率fdist#詞長只有20種fdist.max()#出現頻率最高的詞長fdist.freq(3)#給定樣本的頻率,佔全部詞彙的百分比
Python自然語言處理 Chapter 1