python安裝nltk

nltk官方地址:http://nltk.org

安裝nltk

pip install nltk

下載nltk數據包

import nltk
nltk.download()

如果下載報錯,請到https://github.com/nltk/nltk_data下載,把package目錄下的目錄到copy到nltk_data文件夾下,並放到usr/local/lib/nltk_data

詞頻統計,測試nltk安裝
pip3 install html5lib

import urllib.request
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen('https://blog.csdn.net/xuyankuanrong')
html = response.read()
soup = BeautifulSoup(html, "html5lib")
# 這需要安裝html5lib模塊
text = soup.get_text(strip=True)
tokens = [t for t in text.split()]
freq = nltk.FreqDist(tokens)
for key,val in freq.items():
    print (str(key) + ':' + str(val))

參考來源

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