using BotSharp.Core.Abstractions; using BotSharp.NLP; using BotSharp.NLP.Classify; using BotSharp.NLP.Txt2Vec; using BotSharp.Platform.Models; using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BotSharp.Core.Engines.BotSharp { public class BotSharpIntentClassifier : INlpTrain, INlpPredict { public IConfiguration Configuration { get; set; } public PipeSettings Settings { get; set; } private ClassifierFactory _classifier; public async Task Train(AgentBase agent, NlpDoc doc, PipeModel meta) { Init(meta); var sentences = doc.Sentences.Select(x => new Sentence { Label = x.Intent.Label, Text = x.Text, Words = x.Tokens }).ToList(); _classifier.Train(sentences); Console.WriteLine($"Saved model to {Settings.ModelDir}"); return true; } public async Task Predict(AgentBase agent, NlpDoc doc, PipeModel meta) { Init(meta); var sentence = doc.Sentences.Select(s => new Sentence { Text = s.Text, Words = s.Tokens }).First(); var result = _classifier.Classify(sentence); doc.Sentences[0].Intent = new TextClassificationResult { Classifier = "BotSharpIntentClassifier", Label = result.First().Item1, Confidence = (decimal)result.First().Item2 }; return true; } private void Init(PipeModel meta) { if (_classifier == null) { meta.Model = "intent.model"; var options = new ClassifyOptions { ModelFilePath = Path.Combine(Settings.ModelDir, meta.Model), ModelDir = Settings.ModelDir, ModelName = meta.Model }; _classifier = new ClassifierFactory(options, SupportedLanguage.English); string classifierName = Configuration.GetValue($"classifer"); _classifier.GetClassifer(classifierName); } } } }