2018-07-13 12:21:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using EntityFrameworkCore.BootKit;
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2018-08-01 20:25:50 +00:00
|
|
|
|
using BotSharp.Core.Engines.BotSharp;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.WebHost
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class Startup
|
|
|
|
|
|
{
|
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2018-07-15 12:31:52 +00:00
|
|
|
|
services.AddCors();
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.RespectBrowserAcceptHeader = true;
|
|
|
|
|
|
}).AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
|
|
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
|
|
});
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var info = Configuration.GetSection("Swagger").Get<Info>();
|
|
|
|
|
|
c.SwaggerDoc(info.Version, info);
|
|
|
|
|
|
|
|
|
|
|
|
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml");
|
|
|
|
|
|
c.IncludeXmlComments(filePath);
|
|
|
|
|
|
});
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-15 12:31:52 +00:00
|
|
|
|
app.UseDefaultFiles();
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseSwagger(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var info = Configuration.GetSection("Swagger").Get<Info>();
|
|
|
|
|
|
|
|
|
|
|
|
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
|
|
|
|
|
|
c.ShowExtensions();
|
|
|
|
|
|
c.SwaggerEndpoint(Configuration.GetValue<String>("Swagger:Endpoint"), info.Title);
|
|
|
|
|
|
c.RoutePrefix = String.Empty;
|
|
|
|
|
|
c.DocumentTitle = info.Title;
|
|
|
|
|
|
c.InjectStylesheet(Configuration.GetValue<String>("Swagger:Stylesheet"));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
app.UseMvc();
|
|
|
|
|
|
|
|
|
|
|
|
Database.Configuration = Configuration;
|
|
|
|
|
|
Database.ContentRootPath = env.ContentRootPath;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
Database.Assemblies = Configuration.GetValue<String>("Assemblies").Split(',');
|
2018-08-01 20:25:50 +00:00
|
|
|
|
|
|
|
|
|
|
Runcmd();
|
|
|
|
|
|
|
|
|
|
|
|
var ai = new BotSharpAi();
|
|
|
|
|
|
ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725");
|
|
|
|
|
|
ai.Train();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Runcmd ()
|
|
|
|
|
|
{
|
|
|
|
|
|
string cmd = "/home/bolo/Desktop/BotSharp/TrainingFiles/crfsuite learn -m /home/bolo/Desktop/BotSharp/TrainingFiles/bolo.model /home/bolo/Desktop/BotSharp/TrainingFiles/1.txt";
|
|
|
|
|
|
System.Diagnostics.Process p = new System.Diagnostics.Process();
|
|
|
|
|
|
p.StartInfo.FileName = "sh";
|
|
|
|
|
|
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
|
|
|
|
|
|
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
|
|
|
|
|
|
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
|
|
|
|
|
|
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
|
|
|
|
|
|
p.StartInfo.CreateNoWindow = false;//不显示程序窗口
|
|
|
|
|
|
p.Start();//启动程序
|
|
|
|
|
|
|
|
|
|
|
|
//向cmd窗口发送输入信息
|
|
|
|
|
|
p.StandardInput.WriteLine(cmd + "&exit");
|
|
|
|
|
|
|
|
|
|
|
|
p.StandardInput.AutoFlush = false;
|
|
|
|
|
|
//p.StandardInput.WriteLine("exit");
|
|
|
|
|
|
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
|
|
|
|
|
|
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//获取cmd窗口的输出信息
|
|
|
|
|
|
string output = p.StandardOutput.ReadToEnd();
|
|
|
|
|
|
|
|
|
|
|
|
//StreamReader reader = p.StandardOutput;
|
|
|
|
|
|
//string line=reader.ReadLine();
|
|
|
|
|
|
//while (!reader.EndOfStream)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// str += line + " ";
|
|
|
|
|
|
// line = reader.ReadLine();
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
p.WaitForExit();//等待程序执行完退出进程
|
|
|
|
|
|
p.Close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(output);
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|