BotSharp/BotSharp.Core/Agents/AgentExtension.cs

122 lines
4.4 KiB
C#
Raw Normal View History

2018-04-02 22:42:11 +00:00
using BotSharp.Core.Adapters.Rasa;
using BotSharp.Core.Entities;
using BotSharp.Core.Expressions;
2018-03-28 22:08:49 +00:00
using BotSharp.Core.Intents;
using BotSharp.Core.Models;
2017-12-30 20:26:35 +00:00
using EntityFrameworkCore.BootKit;
2017-12-18 13:31:15 +00:00
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2018-03-28 22:08:49 +00:00
namespace BotSharp.Core.Agents
2017-12-18 13:31:15 +00:00
{
public static class AgentExtension
{
2018-01-02 18:05:35 +00:00
/// <summary>
/// Get agent header row from Agent table
/// </summary>
/// <param name="dc"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public static Agent Agent(this Database dc, string agentId)
{
return dc.Table<Agent>().Find(agentId);
}
public static String CreateEntity(this Agent agent, Entity entity, Database dc)
2017-12-30 20:26:35 +00:00
{
return entity.Id;
}
public static RasaTrainingData GrabCorpus(this Agent agent, Database dc, List<AIContext> ctx)
2017-12-18 13:31:15 +00:00
{
var trainingData = new RasaTrainingData
{
2018-04-02 22:42:11 +00:00
Entities = new List<RasaTraningEntity>(),
2018-03-28 22:08:49 +00:00
UserSays = new List<RasaIntentExpression>()
2017-12-18 13:31:15 +00:00
};
2018-04-02 22:42:11 +00:00
var expressParts = new List<IntentExpressionPart>();
var intents = dc.Table<Intent>()
.Include(x => x.Contexts)
2018-03-28 22:08:49 +00:00
.Include(x => x.UserSays).ThenInclude(say => say.Data)
.Where(x => x.UserSays.Count > 0)
.ToList();
2017-12-18 13:31:15 +00:00
var contexts = ctx.OrderBy(x => x.Name).Select(x => x.Name.ToLower()).ToList();
2017-12-18 13:31:15 +00:00
// search all potential intents which input context included in contexts
intents = intents.Where(it =>
{
if (contexts.Count == 0)
{
return it.Contexts.Count() == 0;
}
else
{
return it.Contexts.Count() > 0 && it.Contexts.Count(x => contexts.Contains(x.Name.ToLower())) == it.Contexts.Count;
}
}).OrderByDescending(x => x.Contexts.Count).ToList();
intents.ForEach(intent =>
{
2018-03-28 22:08:49 +00:00
intent.UserSays.ForEach(exp =>
{
var say = new RasaIntentExpression
2017-12-18 13:31:15 +00:00
{
Intent = intent.Name,
2018-03-28 22:08:49 +00:00
Text = String.Join("", exp.Data.OrderBy(x => x.UpdatedTime).Select(x => x.Text)),
};
// convert entity format
exp.Data.Where(x => !String.IsNullOrEmpty(x.Meta))
.ToList()
.ForEach(x =>
{
int start = say.Text.IndexOf(x.Text);
var part = new RasaIntentExpressionPart
{
Value = x.Text,
Entity = x.Alias,
Start = start,
End = start + x.Text.Length
};
2018-04-02 22:42:11 +00:00
if (say.Entities == null) say.Entities = new List<RasaIntentExpressionPart>();
2018-03-28 22:08:49 +00:00
say.Entities.Add(part);
2018-04-02 22:42:11 +00:00
// assemble entity synonmus
if (!trainingData.Entities.Any(y => y.EntityType == x.Alias && y.EntityValue == x.Text))
{
var allSynonyms = (from e in dc.Table<Entity>()
join ee in dc.Table<EntityEntry>() on e.Id equals ee.EntityId
join ees in dc.Table<EntityEntrySynonym>() on ee.Id equals ees.EntityEntryId
where e.Name == x.Alias && ee.Value == x.Text & ees.Synonym != x.Text
select ees.Synonym ).ToList();
var te = new RasaTraningEntity
{
EntityType = x.Alias,
EntityValue = x.Text,
Synonyms = allSynonyms
};
trainingData.Entities.Add(te);
}
2018-03-28 22:08:49 +00:00
});
trainingData.UserSays.Add(say);
});
2017-12-18 13:31:15 +00:00
});
2018-04-02 22:42:11 +00:00
2017-12-18 13:31:15 +00:00
return trainingData;
}
}
}