我的方式来浏览输出不是试图解析字符串,而是建立一个对象并反序列化。然后你可以在本地使用该对象。
问题中显示的输出是使用名为“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);
现在我正在处理结果对象,我意识到我的答案实际上并没有回答这个问题!解析器输出仍以与一个字符串相同的格式提供,称为“解析”。我现在加入@ user1700890寻找解析它的方法! –
进一步看,我看到这个问题,它似乎是相同的,并有一个答案使用PHP:[PHP和NLP:嵌套括号(解析器输出)数组?](https://stackoverflow.com/questions/7917161/PHP-和NLP-嵌套括号解析器输出到阵列) –