我导航输出的方法不是尝试解析字符串,而是构建一个对象并反序列化。然后你就有了本机可用的对象。
问题中显示的输出是使用管道上名为“prettyPrint”的选项生成的。我将其更改为“jsonPrint”以获取 JSON 输出。然后我能够获取输出并从中生成一个类(VS 可以使用 Paste-Special 选项从 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, lemma, 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);