C#でStanford Core NLPを使用してテキストファイルを解析するフロントエンドを構築しています。私はファイル選択ダイアログを開き、いくつかのテキストファイルを選択します。それから、以下の方法がそこから機能します。Stanford Core NLPのプロパティメソッドのタイプ
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using java.util;
using java.io;
using edu.stanford.nlp.pipeline;
namespace Parser_SVO
{
public partial class Form1 : Form
{
public static List<string> textFiles = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.ShowReadOnly = true;
openFileDialog1.Filter = "Text Files|*.txt";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textFiles.AddRange(openFileDialog1.FileNames);
}
parseText();
}
public static void parseText()
{
label2.Text = "Stanford Parser....";
// Path to the folder with models extracted from `stanford-corenlp-3.7.0-models.jar`
string jarRoot = "";
string prettyPrint = "";
if (textFiles.Count != 0)
{
jarRoot = Path.GetDirectoryName(textFiles[0]) + @"\Models\";
prettyPrint = Path.GetDirectoryName(textFiles[0]);
Directory.CreateDirectory(prettyPrint + @"\PrettyPrint\");
prettyPrint = prettyPrint + @"\PrettyPrint\";
}
// Annotation pipeline configuration
var props = Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
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);
foreach (string file in textFiles)
{
label3.Text = file;
// Text for processing
var text = System.IO.File.ReadAllText(file);
// Annotation
var annotation = new Annotation(text);
pipeline.annotate(annotation);
// Result - Pretty Print
string output = prettyPrint + Path.GetFileName(file);
using (var stream = new ByteArrayOutputStream())
{
pipeline.prettyPrint(annotation, new PrintWriter(stream));
System.IO.File.AppendAllText(output, stream.toString()+Environment.NewLine);
stream.close();
}
}
}
}
}
私は公式StanfordCoreNLPネットポートhereから例を変更しました。 コンソールアプリケーションの代わりにWindowsフォームを使用しているので、このコード行は問題を作成しています:var props = Properties();
。私は完全なnamespace.class.methodのパスを提供するために、このメソッドの名前空間を見つける方法を明確にしていません。 もう一つの小さな問題は、label2.Text = "Stanford Parser....";
のようにラベルテキストを更新したいのですが、Visual Studioでは、同じクラス(Forms1.cs)にいる間に "オブジェクト参照が必要です"と言います。あなたの助けに感謝します。
'java.util.Properties'はタイプです。あなたは 'var props = new Properties();'を意味しましたか? – Crowcoder
はい。それを後で考え出す。今、私は静的メソッドからラベルのテキストを変更することに苦労しています。そして、利用可能な1つの凝視的な回答はないようです。 –
'Label'をパラメータとして取ることができますが、それはハックです。' public static void ParseText(Label lbl) '、' lbl.Text = "..."; ' – Crowcoder