diff --git a/BotSharp.Core.UnitTest/Performance/Spotify.cs b/BotSharp.Core.UnitTest/Performance/Spotify.cs new file mode 100644 index 00000000..236d0c3f --- /dev/null +++ b/BotSharp.Core.UnitTest/Performance/Spotify.cs @@ -0,0 +1,77 @@ +using BotSharp.Core.Agents; +using BotSharp.Core.Engines; +using BotSharp.Core.Engines.BotSharp; +using BotSharp.Core.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace BotSharp.Core.UnitTest.Performance +{ + [TestClass] + public class Spotify : TestEssential + { + private List> Samples; + private IBotPlatform _platform; + + [TestMethod] + public void IntentAccuracy() + { + int correct = 0; + var agent = LoadAgent(); + + for(int i = 0; i < Samples.Count; i++) + { + try + { + var aIResponse = _platform.TextRequest(Samples[i].Item1); + if (aIResponse.Result.Metadata.IntentName == Samples[i].Item2) + { + correct++; + } + } + catch (Exception) + { + + } + + } + + double accuracy = correct / (Samples.Count + 0.0); + } + + private Agent LoadAgent() + { + _platform = new BotSharpAi(); + + // Load agent + var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", "Spotify"); + string model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last(); + var modelPath = Path.Combine(projectPath, model); + var agent = _platform.LoadAgentFromFile(modelPath); + + // Init samples + Samples = new List>(); + agent.Corpus.UserSays.ForEach(intent => + { + Samples.Add(new Tuple(new AIRequest + { + AgentDir = projectPath, + Model = model, + Query = new String[] + { + intent.Text + } + }, intent.Intent)); + }); + + var samples = String.Join("\r\n", Samples.Select(x => $"__label__{x.Item2} {x.Item1.Query[0]}").ToList()); + + + return agent; + } + } +} diff --git a/BotSharp.Core.UnitTest/TestEssential.cs b/BotSharp.Core.UnitTest/TestEssential.cs index 2c42fdf8..f6257b45 100644 --- a/BotSharp.Core.UnitTest/TestEssential.cs +++ b/BotSharp.Core.UnitTest/TestEssential.cs @@ -18,9 +18,9 @@ namespace BotSharp.Core.UnitTest public TestEssential() { contentRoot = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}BotSharp.WebHost{Path.DirectorySeparatorChar}"; - + contentRoot = Path.GetFullPath(contentRoot); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); - var settings = Directory.GetFiles(contentRoot + $"Settings{Path.DirectorySeparatorChar}", "*.json"); + var settings = Directory.GetFiles(contentRoot + $"..{Path.DirectorySeparatorChar}Settings{Path.DirectorySeparatorChar}", "*.json"); settings.ToList().ForEach(setting => { configurationBuilder.AddJsonFile(setting, optional: false, reloadOnChange: true); diff --git a/BotSharp.NLP/Classify/NaiveBayesClassifier.cs b/BotSharp.NLP/Classify/NaiveBayesClassifier.cs index 38f28d7b..610628e4 100644 --- a/BotSharp.NLP/Classify/NaiveBayesClassifier.cs +++ b/BotSharp.NLP/Classify/NaiveBayesClassifier.cs @@ -57,7 +57,7 @@ namespace BotSharp.NLP.Classify var tfidf = new TfIdfFeatureExtractor(); tfidf.Sentences = sentences; tfidf.CalBasedOnCategory(); - var keyWords = tfidf.Features(); + var keyWords = tfidf.Keywords(); string keywords2 = String.Join(",", keyWords.ToArray()); var encoder = new OneHotEncoder(); encoder.Sentences = sentences; diff --git a/BotSharp.NLP/Featuring/TfIdfFeatureExtractor.cs b/BotSharp.NLP/Featuring/TfIdfFeatureExtractor.cs index 8f3ccf77..ce709549 100644 --- a/BotSharp.NLP/Featuring/TfIdfFeatureExtractor.cs +++ b/BotSharp.NLP/Featuring/TfIdfFeatureExtractor.cs @@ -40,12 +40,12 @@ namespace BotSharp.NLP.Featuring } - public List Features() + public List Keywords() { var tfs2 = tfs.OrderByDescending(x => x.Item2) .Select(x => x.Item1) .Distinct() - .Take(Sentences.Count / Categories.Count) + .Take((int)Math.Floor(Sentences.Count / Categories.Count * 1.5)) .ToList(); return tfs2; diff --git a/BotSharp.NLP/Txt2Vec/OneHotEncoder.cs b/BotSharp.NLP/Txt2Vec/OneHotEncoder.cs index 510a37d9..5cf83cb5 100644 --- a/BotSharp.NLP/Txt2Vec/OneHotEncoder.cs +++ b/BotSharp.NLP/Txt2Vec/OneHotEncoder.cs @@ -49,7 +49,7 @@ namespace BotSharp.NLP.Txt2Vec { if (Words == null) { - // Words = "shuffle,pause,resume,next,stop,previous,continue,mode,repeat,back,music,play,enough,off,them,playlist,skip,restart,favourites,on,add,go,again,turn,save,my,station,favourite,start,by,playing,please,now,running,move".Split(',').ToList(); + Words = "shuffle,pause,resume,next,stop,previous,continue,mode,repeat,back,music,play,enough,off,them,playlist,skip,restart,favourites,on,add,go,again,turn,save,my,station,favourite,start,by,playing,please,now,running,move,gym,yoga,backward,one,favorites,mark,as,remember,fave,what,forward,me,and,could,once,more,can".Split(',').ToList(); } return Words;