Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
List NLTK在python/RSS提要分块中将子树转换为列表_List_Parsing_Tree_Nltk_Chunks - Fatal编程技术网

List NLTK在python/RSS提要分块中将子树转换为列表

List NLTK在python/RSS提要分块中将子树转换为列表,list,parsing,tree,nltk,chunks,List,Parsing,Tree,Nltk,Chunks,使用下面的代码,我正在分块一个已经标记和标记的rss提要。“print subtree.leaves()”正在输出: [('Prime','NNP'),('Minister','NNP'),('Stephen','NNP'),('Harper','NNP')] [('U.S.,'NNP'),('President','NNP'),('Barack','NNP'),('Obama','NNP')] [('what\','NNP')] [('Keystone','NNP'),('XL','NNP')

使用下面的代码,我正在分块一个已经标记和标记的rss提要。“print subtree.leaves()”正在输出:

[('Prime','NNP'),('Minister','NNP'),('Stephen','NNP'),('Harper','NNP')] [('U.S.,'NNP'),('President','NNP'),('Barack','NNP'),('Obama','NNP')] [('what\','NNP')] [('Keystone','NNP'),('XL','NNP')] [('CBC','NNP'),('News','NNP')]

这看起来像一个python列表,但我不知道如何直接访问或迭代它。我认为这是一个子树输出

我希望能够将此子树转换为一个可以操纵的列表。有没有一个简单的方法可以做到这一点?这是我第一次在python中遇到树,我迷路了。我想以以下列表结束:

docs=[“斯蒂芬·哈珀总理”、“美国总统巴拉克·奥巴马”、“什么”、“Keystone XL”、“CBC新闻”]

有没有一个简单的方法来实现这一点

谢谢你一如既往的帮助

grammar = r""" Proper: {<NNP>+} """

cp = nltk.RegexpParser(grammar)
result = cp.parse(posDocuments)
nounPhraseDocs.append(result) 

for subtree in result.subtrees(filter=lambda t: t.node == 'Proper'):
# print the noun phrase as a list of part-of-speech tagged words

    print subtree.leaves()
print" "
grammar=r”““正确:{+}”
cp=nltk.RegexpParser(语法)
结果=cp.parse(posDocuments)
名词短语docs.append(结果)
对于result.subtrees中的子树(filter=lambda t:t.node=='Proper'):
#将名词短语打印为词性标记词列表
打印subtree.leaves()
打印“”
这应该能奏效


这应该可以做到。

节点
现在已被
标签
替换。因此,在回答问题时:


这将为您提供一个仅包含属于
property
chuck的令牌的列表。您可以从
subtrees()
方法中删除
filter
参数,您将获得属于树的特定父节点的所有令牌的列表。

节点
现在已被
标签
替换。因此,在回答问题时:

这将为您提供一个仅包含属于
property
chuck的令牌的列表。您可以从
subtrees()
方法中删除
filter
参数,您将获得属于树的特定父级的所有标记的列表

docs = []

for subtree in result.subtrees(filter=lambda t: t.node == 'Proper'):
    docs.append(" ".join([a for (a,b) in subtree.leaves()]))

print docs
docs = []

for subtree in result.subtrees(filter=lambda t: t.label() == 'Proper'):
    docs.append(" ".join([a for (a,b) in subtree.leaves()]))