Python使用nltk进行Chart Parsing
Python用自然语言处理工具nltk(Natural Language Toolkit, http://www.nltk.org/ )完成计算语言学中的Chart Parsing。
安装
使用
pip
安装http://www.nltk.org/install.html
1
pip install nltk
或下载安装包安装( http://pypi.python.org/pypi/nltk )
1
python setup.py install
需要安装NLTK Data( http://www.nltk.org/data.html )。在python中输入以下代码:
1
2import nltk
nltk.download()
Chart Parsing
参考
http://www.nltk.org/api/nltk.parse.html
http://www.ling.helsinki.fi/kit/2008s/clt231/nltk-0.9.5/doc/en/ch08.html
demo - chart parser
tree
Parse a sentence John ate the cat
1 | import nltk |
output
1 | |. John . ate . the . cat .| |
Well-Formed Substring Tables
Parse a sentence John ate the cat
1 | import nltk |
output
1 | WFST 1 2 3 4 |
Charts
Parse a sentence John ate the cat
1 | import nltk |
output
1 | [2] ART [3] N [4] ==> [2] NP [4] |
earleychart demo
1 | import nltk |
Complex grammar and tokens
1 | grammar = nltk.CFG.fromstring(""" |