解決使用 nltk
的 sent_tokenize
, word_tokenize、WordNetLemmatizer
方法時報錯問題
第 2 節的手動方法的法1可解決大部分問題,可首先嘗試章節 2 的方法
1. nltk.download(‘punkt_tab’)
LookupError:
**********************************************************************Resource punkt_tab not found.Please use the NLTK Downloader to obtain the resource:>>> import nltk>>> nltk.download('punkt_tab')For more information see: https://www.nltk.org/data.html Attempted to load tokenizers/punkt_tab/english/Searched in:- 'C:\\Users\\chenw/nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\share\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\lib\\nltk_data'- 'C:\\Users\\chenw\\AppData\\Roaming\\nltk_data'- 'C:\\nltk_data'- 'D:\\nltk_data'- 'E:\\nltk_data'
**********************************************************************
根據提示可手動或自動處理這一報錯
1) 運行命令(不是每個人都能成功)
將 your_path
換為你想安裝的文件地址(必須是上面所顯示的任一路徑)
import nltk
nltk.download('punkt_tab', download_dir=your_path)
可以使用一下方法檢查 NLTK 數據路徑
import nltk
print(nltk.data.find('tokenizers/punkt_tab'))
2) 手動
首先,從官網下載文件
NLTK Corpora: https://www.nltk.org/nltk_data/
在官網頁面搜索 punkt_tab
,在第 77 條:Punkt Tokenizer Models [ download | source ]
點擊 download
下載。
之后,(我使用的是提示的第四條路徑)在上面提示路徑中的任意一個路徑中,在 ..../lib
文件夾下新建 nltk_data/tokenizers
文件夾。
然后,將下載的文件解壓到 tokenizers
文件夾下:
最后,試運行一下是不會報錯的(只要沒有哪一步出錯)
2. nltk.download(‘wordnet’)
WordNetLemmatizer().lemmatize()
LookupError:
**********************************************************************Resource wordnet not found.Please use the NLTK Downloader to obtain the resource:>>> import nltk>>> nltk.download('wordnet')For more information see: https://www.nltk.org/data.htmlAttempted to load corpora/wordnetSearched in:- 'C:\\Users\\chenw/nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\share\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\lib\\nltk_data'- 'C:\\Users\\chenw\\AppData\\Roaming\\nltk_data'- 'C:\\nltk_data'- 'D:\\nltk_data'- 'E:\\nltk_data'
**********************************************************************
同樣也是分手動和自動,按照上述的方法
1) 運行命令(不是每個人都能成功)
與 punkt_tab
的處理方法類似
import nltk
nltk.download('wordnet', download_dir=your_path)
2) 手動
法1:直接從下面的鏈接中下載語料庫(推薦)
nltk_data: https://github.com/nltk/nltk_data
將下載好的包解壓,解壓后包中 packages 中的所以文件復制到 nltk_data
文件夾中
tokenizers\punkt_tab.zip
文件需要解壓,之后無關文件可刪除
可參考文章:nltk.download(‘wordnet‘)錯誤;Resource wordnet not found. Please use the NLTK Downloader to obtain th
法2:與 1. nltk.download('punkt_tab')
的手動操作方法相同
NLTK Corpora 的 114 個壓縮包(我沒試過,文件太大,下載太慢)
3. 驗證數據包是否下載成功
from nltk.tokenize import word_tokenize
text = "This are some sample sentences to test the tokenizer."
tokens = word_tokenize(text)
print(tokens)
['This', 'are', 'some', 'sample', 'sentences', 'to', 'test', 'the', 'tokenizer', '.']
from nltk import WordNetLemmatizer
lemmatizer = WordNetLemmatizer() # 詞形還原器
wordlist = [] # 存儲詞形還原后單詞的列表# 對每個單詞進行詞形還原
for word in tokens:# 這里默認詞性為名詞("n"),可以根據需要擴展到其他詞性# 例如:使用詞性標注工具(如 nltk.pos_tag)來確定單詞的詞性lemma_word = lemmatizer.lemmatize(word, pos="n")wordlist.append(lemma_word)print(wordlist)
['This', 'are', 'some', 'sample', 'sentence', 'to', 'test', 'the', 'tokenizer', '.']