自然語言工具箱

(由NLTK跳轉過嚟)

Natural Language Toolkit粵語口語可以叫:自然語言工具箱),簡稱 NLTK,係 Python 一個專用嚟幫手做英文自然語言處理(NLP)嘅函式庫。NLTK 包含咗多種 NLP 成日要用嘅功能,例如係畀用家齋靠一句陳述式就產生到一樖分析樹,分析樹可以攞嚟分析句子文法結構[1][2]

英文句子 A mouse eats a cat(一隻老鼠食一隻)用分析樹表達嘅樣;
A mouse 結合成一段名詞短語NP),
a cat 又係結合成一段名詞短語(NP),
eats現在式,第三身所以加 s)係動詞V)。

喺廿一世紀初,NLTK 廣受 NLP 相關領域嘅工作者採用:語言學認知科學人工智能同埋資訊科學等領域嘅工作,都成日要教電腦處理文字數據;而經驗表明 NLTK 好方便好好使,於是就成為咗 NLP 上嘅一隻標準架生,呢啲咁多唔同領域嘅工作者都會用到 NLTK 寫程式[3]

重要功能 編輯

  • 畀用家下載同引入語料庫嘅各種資源,包括出名嘅文學作品字表詞典同埋語義網絡呀噉,例:
    import nltk.corpus # 引入 NLTK 嘅語料庫
    
    emma = nltk.corpus.gutenberg.words('austen-emma.txt') # 將 emma 設做 nltk.corpus 入面嘅...
    print(emma) # output 出 emma。
    
  • 畀用家用一句陳述式就搵出字之間嘅關係,例:
    ...
    moby = Text(nltk.corpus.gutenberg.words('melville-moby_dick.txt')) # 將 moby 設做一嚿 Text 物件,入面啲字嚟自 nltk.corpus 嘅 'melville-moby_dick.txt'...
    
    print(moby.concordance("monstrous")) # Output 畀出所有 "monstrous" 呢隻字出現嘅 context。
    print(moby.similar("monstrous")) # Output 畀出所有 context 上同 "monstrous" 相近嘅字。
    
  • 畀用家用一句陳述式畫圖顯示字詞嘅頻率,例[4]
    ...
    moby.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"]) 
    # 出幅圖顯示 "citizen", "democracy", "freedom", "duties", "America" 呢幾隻字喺 moby 唔同部份入面出現嘅頻率。
    
  • 畀用家引入語義網絡同搵字嘅同義字,仲可以做「搵兩隻字之間最低嘅共同上位詞」或者「計兩隻字之間嘅語義距離」等,例[1]:Ch. 2.5
    from nltk.corpus import wordnet as wn
    
    print(wn.synsets('word'))
    wn.synset('word.n.01').path_similarity(wn.synset('whale.n.01'))
    # Output 畀出 "word" 呢隻字嘅同義字。
    
  • 畀玩家用一句陳述式做記號化詞形還原字幹提取同埋攞走停用詞等嘅事前處理,例如 word_tokenize(my_string) 噉。
  • 畀玩家用一句陳述式計啲字喺一段語料當中嘅頻率分佈FreqDist(my_text)ConditionalFreqDist(my_text_cond)[1]:Ch. 2
  • 畀玩家用一句陳述式做 regex 同相關嘅功能[1]:Ch. 3.4, p. 117,好似係 re.search('ed$', w)(搵 w 當中 -ed 尾嘅字)、re.search(^..j..t..$', w)(搵 w 當中 ..j..t.. 噉嘅字,當中 . 係乜字母都得)同 re.findall(r'[aeiou]', w)(由 w 當中搵出嗮所有 aeiou)... 呀噉。
  • 畀玩家用一句陳述式整 n-gram

... 呀噉。

睇埋 編輯

文獻 編輯

  • Bird, S., Klein, E., & Loper, E. (2009). Natural language processing with Python: analyzing text with the natural language toolkit. O'Reilly Media, Inc.

引咗 編輯

  1. 1.0 1.1 1.2 1.3 1.4 Bird, S., Klein, E., & Loper, E. (2009). Natural language processing with Python: analyzing text with the natural language toolkit. O'Reilly Media, Inc.
  2. Perkins, Jacob (2010). Python Text Processing with NLTK 2.0 Cookbook. Packt Publishing.
  3. Bird, S., Klein, E., Loper, E., & Baldridge, J. (2008, June). Multidisciplinary instruction with the natural language toolkit (PDF). In Proceedings of the Third Workshop on Issues in Teaching Computational Linguistics (pp. 62-70).
  4. Language Processing and Python.

編輯