轉--NLTK的內置函數


NLTK的內置函數 

       1. 詞語索引

           (1) concordance函數    給出一個指定單詞每一次出現,連同上下文一起顯示。

           >>>text1.concordance('monstrous')

           (2) similar函數    查找文中上下文結構相似的詞,如the___pictures 和 the___size 等。

           >>> text1.similar("monstrous")

           (3) common_contexts 函數    檢測、查找兩個或兩個以上的詞共同的上下文。

           >>> text2.common_contexts(["monstrous", "very"])
                   be_glad am_glad a_pretty is_pretty a_lucky
           >>>

 

       2. 詞語離散圖

           判斷詞在文本中的位置:從文本開頭算起在它前面有多少詞。這個位置信息可以用離散圖表示。

           >>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
           >>>

 

       3. 詞語計數

           >>>len(text3)

           44764

 

       4. 文本-->詞表 並排序

           sorted(set(text3))

 

       5. 詞匯豐富度

           >>> from __future__ import division
           >>> len(text3) / len(set(text3))
           16.050197203298673
           >>>

 

       6. 詞在文本中出現的次數和百分比

           >>> text3.count("smote")
           5
           >>> 100 * text4.count('a') / len(text4)
           1.4643016433938312
           >>>

 

       7. 索引列表

           (1) 表示元素位置的數字叫做元素的索引。

               >>> text1[50]
               'grammars'
               >>> 

           (2) 找出一個詞第一次出現的索引。

               >>> text1.index('grammars')
               50
               >>> 

 

       8. 切片    可以獲取到文本中的詞匯(文本片段)。

               >>>text1[100:120]['and', 'to', 'teach', 'them', 'by', 'what', 'name', 'a', 'whale', '-', 'fish', 'is', 'to', 'be', 'called', 'in', 'our', 'tongue', 'leaving', 'out']
               >>> 

 

       9. NLTK 頻率分布類中定義的函數

                   例子                                                        描述
   fdist = FreqDist(samples)                     創建包含給定樣本的頻率分布
   fdist.inc(sample)                                                 增加樣本
   fdist['monstrous']                                     計數給定樣本出現的次數
   fdist.freq('monstrous')                                     給定樣本的頻率
   fdist.N()                                                               樣本總數
   fdist.keys()                                             以頻率遞減順序排序的樣本鏈表
   for sample in fdist:                                    以頻率遞減的順序遍歷樣本
   fdist.max()                                                         數值最大的樣本
   fdist.tabulate()                                                 繪制頻率分布表
   fdist.plot()                                                        繪制頻率分布圖

   fdist.plot(cumulative=True)                         繪制累積頻率分布圖
fdist1 < fdist2                                    測試樣本在fdist1 中出現的頻率是否小於fdist2

 

 

text1.concordance("monstrous") # 搜索單詞,並顯示上下文
text1.similar("monstrous") # 搜索具有相似上下文的單詞
text2.common_context(["monstrous", "very"]) #兩個或兩個以上的詞的共同的上下文
text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"]) # 將語料按時間順序拼接,此命令即可畫出這些單詞在語料中的位置,可以用來研究隨時間推移語言使用上的變化
text3.generate() # 根據語料3的詞序列統計信息生成隨機文本【計算機寫SCI論文的原理?】

len(text3) / len(set(text3)) # 計算平均詞頻 或者叫 詞匯豐富度
100* text3.count("smote") / len(text3) # 計算特定詞在文本中的百分比
標識符: All words
類型:Unique words

FreqDist(text1).keys()[:50] # 查看text1中頻率最高的前50個詞,FreeDist([])用來計算列表中元素的頻率
FreqDist(text1).hapaxes() # 查看頻率為1的詞
bigrams(['more', 'is', 'said', 'than', 'done']) # 構造雙連詞,即[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
text4.collocations() # 返回文本中的雙連詞

fdist = FreqDist(samples) 創建包含給定樣本的頻率分布
fdist.inc(sample) 增加樣本
fdist['monstrous'] 計數給定樣本出現的次數
fdist.freq('monstrous') 給定樣本的頻率
fdist.N() 樣本總數
fdist.keys() 以頻率遞減順序排序的樣本鏈表
for sample in fdist: 以頻率遞減的順序遍歷樣本
fdist.max() 數值最大的樣本
fdist.tabulate() 繪制頻率分布表
fdist.plot() 繪制頻率分布圖
fdist.plot(cumulative=True) 繪制累積頻率分布圖
fdist1 < fdist2 測試樣本在 fdist1 中出現的頻率是否小於 fdist2

 

 
# Python 過程風格與聲明風格
# 找到文本中最長的詞

maxlen = max(len(word) for word in text)
[word for word in text if len(word) == maxlen] # 熟悉並經常使用

lengths = map(len, nltk.corpus.brown.sents(categories="news"))
avg = sum(lengths) / len(lengths)

set() # 后台已經做了索引,集合成員地查找盡可能使用set

matplotlib # 繪圖工具
NetworkX # 網絡可視化


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM