nltk的安裝和簡單使用


 

使用python進行自然語言處理,有一些第三方庫供大家使用:

·NLTK(Python自然語言工具包)用於諸如標記化、詞形還原、詞干化、解析、POS標注等任務。該庫具有幾乎所有NLP任務的工具。

·Spacy是NLTK的主要競爭對手。這兩個庫可用於相同的任務。

·Scikit-learn為機器學習提供了一個大型庫。此外還提供了用於文本預處理的工具。

·Gensim是一個主題和向量空間建模、文檔集合相似性的工具包。

·Pattern庫的一般任務是充當Web挖掘模塊。因此,它僅支持自然語言處理(NLP)作為輔助任務。

·Polyglot是自然語言處理(NLP)的另一個Python工具包。它不是很受歡迎,但也可以用於各種NLP任務。

先由nltk入手學習。

1. NLTK安裝

簡單來說還是跟python其他第三方庫的安裝方式一樣,直接在命令行運行:pip install nltk

2. 運行不起來?

當你安裝完成后,想要試試下面的代碼對一段英文文本進行簡單的切分:

import nltk
text=nltk.word_tokenize("PierreVinken , 59 years old , will join as a nonexecutive director on Nov. 29 .")
print(text)

運行結果, 報錯如下:

...
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource punkt not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('punkt')
  
  For more information see: https://www.nltk.org/data.html

  Attempted to load tokenizers/punkt/english.pickle

  Searched in:
    - 'C:\\Users\\Administrator/nltk_data'
    - 'C:\\Users\\Administrator\\Desktop\\meatwice\\venv\\nltk_data'
    - 'C:\\Users\\Administrator\\Desktop\\meatwice\\venv\\share\\nltk_data'
    - 'C:\\Users\\Administrator\\Desktop\\meatwice\\venv\\lib\\nltk_data'
    - 'C:\\Users\\Administrator\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - ''
**********************************************************************

3. 解決方法:

不用着急,解決方法在異常中已經給出來了

 

命令行進入python交互模式,運行如下:

import nltk
nltk.download()

然后會彈出一個窗口,點擊models,找到punkt,雙擊進行下載即可。

然后運行開始的那段python代碼,對文本進行切分:

import nltk
text=nltk.word_tokenize("PierreVinken , 59 years old , will join as a nonexecutive director on Nov. 29 .")
print(text)

結果如下,不會報錯:

 4. nltk的簡單使用方法。

上面看了一個簡單的nltk的使用示例,下面來具體看看其使用方法。

4.1 將文本切分為語句, sent_tokenize()

from nltk.tokenize import sent_tokenize
text=" Welcome readers. I hope you find it interesting. Please do reply."
print(sent_tokenize(text))

從標點處開始切分,結果:

4.2 將句子切分為單詞, word_tokenize()

from nltk.tokenize import word_tokenize
text=" Welcome readers. I hope you find it interesting. Please do reply."
print(word_tokenize(text))

切分成單個的單詞,運行結果:

4.3.1 使用 TreebankWordTokenizer 進行切分

from nltk.tokenize import TreebankWordTokenizer
tokenizer = TreebankWordTokenizer()
print(tokenizer.tokenize("What is Love? I know this question exists in each human being's mind including myse\
lf. If not it is still waiting to be discovered deeply in your heart. What do I think of love? For me, I belie\
ve love is a priceless diamond, because a diamond has thousands of reflections, and each reflection represent\
s a meaning of love."))

也是將語句切分成單詞,運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM