简体   繁体   English

C#运行外部批处理和Java文件

[英]C# running external batch and java file

I am using the Stanford POS-tagger application to tag some articles in about 300 files. 我正在使用Stanford POS-tagger应用程序在大约300个文件中标记一些文章。 To do this, I wrote a C# code that will go through the files and use the tagger. 为此,我编写了一个C#代码,该代码将遍历文件并使用标记器。

My code looks like this: 我的代码如下所示:

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();

And the stanford-postagger.bat looks like this: stanford-postagger.bat看起来像这样:

usage: stanford-postagger model textFile eg, stanford-postagger models\\left3words-wsj-0-18.tagger sample-input.txt 用法:stanford-postagger模型文本文件,例如stanford-postagger模型\\ left3words-wsj-0-18.tagger sample-input.txt

java -mx300m -cp "stanford-postagger.jar;" java -mx300m -cp“ stanford-postagger.jar;” edu.stanford.nlp.tagger.maxent.MaxentTagger -model %1 -textFile %2 edu.stanford.nlp.tagger.maxent.MaxentTagger -model%1 -textFile%2

The problem is: 问题是:

The code runs it, but it will NOT run the java command. 该代码运行它,但不会运行java命令。 I tried it on my laptop, and it works like a charm, it tags. 我在笔记本电脑上尝试过,它像吊饰一样工作,带有标签。 But it won't tag large files due to not enough memory. 但是由于内存不足,它不会标记大文件。 But on my PC, which is more powerfull, it will not run the java. 但是在功能更强大的PC上,它将无法运行Java。

If I open the CMD and enter that java command with the right parameters for a file, it works. 如果我打开CMD并使用文件的正确参数输入该Java命令,那么它将起作用。 Any ideea of what may cause it not to work? 有什么想法可能导致它无法正常工作? All the paths are good, I triple checked them. 所有路径都很好,我对它们进行了三重检查。

Here is an example of output I get from the non-working program(on my PC): 这是我从非工作程序(在我的PC上)获得的输出示例:

C:\\postagger>java -mx300m -cp "stanford-postagger.jar;" C:\\ postagger> java -mx300m -cp“ stanford-postagger.jar;” edu.stanford.nlp.tagger.maxent.MaxentTagger -model C:\\postagger\\models\\wsj-0-18-bidirectional-distsim.tagger -textFile C:\\brown2\\aaa.txt edu.stanford.nlp.tagger.maxent.MaxentTagger-模型C:\\ postagger \\ models \\ wsj-0-18-bidirectional-distsim.tagger -textFile C:\\ brown2 \\ aaa.txt

I'm not 100% certain about running a batch file using this, but perhaps using Process to execute it? 我不确定使用此方法运行批处理文件,但不是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM