NLTK 分块并遍历结果树

发布于 2024-12-07 06:35:29 字数 597 浏览 0 评论 0原文

我正在使用 NLTK RegexpParser 从标记的标记中提取名词组和动词组。

如何遍历生成的树以仅查找 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) ./.)

I'm using NLTK RegexpParser to extract noungroups and verbgroups from tagged tokens.

How do I walk the resulting tree to find only the chunks that are NP or V groups?

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 Carrier/NN)
for/IN
tissue-/JJ
and/CC
cell-culture/JJ
for/IN
(NP the/DT preparation/NN)
of/IN
(NP implants/NNS)
and/CC
(NP implant/NN)
(V containing/VBG)
(NP the/DT carrier/NN)
./.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒夜笙歌凉 2024-12-14 06:35:30

令牌中的小错误

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

Small mistake in 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
呆萌少年 2024-12-14 06:35:30

Savino 的答案很好,但值得注意的是,子树也可以通过索引访问,例如

for n in range(len(chunked)):
    do_something_with_subtree(chunked[n])

Savino's answer is great, but it's also worth noting that subtrees can be accessed by index as well, e.g.

for n in range(len(chunked)):
    do_something_with_subtree(chunked[n])
陌伤ぢ 2024-12-14 06:35:30
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)   
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)   
羅雙樹 2024-12-14 06:35:29

这应该有效:

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)

This should work:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文