intent accuray up to 0.41.

This commit is contained in:
Esther2013 2018-09-13 07:37:26 -05:00
parent a8c5242c7b
commit a173569681
5 changed files with 83 additions and 6 deletions

View file

@ -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<Tuple<AIRequest, string>> 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<Tuple<AIRequest, string>>();
agent.Corpus.UserSays.ForEach(intent =>
{
Samples.Add(new Tuple<AIRequest, string>(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;
}
}
}

View file

@ -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);

View file

@ -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;

View file

@ -40,12 +40,12 @@ namespace BotSharp.NLP.Featuring
}
public List<string> Features()
public List<string> 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;

View file

@ -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;