Stanford nlp 使用斯坦福nlp对一些文本进行分块

Stanford nlp 使用斯坦福nlp对一些文本进行分块,stanford-nlp,Stanford Nlp,我正在使用斯坦福核心NLP,我使用这一行加载一些模块来处理我的文本: props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); 是否有一个模块,我可以加载到文本块 或者有没有其他的建议,可以用斯坦福大学的core来拼凑文本 谢谢我认为解析器输出可以用于获取NP块。查看上的上下文无关表示法,它提供了示例输出。要在斯坦福NLP中使用分块,可以使用以下软件包: YamCha:基于SVM的NP chunk

我正在使用斯坦福核心NLP,我使用这一行加载一些模块来处理我的文本:

props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
是否有一个模块,我可以加载到文本块

或者有没有其他的建议,可以用斯坦福大学的core来拼凑文本


谢谢

我认为解析器输出可以用于获取NP块。查看上的上下文无关表示法,它提供了示例输出。

要在斯坦福NLP中使用分块,可以使用以下软件包:

  • YamCha:基于SVM的NP chunker,也可用于词性标记、NER等。C/C++开源。Won CoNLL 2000共享任务。(与终端用户专用的POS标签机相比,自动化程度更低。)
  • Mark Greenwood的名词短语Chunker:Ramshaw和Marcus(1995)的Java重新实现
  • FNTBL:一种快速灵活的C++实现基于转换的学习方法。包括POS标记器,但也包括NP组块和通用组块模型
资料来源:
您需要的是CoreNLP中的选区解析的输出,它为您提供组块信息,例如动词短语(VP)、名词短语(NPs)等。但据我所知,CoreNLP中没有方法为您提供组块列表。这意味着您必须解析选区解析的实际输出以提取区块

例如,这是CoreNLP的选区分析器对一个示例句子的输出:

(ROOT (S ("" "") (NP (NNP Anarchism)) (VP (VBZ is) (NP (NP (DT a) (JJ political) (NN philosophy)) (SBAR (WHNP (WDT that)) (S (VP (VBZ advocates) (NP (NP (JJ self-governed) (NNS societies)) (VP (VBN based) (PP (IN on) (NP (JJ voluntary) (, ,) (JJ cooperative) (NNS institutions))))))))) (, ,) (S (VP (VBG rejecting) (NP (JJ unjust) (NN hierarchy))))) (. .)))

如您所见,字符串中有NP和VP标记,现在您必须通过解析该字符串来提取块的实际文本。让我知道你是否能找到一个方法,给你块列表

根据Pedram的答案展开,可以使用以下代码:

from nltk.parse.corenlp import CoreNLPParser
nlp = CoreNLPParser('http://localhost:9000')  # Assuming CoreNLP server is running locally at port 9000


def extract_phrase(trees, labels):
    phrases = []
    for tree in trees:
        for subtree in tree.subtrees():
            if subtree.label() in labels:
                t = subtree
                t = ' '.join(t.leaves())
                phrases.append(t)
    return phrases


def get_chunks(sentence):
    trees = next(nlp.raw_parse(sentence))
    nps = extract_phrase(trees, ['NP', 'CC'])
    vps = extract_phrase(trees, ['VP'])
    return trees, nps, vps


if __name__ == '__main__':
    dialog = [
        "Anarchism is a political philosophy that advocates self-governed societies based on voluntary cooperative institutions rejecting unjust hierarchy"
    ]
    for sentence in dialog:
        trees, nps, vps = get_chunks(sentence)
        print("\n\n")
        print("Sentence: ", sentence)
        print("Tree:\n", trees)
        print("Noun Phrases: ", nps)
        print("Verb Phrases: ", vps)

"""
Sentence:  Anarchism is a political philosophy that advocates self-governed societies based on voluntary cooperative institutions rejecting unjust hierarchy
Tree:
 (ROOT
  (S
    (NP (NN Anarchism))
    (VP
      (VBZ is)
      (NP
        (NP (DT a) (JJ political) (NN philosophy))
        (SBAR
          (WHNP (WDT that))
          (S
            (VP
              (VBZ advocates)
              (NP
                (ADJP (NN self) (HYPH -) (VBN governed))
                (NNS societies))
              (PP
                (VBN based)
                (PP
                  (IN on)
                  (NP
                    (NP
                      (JJ voluntary)
                      (JJ cooperative)
                      (NNS institutions))
                    (VP
                      (VBG rejecting)
                      (NP (JJ unjust) (NN hierarchy)))))))))))))
Noun Phrases:  ['Anarchism', 'a political philosophy that advocates self - governed societies based on voluntary cooperative institutions rejecting unjust hierarchy', 'a political philosophy', 'self - governed societies', 'voluntary cooperative institutions rejecting unjust hierarchy', 'voluntary cooperative institutions', 'unjust hierarchy']
Verb Phrases:  ['is a political philosophy that advocates self - governed societies based on voluntary cooperative institutions rejecting unjust hierarchy', 'advocates self - governed societies based on voluntary cooperative institutions rejecting unjust hierarchy', 'rejecting unjust hierarchy']

"""

“组块”是指挑选基本NP组块和动词组之类的东西吗?或者你的意思是把一篇大文章分成几个部分,比如相关的文本分组,比如个人博客评论?我有完全相同的问题;在我的例子中,我指的是提取名词短语,例如,这些只是NP组块的软件包。例如:Mark Greenwood的名词短语Chunker提供了一个GATE包装器,但没有任何用于使用StanfordNLP解析树等的包装器。我认为至少可以进行基于正则表达式的分块——可以有一个自定义的分块注释器添加到管道中。假设一个自定义注释器在POS上使用TokenRegex,放在管道中的“parse”之后。这样,解析树可以有一个或多个节点“NNP”,在该节点下有分块标记。希望有人为coreNLP做过这件事。