c# 调用 CRFs应用程序
今天想用C#调用crfs,但是老出问题。原因有几点。第一,我对crf不理解,虽然我用cmd跑了一遍,但是根本不理解为什么,而且只是草草看了下参数该输入什么,只是了解了形式,没有了解实质。所以在调用的时候,我不知道怎样转移输入输出的参数。另外,crf_learn在cmd运行的时候,用到了一个自带的dll文件我也不知道怎么处理。而且我在网上找类似调用的例子也找不到。第二,对找到的实例代码不熟悉,没有理解每一行代码的含义和作用。第三,不知道怎么输出结果,不会适当的断点。
经过一位同学的帮助,他帮我解决了以问题。最终运行成功。
Console.WriteLine("aaaaa"); //用于测试输出
System.Diagnostics.Process p = new System.Diagnostics.Process(); //开始系统进程
p.StartInfo = new System.Diagnostics.ProcessStartInfo(@"C:\Users\AMY\Desktop\test\crf_learn.exe"); //应用程序路径
p.StartInfo.Arguments = @" C:\Users\AMY\Desktop\test\template C:\Users\AMY\Desktop\test\train.data C:\Users\AMY\Desktop\test\model"; //参数输入
p.StartInfo.RedirectStandardOutput = true; //结果输出
p.StartInfo.UseShellExecute = false; //是否在shell中显示
p.Start();
//p.WaitForExit();
Console.WriteLine("nnnn");
p.Close() //要记得释放资源,不然程序会很慢很慢的。
Console.ReadKey();
dll文件在CRFs运行的时候是可以自己找到的,只要你把它和learn.exe文件放在一个文件夹里面。另外test的结果不能通过参数来重定向,所以,只能将输出流保存到字符串,再保存到txt文件。现阶段(2015.11.8)完整的代码为:
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.Diagnostics;
using System.IO;
private void start_CRFs_Click_1(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string TrainDirectory = Train_directoryT.Text;
string ArgumentsStr = tb_Arguments.Text;
string output = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(TrainDirectory + "crf_learn.exe");
p.StartInfo.Arguments = ArgumentsStr + " " + TrainDirectory + "template " + TrainDirectory + "train.data " + TrainDirectory + "model";
string argStr = p.StartInfo.Arguments;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
tbtrain_result.Text = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
// System.Threading.Thread.Sleep(50000); 最好能用多线程来处理
System.Diagnostics.Process p2 = new System.Diagnostics.Process();
p2.StartInfo = new System.Diagnostics.ProcessStartInfo(TrainDirectory + "crf_test.exe");//需要启动的程序名
p2.StartInfo.Arguments = " -m " + TrainDirectory + "model " + TrainDirectory + "test.data";
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.UseShellExecute = false;
p2.Start();
output = p2.StandardOutput.ReadToEnd();
TbTest_outputT.Text = output;
byte[] buffer = Encoding.Default.GetBytes(output);
File.WriteAllBytes(TrainDirectory + "output.txt", buffer);
p2.Close();
sw.Stop();
TimeSpan ts = sw.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
TimeSpanL.Text = "用时:" + elapsedTime;
}
一些输入输出定义的变量用的时候记得先定义好。
其实很多不会的知识点,或是一些知识点的细节都是百度到的,记得好的笔记真心让人受益匪浅。最后程序也算是成功的跑起来了,所以,我把学会的东西记下来,一来是可以日后自己复习,二来也可以跟大家一起分享
c# 调用 CRFs应用程序的更多相关文章
- java: Runtime和Process调用本机程序
java: Runtime和Process调用本机程序 调用纸牌程序,Process用来销毁程序 import java.io.IOException; public class RunTimeDem ...
- windows下调用外部exe程序 SHELLEXECUTEINFO
本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...
- js网页中调用本地应用程序
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- Unity开发Android应用程序:调用安卓应用程序功能
开发环境: Eclipse3.4 + adt12 + jdk6 + AndroidSDK2.2 Unity3.4 + windows7 测试设备: HTC Desire HD 本文要涉及到的几个重点问 ...
- RPC框架Thrift例子-PHP调用C++后端程序
更新 2016-02-22: Response对象不用主动创建. 前言 前段时间用了一下Facebook的开源RPC框架Thrift,做PHP客户端调用C++后端程序,真心觉得Thrift不错! 本文 ...
- MAC COCOA call command 调用终端控制台程序
MAC COCOA call command 调用终端控制台程序 STEP 1 先写一个C++ DOS程序 STEP2 使用NSTask来运行,然后用NSPipe和 NSData来接受运行的结果字符串 ...
- ubuntu下浏览器调用本地应用程序
ubunut下浏览器调用本地应用程序需要desktop文件和scheme协议的支持,和windows 的url protocol类似,只是注册协议的方式不一样. 首先是desktop文件,里面需要加入 ...
- c# 调用外部exe程序
c#调用外部exe程序,首先要 using System.Diagnostics; 然后开启一个新process System.Diagnostics.ProcessStartInfo p=null; ...
- html网页调用本地exe程序的实现方法:
html网页调用本地exe程序的实现方法:1.新建注册表具体文件: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\hhtpexe] [ ...
随机推荐
- java学习笔记(二)之数据部分
数据类型 java数据类型 基本数据类型 数值型 整型byte/short/int/long 浮点型/double/float 字符型char 布尔型boolean 取值true f ...
- div+css之清除浮动
当元素有浮动属性时,会对其父元素或后面的元素产生影响,会出现一个布局错乱的现象,可以通过清除浮动的方法来解决浮动的影响. 浮动的清理(clear): 值:none:默认值.允许两边都可以有浮动对象:l ...
- 黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性
<? /* * 参考资料: * http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm * http://www.phpddt ...
- yarn的调度策略
一. yarn的资源分配模型 无论先进先出调度器,容量调度器,还是公平调度器,他们的核心:资源分配模型是一样的. 调度器维护着多个队列的信息,用户可以向任意一个或多个队列提交job.每次NodeMan ...
- *.hbm.xml讲解
<!-- package声明pojo类所在的包,如果不写 那么在class中需要指明pojo类所在的包 schema指数据库模式 一个模式下可以有多张表 --> <hibernate ...
- hdu 5698 瞬间移动(排列组合)
这题刚看完,想了想,没思路,就题解了 = = 但不得不说,找到这个题解真的很强大,链接:http://blog.csdn.net/qwb492859377/article/details/514781 ...
- Git Step by Step
原文地址:http://www.cnblogs.com/wilber2013/category/643754.html 1.Git简介 2.Git本地仓库 3.Git对象模型 4.探索.git目录 5 ...
- 回调--一个经典例子让你彻彻底底理解java回调机制
本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 以前不理解什么叫回调 ...
- gridview--基本的gridview
GridView 元素距离设定 因为该设定比较简单 防止以后忘记 所以贴 供自己查阅 1. 布局:main.xml <?xml version="1.0" encoding= ...
- Apache2 CGI demo
1. 修改 httpd.conf 配置 <IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache2/cg ...