Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NLTK:conllstr2tree无法正常工作(Python3)_Python_Python 3.x_Machine Learning_Nlp_Nltk - Fatal编程技术网

NLTK:conllstr2tree无法正常工作(Python3)

NLTK:conllstr2tree无法正常工作(Python3),python,python-3.x,machine-learning,nlp,nltk,Python,Python 3.x,Machine Learning,Nlp,Nltk,在本教程的第3.1部分中,有一个示例演示了我正在尝试做的事情 基本上是这样的: import nltk text = " ..... " #Whatever the text should be nltk.chunk.conllstr2tree(text, chunk_types=['NP']).draw() 这将根据给定的文本生成树。 我编写的代码试图使用文本文件中的输入。 因此,在打开它之后,我使用readlines获取它的字符串版本 import nltk, re, pprint f

在本教程的第3.1部分中,有一个示例演示了我正在尝试做的事情

基本上是这样的:

import nltk
text = " ..... "  #Whatever the text should be
nltk.chunk.conllstr2tree(text, chunk_types=['NP']).draw()
这将根据给定的
文本生成树。

我编写的代码试图使用文本文件中的输入。 因此,在打开它之后,我使用
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
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
3 f1=f.read().strip()
4 f2=''.join(f1.split())
---->5 nltk.chunk.conllstr2tree(f2,chunk_types=['NP']).draw()
/conllstr2树中的usr/local/lib/python3.4/dist-packages/nltk/chunk/util.py(s,chunk\u类型,root\u标签)
380匹配=线匹配(线)
381如果匹配为无:
-->382 raise VALUERROR('第%d行'%lineno'上的错误)
383(字、标记、状态、块类型)=匹配.groups()
384
ValueError:第0行出现错误

您正在从sample.txt传入原始字符串数据,修剪空格
f1
,然后在空格
f2
上标记

如果你看看他们提到的分块方法

nltk.chunk.conllstr2tree(text, chunk_types=['NP']).draw()
text
变量是一系列IOB标记的数据,如下所示:

text = """
   he PRP B-NP
   accepted VBD B-VP
   the DT B-NP
   position NN I-NP
   of IN B-PP
   vice NN B-NP
   chairman NN I-NP
   of IN B-PP
   Carlyle NNP B-NP
   Group NNP I-NP
   , , O
   a DT B-NP
   merchant NN I-NP
   banking NN I-NP
   concern NN I-NP
   . . O
"""
根据文档,conllstr2tree方法:

返回单个句子的组块结构 在给定的CONLL 2000样式字符串中编码。 此函数用于将CoNLL IOB字符串转换为树。 它使用指定的块类型 (默认为NP、PP和VP),并创建以节点为根的树 标记为S(默认情况下)

问题在于,您没有以正确的格式传递(CoNLL 2000《华尔街日报》),这应该是这样的(没有斜线):

因此,您需要执行两个额外的步骤:

  • 找到每个单词可能的词性标记
  • 查找块类型
  • 在适当的IOB标签前面加上前缀

  • 提供一个示例代码段是不合理的(对于一个SO问题),因为这是相当多的工作,但希望这能为您指明正确的方向

    很好的解释。OP引用的NLTK书籍章节给出了编写和培训chunker的完整代码;一个人只要通读这一章,就能得到相关的部分。谢谢!是的,很有道理。谢谢你指出这一点!他应该很快就好了你想干什么?块核动力源?识别命名实体?完全是别的吗?从你的片段中根本不清楚。
    token / POS Tag / IOB-Chunk Type