我正在使用NLTK RegexpParser从标记的标记中提取noungroups和verbgroups。
如何查看结果树以仅查找NP或V组的块?
from nltk.chunk import RegexpParser
grammar = '''
NP: {<DT>?<JJ>*<NN>*}
V: {<V.*>}'''
chunker = RegexpParser(grammar)
token = [] ## Some tokens from my POS tagger
chunked = chunker.parse(tokens)
print chunked
#How do I walk the tree?
#for chunk in chunked:
# if chunk.??? == 'NP':
# print chunk
(S (NP运营商/ NN) 为/ IN 组织 - / JJ 和/ CC 细胞培养/ JJ 为/ IN (NP / DT准备/ NN) 中/ IN (NP植入物/ NNS) 和/ CC (NP种植体/ NN) (V包含/ VBG) (NP / DT载波/ NN) ./.)
答案 0 :(得分:13)
这应该有效:
for n in chunked:
if isinstance(n, nltk.tree.Tree):
if n.label() == 'NP':
do_something_with_subtree(n)
else:
do_something_with_leaf(n)
答案 1 :(得分:0)
token
from nltk.chunk import RegexpParser
grammar = '''
NP: {<DT>?<JJ>*<NN>*}
V: {<V.*>}'''
chunker = RegexpParser(grammar)
token = [] ## Some tokens from my POS tagger
//chunked = chunker.parse(tokens) // token defined in the previous line but used tokens in chunker.parse(tokens)
chunked = chunker.parse(token) // Change in this line
print chunked
答案 2 :(得分:0)
Savino的答案很棒,但同样值得注意的是,也可以通过索引访问子树,例如。
for n in range(len(chunked)):
do_something_with_subtree(chunked[n])
答案 3 :(得分:0)
def preprocess(sent):
sent = nltk.word_tokenize(sent)
sent = nltk.pos_tag(sent)
return sent
pattern = 'NP: {<JJ>*<NNP.*>*}'
cp = nltk.RegexpParser(pattern)
exp = []
for line in lines:
line = preprocess(line)
cs = cp.parse(line)
for n in cs:
if isinstance(n, nltk.tree.Tree):
if n.label() == 'NP':
if len(n.leaves()) > 1:
req = ''
for leaf in n.leaves():
req += leaf[0]+' '
exp.append(req)
print(exp)