Python nltk -- Sinica Treebank
2015-08-18 20:13
756 查看
原url:http://cpmarkchang.logdown.com/posts/184192-python-nltk-sinica-treebank
以下为原文:
今天我們來談談如何用python nltk做中文的的自然語言處理
nltk有內建的中文treebank,是中研院的sinica treebank
是免費的中文treebank
至於要如何使用呢?
首先 先載入模組
接下來我們來看看treebank裡面的東西:
1.words, tags, sentence, tree:
其中’\xe4\xb8\x80’是以utf-8編碼的中文字
如果想知道它是什麼字,可以輸入print ‘\xe4\xb8\x80’, 輸出結果為”一”
再來是Tag和sentence
接下來,我們來看看如何印出tree的結構
結果印出來了,但看起來結構不清楚的樣子
沒關係,可以試試看用圖形介面呈現
執行這行時會跳出一個視窗,如下:
這樣就可以很清楚地把樹狀結構呈現出來了
但如果你執行這行會有error出現
請先安裝python-tk
https://wiki.python.org/moin/TkInter
2.concordance:
concordance的作用就是找某個字的前後文,例如要找”我”這個字的前後文,用法如下:
若以上程式因為編碼的error而跑不出結果,可以試試
sinica_text.concordance(u’我’.encode(‘utf-8’))
3.frequency distribution:
frequency distribution就是計算字詞在語料庫中出現的頻率
如果要計算前一百個常出現的字詞,可以這樣寫
結語:
以上是很粗淺的python nltk sinica treebank介紹
至於treebank還有什麼用呢?
用途實在太多了,在此說不完
請參考自然語言處理(Natural language processing)相關領域的教科書
以下为测试代码:
以下为原文:
今天我們來談談如何用python nltk做中文的的自然語言處理
nltk有內建的中文treebank,是中研院的sinica treebank
是免費的中文treebank
至於要如何使用呢?
首先 先載入模組
[code]>>> from nltk.corpus import sinica_treebank >>> import nltk
接下來我們來看看treebank裡面的東西:
1.words, tags, sentence, tree:
[code] >>> sinica_treebank.words() ['\xe4\xb8\x80', '\xe5\x8f\x8b\xe6\x83\x85', ...]
其中’\xe4\xb8\x80’是以utf-8編碼的中文字
如果想知道它是什麼字,可以輸入print ‘\xe4\xb8\x80’, 輸出結果為”一”
再來是Tag和sentence
[code]>>> sinica_treebank.tagged_words() [('\xe4\xb8\x80', 'Neu'), ('\xe5\x8f\x8b\xe6\x83\x85', 'Nad'), ...] >>> sinica_treebank.sents()[15] ['\xe5\xa4\xa7\xe8\x81\xb2', '\xe7\x9a\x84', '\xe5\x8f\xab', '\xe8\x91\x97']
接下來,我們來看看如何印出tree的結構
[code]>>> sinica_treebank.parsed_sents()[15] Tree('VP', [Tree('V\xe2\x80\xa7\xe5\x9c\xb0', [Tree('VH11', ['\xe5\xa4\xa7\xe8\x81\xb2']), Tree('DE', ['\xe7\x9a\x84'])]), Tree('VE2', ['\xe5\x8f\xab']), Tree('Di', ['\xe8\x91\x97'])])
結果印出來了,但看起來結構不清楚的樣子
沒關係,可以試試看用圖形介面呈現
[code]>>> sinica_treebank.parsed_sents()[15].draw()
執行這行時會跳出一個視窗,如下:
這樣就可以很清楚地把樹狀結構呈現出來了
但如果你執行這行會有error出現
請先安裝python-tk
https://wiki.python.org/moin/TkInter
2.concordance:
concordance的作用就是找某個字的前後文,例如要找”我”這個字的前後文,用法如下:
[code] >>> sinica_text=nltk.Text(sinica_treebank.words()) >>> sinica_text.concordance('我') Displaying 25 of 724 matches: 我 住在 同一條 巷子 我們 是 �� 一起 回家 有一天 上學 時 我 到 她 家 等候 按 了 門鈴 卻 ��鈴 卻 沒有 任何 動靜 正當 我 想 離開 時 門 內 突然 傳來 �� 了 門 大聲 的 叫 著 快 點 我 媽媽 暈倒 了 嘉珍 抓起 我 � 我 媽媽 暈倒 了 嘉珍 抓起 我 的 手 急忙 往 屋 裡 跑 進入 ......以下省略
若以上程式因為編碼的error而跑不出結果,可以試試
sinica_text.concordance(u’我’.encode(‘utf-8’))
3.frequency distribution:
frequency distribution就是計算字詞在語料庫中出現的頻率
如果要計算前一百個常出現的字詞,可以這樣寫
[code] >>> sinica_fd=nltk.FreqDist(sinica_treebank.words()) >>> top100=sinica_fd.items()[0:100] >>> for (x,y) in top100: >>> print x,y 的 6776 、 1482 在 1331 是 1317 了 1190 有 759 我 724 他 688 就 627 ......以下省略
結語:
以上是很粗淺的python nltk sinica treebank介紹
至於treebank還有什麼用呢?
用途實在太多了,在此說不完
請參考自然語言處理(Natural language processing)相關領域的教科書
以下为测试代码:
[code]# -*- coding:utf-8 -*- # __author__ = 'Janvn' # 2015/08/18 import nltk from nltk.corpus import sinica_treebank ''' #显示中文 words=sinica_treebank.words() for word in words: print word.encode('gbk'), ''' ''' #nltk中文词性标注 sinica_text=nltk.Text(sinica_treebank.words()) print sinica_text for (key,var) in sinica_treebank.tagged_words()[:8]: word_tag='%s/%s' % (key,var) print word_tag.encode('gbk'), ''' ''' #nltk中文句法树 sent=sinica_treebank.sents()[15] print sent for word in sent: print word.encode('gbk'), parse_sent=sinica_treebank.parsed_sents()[15] print parse_sent parse_sent.draw() ''' ''' #搜索中文文本 sinica_text=nltk.Text(sinica_treebank.words()) sinica_text.concordance(u'我') #查找关键字 ''' #nltk计算中文高频词,测试的时候貌似只计算了词频,却没得到前100个高频词?why,why,why? sinica_fd=nltk.FreqDist(sinica_treebank.words()) top100=sinica_fd.items()[0:100] for (x,y) in top100: print x,y
相关文章推荐
- Python中有关list的增删改查
- python学习--windows下安装Numpy包的错误:Unable to find vcvarsall.bat
- python第三方库系列之二十二--subprocess的用法
- python视频教程大全集下载啦!超级全的教程!
- 我第一个Python教程笔记
- python中的有序字典OrderedDict
- Python3与Python2的区别
- 15 正则表达式 - 《Python 核心编程》
- Python学习 windows下面安装Python和pip(一)
- 14 执行环境 - 《Python 核心编程》
- python之路-----搭建django框架
- python 图片爬虫
- 浅析python 中__name__ = '__main__' 的作用
- vijos - P1494杨辉三角形中的偶数个数 (递归分治 + 记忆化搜索 + python)
- openCV—Python(11)—— 图像边缘检测
- python中MySQLdb模块用法实例
- python list遍历时删除元素的推荐做法
- 从错误中学python(4)——最小公约数与辗转相除法
- 一个很好的python代理脚本(给sqlmapapi发送请求的玩意),函数可以参考拿着用呗
- 03-树1. 二分法求多项式单根(20) Python