1. 程式人生 > 其它 >NLTK安裝過程中遇到的坑

NLTK安裝過程中遇到的坑

技術標籤:Python爬蟲

NLTK安裝過程中遇到的坑

  1. 無法下載

    按照網上的教程輸入如下程式碼會報錯 “Connection refused”

    import nltk
    nltk.download()
    

    在這裡插入圖片描述

    解決辦法:手動下載資料檔案,連結:https://github.com/nltk/nltk_data/tree/gh-pages(2021-1-5下載的大概620M左右)。下載之後解壓,將packages資料夾更名為“nltk_data”後放入上圖Download Directory路徑。

    然後執行程式碼:

    import nltk
    from nltk.book import *
    

    如果輸出下面的提示資訊,則代表安裝成功

    *** Introductory Examples for the NLTK Book ***
    Loading text1, …, text9 and sent1, …, sent9
    Type the name of the text or sentence to view it.
    Type: ‘texts()’ or ‘sents()’ to list the materials.
    text1: Moby Dick by Herman Melville 1851
    text2: Sense and Sensibility by Jane Austen 1811
    text3: The Book of Genesis

    text4: Inaugural Address Corpus
    text5: Chat Corpus
    text6: Monty Python and the Holy Grail
    text7: Wall Street Journal
    text8: Personals Corpus
    text9: The Man Who Was Thursday by G . K . Chesterton 1908

  2. 無法使用分詞

    我在使用word_tokenize進行分詞的時候還是會報錯,提示缺少“punkt”。解決辦法如下

    下載資料檔案,連結:https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/tokenizers/punkt.zip。然後將其解壓放入上面nltk_data 下的tokenizers中。即可正常使用分詞。