【问题标题】:Using custom POS tags for NLTK chunking?使用自定义 POS 标签进行 NLTK 分块?
【发布时间】:2014-07-07 19:42:49
【问题描述】:

在 NLTK 中制作分块语法时是否可以使用非标准的词性标签?例如,我有以下句子要解析:

complication/patf associated/qlco with/prep breast/noun surgery/diap
independent/adj of/prep the/det use/inpr of/prep surgical/diap device/medd ./pd

“medd”或“diap”等专门标签极大地帮助了从文本中找到我需要的短语。我以为因为你可以使用 RegEx 进行解析,所以它会独立于其他任何东西,但是当我尝试运行以下代码时,我得到一个错误:

grammar = r'TEST: {<diap>}'
cp = nltk.RegexpParser(grammar)
cp.parse(sentence)

ValueError: Transformation generated invalid chunkstring:
<patf><qlco><prep><noun>{<diap>}<adj><prep><det><inpr><prep>{<diap>}<medd><pd>

我认为这与标签本身有关,因为 NLTK 无法从它们生成树,但是否可以跳过该部分并只返回分块项?也许 NLTK 不是最好的工具,如果是这样,任何人都可以推荐另一个模块来分块文本吗?

我正在使用 Anaconda 发行版在 python 2.7.6 中进行开发。

提前致谢!

【问题讨论】:

  • 我遇到了同样的问题,发生在我身上的是我的一些自定义标签是空的'',这导致分块器在解析时失败

标签: python nlp nltk


【解决方案1】:

是的,可以为 NLTK 分块使用自定义标签。我也用过。 参考:How to parse custom tags using nltk.Regexp.parser()

ValueError 和错误描述表明你的语法形成有错误,你需要检查一下。您可以更新答案以获取更正建议。

【讨论】:

    【解决方案2】:
    #POS Tagging
    words=word_tokenize(example_sent)
    pos=nltk.pos_tag(words)
    print(pos)
    
    #Chunking
    chunk=r'Chunk: {<JJ.?>+<NN.?>+}'
    par=nltk.RegexpParser(chunk)
    par2=par.parse(pos)
    print('Chunking - ',par2)
    print('------------------------------ Parsing the filtered chunks')
    # printing only the required chunks
    for i  in par2.subtrees():
        if i.label()=='Chunk':
            print(i)
    print('------------------------------NER')        
    # NER
    ner=nltk.ne_chunk(pos)
    print(ner)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多