【发布时间】:2015-09-19 17:46:56
【问题描述】:
http://www.nltk.org/book/ch07.html 的第 3.1 部分中的示例说明了我正在尝试做的事情
本质上是这样的:
import nltk
text = " ..... " #Whatever the text should be
nltk.chunk.conllstr2tree(text, chunk_types=['NP']).draw()
这会根据给定的text 生成树。
我编写的代码试图使用来自文本文件的输入。
所以打开它后,我使用readlines 来获取它的字符串版本。
import nltk, re, pprint
f = open('sample.txt', 'r')
f1 = f.read().strip()
f2 = ' '.join(f1.split())
nltk.chunk.conllstr2tree(f2, chunk_types=['NP']).draw()
我得到的错误是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-768af8cd2f77> in <module>()
3 f1 = f.read().strip()
4 f2 = ' '.join(f1.split())
----> 5 nltk.chunk.conllstr2tree(f2, chunk_types=['NP']).draw()
/usr/local/lib/python3.4/dist-packages/nltk/chunk/util.py in conllstr2tree(s, chunk_types, root_label)
380 match = _LINE_RE.match(line)
381 if match is None:
--> 382 raise ValueError('Error on line %d' % lineno)
383 (word, tag, state, chunk_type) = match.groups()
384
ValueError: Error on line 0
【问题讨论】:
-
你想做什么是?块NP?识别命名实体?完全不同的东西?从你的 sn-p 看一点都不清楚。
标签: python python-3.x machine-learning nlp nltk