2017-06-21 106 views
0

斯坦福核心NLP解析器生成句子以下的输出:导航斯坦福CoreNLP解析结果

"He didn't get a reply" 

(ROOT 
(S 
(NP (PRP He)) 
(VP (VBD did) (RB n’t) 
(VP (VB get) 
(NP (DT a) (NN reply)))) 
(. .))) 

我需要一种方法来轻松浏览它即额外的标签,发现孩子和家长。目前我正在手动进行(统计括号)。我想知道是否有一个Python库可以为我进行括号计数,或者甚至更好,比如Beautiful Soup或Scrapy会让我使用对象。

如果没有工具,遍历句子并获取所有标签的最佳方法是什么?我猜我需要创建一些标签对象与包含儿童标签对象的列表。

回答

1

这看起来像LISP。编写一个Lisp程序来遍历它并提取你想要的内容似乎很容易。

你也可以将它转换成一个列表,Python和过程中的Python:

from pyparsing import OneOrMore, nestedExpr 
nlpdata = '(ROOT (S (NP (PRP He)) (VP (VBD did) (RB n\'t) (VP (VB get) (NP (DT a) (NN reply)))) (. .)))' 
data = OneOrMore(nestedExpr()).parseString(nlpdata) 
print data 
# [['ROOT', ['S', ['NP', ['PRP', 'He']], ['VP', ['VBD', 'did'], ['RB', "n't"], ['VP', ['VB', 'get'], ['NP', ['DT', 'a'], ['NN', 'reply']]]], ['.', '.']]]] 

注意,我必须逃脱报价中的“不”

1

我的方式来浏览输出不是试图解析字符串,而是建立一个对象并反序列化。然后你可以在本地使用该对象。

问题中显示的输出是使用名为“prettyPrint”的管道上的选项生成的。我将其改为“jsonPrint”来取代JSON输出。然后我可以获取输出并从中生成一个类(VS可以通过粘贴特殊选项从JSON生成一个类,或者有像http://json2csharp.com/这样的在线资源)。生成的类如下所示:

public class BasicDependency 
    { 
     public string dep { get; set; } 
     public int governor { get; set; } 
     public string governorGloss { get; set; } 
     public int dependent { get; set; } 
     public string dependentGloss { get; set; } 
    } 

    public class EnhancedDependency 
    { 
     public string dep { get; set; } 
     public int governor { get; set; } 
     public string governorGloss { get; set; } 
     public int dependent { get; set; } 
     public string dependentGloss { get; set; } 
    } 

    public class EnhancedPlusPlusDependency 
    { 
     public string dep { get; set; } 
     public int governor { get; set; } 
     public string governorGloss { get; set; } 
     public int dependent { get; set; } 
     public string dependentGloss { get; set; } 
    } 

    public class Token 
    { 
     public int index { get; set; } 
     public string word { get; set; } 
     public string originalText { get; set; } 
     public string lemma { get; set; } 
     public int characterOffsetBegin { get; set; } 
     public int characterOffsetEnd { get; set; } 
     public string pos { get; set; } 
     public string ner { get; set; } 
     public string speaker { get; set; } 
     public string before { get; set; } 
     public string after { get; set; } 
     public string normalizedNER { get; set; } 
    } 

    public class Sentence 
    { 
     public int index { get; set; } 
     public string parse { get; set; } 
     public List<BasicDependency> basicDependencies { get; set; } 
     public List<EnhancedDependency> enhancedDependencies { get; set; } 
     public List<EnhancedPlusPlusDependency> enhancedPlusPlusDependencies { get; set; } 
     public List<Token> tokens { get; set; } 
    } 

    public class RootObject 
    { 
     public List<Sentence> sentences { get; set; } 
    } 

*注意:不幸的是,这种技术对于coref注释并不适用。 JSON没有正确转换为类。我现在正在处理这个问题。该模型是使用注释器“tokenize,ssplit,pos,引理,ner,parse”从输出中构建的。

我的代码,只能从样本代码略有改变,看起来是这样的(注意“pipeline.jsonPrint”):

public static string LanguageAnalysis(string sourceText) 
     { 
      string json = ""; 
      // Path to the folder with models extracted from stanford-corenlp-3.7.0-models.jar 
      var jarRoot = @"..\..\..\..\packages\Stanford.NLP.CoreNLP.3.7.0.1\"; 

      // Annotation pipeline configuration 
      var props = new Properties(); 
      props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); 
      props.setProperty("ner.useSUTime", "0"); 

      // We should change current directory, so StanfordCoreNLP could find all the model files automatically 
      var curDir = Environment.CurrentDirectory; 
      Directory.SetCurrentDirectory(jarRoot); 
      var pipeline = new StanfordCoreNLP(props); 
      Directory.SetCurrentDirectory(curDir); 

      // Annotation 
      var annotation = new Annotation(sourceText); 
      pipeline.annotate(annotation); 

      // Result - JSON Print 
      using (var stream = new ByteArrayOutputStream()) 
      { 
       pipeline.jsonPrint(annotation, new PrintWriter(stream)); 
       json = stream.toString(); 
       stream.close(); 
      } 

      return json; 
     } 

这似乎与这样的代码很好地反序列化:

using Newtonsoft.Json; 
string sourceText = "My text document to parse."; 
string json = Analysis.LanguageAnalysis(sourceText); 
RootObject document = JsonConvert.DeserializeObject<RootObject>(json); 
+0

现在我正在处理结果对象,我意识到我的答案实际上并没有回答这个问题!解析器输出仍以与一个字符串相同的格式提供,称为“解析”。我现在加入@ user1700890寻找解析它的方法! –

+0

进一步看,我看到这个问题,它似乎是相同的,并有一个答案使用PHP:[PHP和NLP:嵌套括号(解析器输出)数组?](https://stackoverflow.com/questions/7917161/PHP-和NLP-嵌套括号解析器输出到阵列) –