using BotSharp.Core.Abstractions; using BotSharp.Core.Agents; using BotSharp.Core.Intents; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BotSharp.Core.Engines.SpaCy { public class SpaCyTextCategorizer : INlpPipeline { public IConfiguration Configuration { get; set; } public PipeSettings Settings { get; set; } public async Task Train(Agent agent, NlpDoc doc, PipeModel meta) { //var input = new List>(); var texts = new List(); var golds = new List(); List intentNames = agent.Intents.Select(x => x.Name).Distinct().ToList(); agent.Intents.ForEach(intent => { intent.UserSays.ForEach(userSay => { var text = String.Join(string.Empty, userSay.Data.Select(say => say.Text)); var dim = JObject.FromObject(new { }); intentNames.ForEach(name => { dim[name] = (intent.Name == name) ? 1 : 0; }); //input.Add(new Tuple(text, JObject.FromObject(new { Cats = dim }))); texts.Add(text); golds.Add(JObject.FromObject(new { cats = dim })); }); }); var client = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value); var request = new RestRequest("textcategorizer", Method.POST); request.RequestFormat = DataFormat.Json; request.AddParameter("application/json", JsonConvert.SerializeObject(new { Texts = texts.Take(2), Golds = golds.Take(2), Labels = intentNames }), ParameterType.RequestBody); var response = client.Execute(request); return true; } public async Task Predict(Agent agent, NlpDoc doc, PipeModel meta) { return true; } public class Result { public String ModelName { get; set; } } } }