首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NLTK3中遍历零块的问题

在NLTK3中遍历零块的问题
EN

Stack Overflow用户
提问于 2014-11-30 10:02:25
回答 1查看 890关注 0票数 2

嗨,我在NLTK 3中尝试这段代码:-不知何故,我设法修复了行-6来使用NLTK的第3版。但是for循环仍然没有返回任何内容。

代码语言:javascript
复制
import nltk
sample = """ some random text content with names and countries etc"""     
sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences=nltk.chunk.ne_chunk_sents(tagged_sentences) #Managed to fix this to work with version_3

for i in chunked_sentences:
    if hasattr(i,'label'):
        if i.label()=='NE':
            print i

此外,如果我尝试调试,我会看到以下输出:

代码语言:javascript
复制
for i in chunked_sentences:
    if hasattr(i,'label') and i.label:
        print i.label
S
S
S
S
S
S
S
S

那我怎么查"NE“呢?NLTK-3有什么问题,我真的无法理解out.Pls的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-30 10:41:59

看来你在重复句子。我假设您想要迭代句子中包含的各个节点。

它应该是这样的:

代码语言:javascript
复制
for sentence in chunked_sentences:
    for token in sentence: 
       if hasattr(token,'label') and token.label() == 'NE':
           print token

编辑:为了将来的参考,让我意识到你在重复句子这个事实的是,一个句子的根节点通常被标记为'S‘。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27212050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档