私のアプローチで引用符をエスケープしなければならなかった
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']]]], ['.', '.']]]]
注意文字列の解析を試みるのではなく、オブジェクトを構築してデシリアライズすることでした。次に、ネイティブでオブジェクトを使用できます。
質問に表示される出力は、「prettyPrint」というパイプライン上のオプションを使用して生成されます。代わりにJSON出力を取得するために "jsonPrint"に変更しました。その後、出力を取り込んでクラスを生成することができました(VSはJSONからクラスを生成するためにPaste-Specialオプションを実行するか、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);
結果のオブジェクトで作業しているので、私の答えは実際に質問に答えるわけではありません。パーサの出力は、 "Parse"という1つの文字列と同じ形式で出力されます。私は今@ user1700890にそれを解析する方法を探しに参加する! –
さらに、私はこの質問を見ると同じであるように見え、PHPを使った答えがあります:[PHPとNLP:配列への入れ子の括弧(パーサー出力)](https://stackoverflow.com/questions/7917161)/php-and-nlp-nested-parenthesis-parser-output-to-array) –