在網上找了各種安裝教程,都沒有在python3下安裝nltk,于是我自己嘗試著安裝,算是成功了
1、首先,假設你的python3已經安裝好,并且安裝了numpy,matplotlib,pandas一些常用的庫,當你安裝nltk時,我相信你一定具備了一些python的常用技能,所以默認你已經安裝了以上常用模塊
2、然后,用cmd打開電腦終端,然后輸入D:/Python362/Scripts/pip install pyyaml nltk,這樣既安裝了PyYAML模塊,又安裝了nltk模塊
3、再次,在python3編輯器里輸入import nltk按回車,如下圖:
如這樣你以為nltk就安裝成功了那么你就大錯特錯了,因為還有一步,如果最后一步忽略的話,那么你在用nltk的時候會出現很多錯誤,具體出現什么樣的錯誤,忘記截圖了,后來運行成功后就沒有錯誤了。
4、最后,在python3 shell里面輸入一下命令:
按回車,然后就會出現如下窗口:
我是選擇默認的下載路徑,點擊Download,靜靜的等待它下載完,關閉窗口。
下面進行nltk測試:
sent1 = 'The cat is walking in the bedroom'sent2 = 'A dog was running across the kitchen' from sklearn.feature_extraction.text import CountVectorizercount_vec = CountVectorizer() sentences = [sent1, sent2]print(count_vec.fit_transform(sentences).toarray())print(count_vec.get_feature_names()) import nltktokens_l = nltk.word_tokenize(sent1)print(tokens_l)
運行結果如下:
[[0 1 1 0 1 1 0 0 2 1 0] [1 0 0 1 0 0 1 1 1 0 1]]['across', 'bedroom', 'cat', 'dog', 'in', 'is', 'kitchen', 'running', 'the', 'walking', 'was']['The', 'cat', 'is', 'walking', 'in', 'the', 'bedroom']
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答