手動安裝Python自然語言工具包NLTK

在嘗試用官方的安裝方式幾次失敗後,發現官方的這種方式真的很坑。於是上網按着大家的教程,終於摸索出了一個可行的方法。

安裝

首先下載nltk安裝包:

官方下載網址

github下載地址1

github下載地址2

解壓

下載完成後,將文件解壓放在D盤根目錄。

測試

然後打開IDE進行測試:

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

到這裏,說明nltk庫已經安裝好了,但是在編程測試的時候,仍然報錯:

from nltk import word_tokenize
from nltk import Text

tokens = word_tokenize("Here is some not very interesting text")
text = Text(tokens)

print(text)

錯誤1:punkt沒找到

解決方案:

1、打開路徑:D:\nltk_data\tokenizers,發現有一個名爲<punkt.zip>的壓縮包;

2、選中該壓縮包,解壓到當前目錄下

再次運行上述程序,又報錯,錯誤爲:D:\nltk_data\tokenizers\punkt\PY3\english.pickle沒找到

解決方案:

1、打開路徑D:\nltk_data\tokenizers\punkt,發現其實存在english.pickle

2、路徑檢索的時候多了個\PY3\,所以,在D:\nltk_data\tokenizers\punkt新建一下文件夾,命名爲PY3,然後將文件english.pickle拷貝進去PY3文件夾下

再次運行程序,結果正確:

<Text: Here is some not very interesting text...>

總結

如果在運行程序時,提示說沒有找到某個文件,那麼可以去該路徑下查看,是否沒有解壓文件,或者路徑不對。

按照提示路徑創建文件即可。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章