NLTK语言树遍历并提取名词短语(NP)

时间:2018-08-25 22:55:02

标签: python tree nltk chunking

我创建了一个基于自定义分类器的分块器:DigDug_classifier,它对以下句子进行分块:

sentence = "There is high signal intensity evident within the disc at T1."

要创建这些块:

(S
  (NP There/EX)
  (VP is/VBZ)
  (NP high/JJ signal/JJ intensity/NN evident/NN)
  (PP within/IN)
  (NP the/DT disc/NN)
  (PP at/IN)
  (NP T1/NNP)
  ./.)

我需要创建一个上面的NP的列表,像这样:

NP = ['There', 'high signal intensity evident', 'the disc', 'T1']

我编写了以下代码:

output = []
for subtree in DigDug_classifier.parse(pos_tags): 
    try:
        if subtree.label() == 'NP': output.append(subtree)
    except AttributeError:
        output.append(subtree)
print(output)

但这给了我这个答案:

[Tree('NP', [('There', 'EX')]), Tree('NP', [('high', 'JJ'), ('signal', 'JJ'), ('intensity', 'NN'), ('evident', 'NN')]), Tree('NP', [('the', 'DT'), ('disc', 'NN')]), Tree('NP', [('T1', 'NNP')]), ('.', '.')]

我该怎么做才能得到想要的答案?

1 个答案:

答案 0 :(得分:1)

首先,请参见How to Traverse an NLTK Tree object?

特定于您提取NP的问题:

>>> from nltk import Tree
>>> parse_tree = Tree.fromstring("""(S
...   (NP There/EX)
...   (VP is/VBZ)
...   (NP high/JJ signal/JJ intensity/NN evident/NN)
...   (PP within/IN)
...   (NP the/DT disc/NN)
...   (PP at/IN)
...   (NP T1/NNP)
...   ./.)""")

# Iterating through the parse tree and 
# 1. check that the subtree is a Tree type and 
# 2. make sure the subtree label is NP
>>> [subtree for subtree in parse_tree if type(subtree) == Tree and subtree.label() == "NP"]
[Tree('NP', ['There/EX']), Tree('NP', ['high/JJ', 'signal/JJ', 'intensity/NN', 'evident/NN']), Tree('NP', ['the/DT', 'disc/NN']), Tree('NP', ['T1/NNP'])]

# To access the item inside the Tree object, 
# use the .leaves() function
>>> [subtree.leaves() for subtree in parse_tree if type(subtree) == Tree and subtree.label() == "NP"]
[['There/EX'], ['high/JJ', 'signal/JJ', 'intensity/NN', 'evident/NN'], ['the/DT', 'disc/NN'], ['T1/NNP']]

# To get the string representation of the leaves
# use " ".join()
>>> [' '.join(subtree.leaves()) for subtree in parse_tree if type(subtree) == Tree and subtree.label() == "NP"]
['There/EX', 'high/JJ signal/JJ intensity/NN evident/NN', 'the/DT disc/NN', 'T1/NNP']


# To just get the leaves' string, 
# iterate through the leaves and split the string and
# keep the first part of the "/"
>>> [" ".join([leaf.split('/')[0] for leaf in subtree.leaves()]) for subtree in parse_tree if type(subtree) == Tree and subtree.label() == "NP"]
['There', 'high signal intensity evident', 'the disc', 'T1']
相关问题