[英]C# running external batch and java file
我正在使用Stanford POS-tagger應用程序在大約300個文件中標記一些文章。 為此,我編寫了一個C#代碼,該代碼將遍歷文件並使用標記器。
我的代碼如下所示:
Process thisProcess=new Process();
thisProcess.StartInfo.CreateNoWindow=true;
thisProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory=@"C:\postagger";
thisProcess.StartInfo.FileName=@"C:\postagger\stanford-postagger.bat";
thisProcess.StartInfo.UseShellExecute=false;
thisProcess.StartInfo.RedirectStandardOutput=true;
if(Directory.Exists(@"C:\brown2")) {
DirectoryInfo brown=new DirectoryInfo(@"C:\brown2");
DirectoryInfo brownParsed;
if(!Directory.Exists(@"C:\brown-parsed"))
brownParsed=Directory.CreateDirectory(@"C:\brown-parsed");
else
brownParsed=new DirectoryInfo(@"C:\brown-parsed");
FileInfo[] files=brown.GetFiles();
foreach(FileInfo f in files) {
Console.WriteLine("Parsing file "+f.Name+" ...");
thisProcess.StartInfo.Arguments=@"C:\postagger\models\wsj-0-18-bidirectional-distsim.tagger "+f.FullName;
//Console.WriteLine(thisProcess.StartInfo.Arguments);
thisProcess.Start();
thisProcess.WaitForExit();
//Console.Read();
StreamWriter sw=new StreamWriter(Path.Combine(brownParsed.FullName, f.Name), false);
string output=thisProcess.StandardOutput.ReadToEnd();
//sw.Write(thisProcess.StandardOutput.ReadToEnd());
sw.Write(output);
sw.Flush();
sw.Close();
//Console.WriteLine("File {0} done!",f.Name);
Console.WriteLine(output);
}
}
else
Console.WriteLine("Dir not found!");
Console.Read();
stanford-postagger.bat看起來像這樣:
用法:stanford-postagger模型文本文件,例如stanford-postagger模型\\ left3words-wsj-0-18.tagger sample-input.txt
java -mx300m -cp“ stanford-postagger.jar;” edu.stanford.nlp.tagger.maxent.MaxentTagger -model%1 -textFile%2
問題是:
該代碼運行它,但不會運行java命令。 我在筆記本電腦上嘗試過,它像吊飾一樣工作,帶有標簽。 但是由於內存不足,它不會標記大文件。 但是在功能更強大的PC上,它將無法運行Java。
如果我打開CMD並使用文件的正確參數輸入該Java命令,那么它將起作用。 有什么想法可能導致它無法正常工作? 所有路徑都很好,我對它們進行了三重檢查。
這是我從非工作程序(在我的PC上)獲得的輸出示例:
C:\\ postagger> java -mx300m -cp“ stanford-postagger.jar;” edu.stanford.nlp.tagger.maxent.MaxentTagger-模型C:\\ postagger \\ models \\ wsj-0-18-bidirectional-distsim.tagger -textFile C:\\ brown2 \\ aaa.txt
我不確定使用此方法運行批處理文件,但不是100%確定,但是也許可以使用Process執行該文件?
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "path-to-file.bat"
}
};
process.Start();
process.WaitForExit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.