天天看點

自然語言處理工具 nltk 安裝使用

github 位址:https://github.com/nltk/nltk/

官方位址:http://www.nltk.org/

中文文檔:http://download.csdn.net/detail/u013378306/9756747

安裝及測試

install nltk: run <code>sudo pip install -u nltk</code>

install numpy (optional): run <code>sudo pip install -u numpy</code>

test installation: run <code>python</code> then type <code>import nltk</code>

python nltk庫中包含着大量的語料庫,但是大部分都是英文,不過有一個sinica(中央研究院)提供的繁體中文語料庫,值得我們注意。

在使用這個語料庫之前,我們首先要檢查一下是否已經安裝了這個語料庫。

下載下傳資料檔案

自然語言處理工具 nltk 安裝使用

總的資料有300m左右,下載下傳很慢,

提供下載下傳位址:https://pan.baidu.com/s/1nvfr485

nltk 資料檔案結構

資料檔案存放位址(linux下的搜尋路徑)

主要功能

自然語言處理工具 nltk 安裝使用

使用

 結果:['一', '友情', '嘉珍', '和', '我', '住在', '同一條', '巷子', '我們', ...]

(1)來看一下nltk中文文法樹。

&gt;&gt;&gt;sinica_treebank.parsed_sents()[33].draw()

自然語言處理工具 nltk 安裝使用

 python 萬歲!!!

(2)搜尋中文文本

1

2

3

4

5

<code>import</code> <code>nltk</code>

<code>from</code> <code>nltk.corpus </code><code>import</code> <code>sinica_treebank</code>

<code>sinica_text</code><code>=</code><code>nltk.text(sinica_treebank.words())</code>

<code>print</code><code>(sinica_text.concordance(</code><code>'我'</code><code>))</code>

 結果: