Merge pull request #2 from PppBr/master

add sebis agent
This commit is contained in:
Oceania 2018-08-03 17:22:02 -05:00 committed by GitHub
commit 1c3c49e501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 4487 additions and 4 deletions

View file

@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BotSharp.Core.Adapters.Dialogflow;
using BotSharp.Core.Adapters.Sebis;
using BotSharp.Core.Agents;
using BotSharp.Core.Entities;
using BotSharp.Core.Intents;
using BotSharp.Core.Models;
using DotNetToolkit;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BotSharp.Core.Engines
{
/// <summary>
/// Import agent from Dialogflow
/// </summary>
public class AgentImporterInSebis : IAgentImporter
{
/// <summary>
/// Load agent meta
/// </summary>
/// <param name="agentName"></param>
/// <param name="agentDir"></param>
/// <returns></returns>
public Agent LoadAgent(AgentImportHeader agentHeader, string agentDir)
{
// load agent profile
string data = File.ReadAllText(Path.Join(agentDir, "Sebis", $"{agentHeader.Name}{Path.DirectorySeparatorChar}agent.json"));
var agent = JsonConvert.DeserializeObject<SebisAgent>(data);
agent.Name = agentHeader.Name;
agent.Id = agentHeader.Id;
var result = agent.ToObject<Agent>();
result.ClientAccessToken = agentHeader.ClientAccessToken;
result.DeveloperAccessToken = agentHeader.DeveloperAccessToken;
if(agentHeader.UserId != null)
{
result.UserId = agentHeader.UserId;
}
return result;
}
public void LoadCustomEntities(Agent agent, string agentDir)
{
agent.Entities = new List<EntityType>();
}
public void LoadIntents(Agent agent, string agentDir)
{
string data = File.ReadAllText(Path.Join(agentDir, "Sebis", $"{agent.Name}{Path.DirectorySeparatorChar}corpus.json"));
var sentences = JsonConvert.DeserializeObject<SebisAgent>(data).Sentences;
agent.Intents = sentences.Select(x => x.Name).Distinct().Select(x => new Intent{Name = x}).ToList();
agent.Intents.ForEach(intent => {
intent.UserSays = new List<IntentExpression>();
var userSays = sentences.Where(x => x.Name == intent.Name).ToList();
userSays.ForEach(say =>
{
var expression = new IntentExpression();
expression.Data = new List<IntentExpressionPart>();
for(int index = 0; index < say.Entities.Count; index++)
{
}
intent.UserSays.Add(expression);
});
});
}
private Intent ImportIntentUserSays(Agent agent, Intent intent, string fileName)
{
// void id confict
intent.Id = Guid.NewGuid().ToString();
intent.Name = intent.Name.Replace("/", "_");
// load user expressions
string expressionFileName = fileName.Replace(intent.Name, $"{intent.Name}_usersays_{agent.Language}");
if (File.Exists(expressionFileName))
{
string expressionJson = File.ReadAllText($"{expressionFileName}");
intent.UserSays = JsonConvert.DeserializeObject<List<IntentExpression>>(expressionJson);
}
return null;
}
public void LoadBuildinEntities(Agent agent, string agentDir)
{
agent.Intents.ForEach(intent =>
{
if (intent.UserSays != null)
{
intent.UserSays.ForEach(us =>
{
us.Data.Where(data => data.Meta != null)
.ToList()
.ForEach(data =>
{
LoadBuildinEntityTypePerUserSay(agent, data);
});
});
}
});
}
private void LoadBuildinEntityTypePerUserSay(Agent agent, IntentExpressionPart data)
{
var existedEntityType = agent.Entities.FirstOrDefault(x => x.Name == data.Meta);
if (existedEntityType == null)
{
existedEntityType = new EntityType
{
Name = data.Meta,
Entries = new List<EntityEntry>(),
IsOverridable = true
};
agent.Entities.Add(existedEntityType);
}
var entries = existedEntityType.Entries.Select(x => x.Value.ToLower()).ToList();
if (!entries.Contains(data.Text.ToLower()))
{
existedEntityType.Entries.Add(new EntityEntry
{
Value = data.Text,
Synonyms = new List<EntrySynonym>
{
new EntrySynonym
{
Synonym = data.Text
}
}
});
}
}
}
public class Sebis
{
public string Name { get; set; }
public string Desc { get; set; }
public string Lang { get; set; }
public List<TrainingIntentExpression<TrainingIntentExpressionPart>> Sentences { get; set; }
}
}

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BotSharp.Core.Adapters.Sebis
{
public class SebisAgent
{
public String Id { get; set; }
public String Name { get; set; }
[JsonProperty("desc")]
public String Description { get; set; }
[JsonProperty("lang")]
public String Language { get; set; }
public List<SebisIntent> Sentences { get; set; }
}
}

View file

@ -0,0 +1,16 @@
using BotSharp.Core.Intents;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Adapters.Sebis
{
public class SebisIntent
{
public string Text { get; set; }
[JsonProperty("intent")]
public string Name { get; set; }
public List<SebisIntentExpressionPart> Entities { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using BotSharp.Core.Engines;
namespace BotSharp.Core.Adapters.Sebis
{
public class SebisIntentExpression : TrainingIntentExpression<SebisIntentExpressionPart>
{
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using BotSharp.Core.Engines;
using Newtonsoft.Json;
namespace BotSharp.Core.Adapters.Sebis
{
public class SebisIntentExpressionPart : TrainingIntentExpressionPart
{
[JsonProperty("stop")]
public new int End { get; set; }
[JsonProperty("text")]
public new String Value { get; set; }
}
}

View file

@ -1,5 +1,6 @@
using BotSharp.Core.Agents; using BotSharp.Core.Agents;
using BotSharp.Core.Engines; using BotSharp.Core.Engines;
using BotSharp.Core.Engines.BotSharp;
using BotSharp.Core.Models; using BotSharp.Core.Models;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -29,9 +30,9 @@ namespace BotSharp.RestApi
var botsHeaderFilePath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), $"DbInitializer{Path.DirectorySeparatorChar}Agents{Path.DirectorySeparatorChar}agents.json"); var botsHeaderFilePath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), $"DbInitializer{Path.DirectorySeparatorChar}Agents{Path.DirectorySeparatorChar}agents.json");
var agents = JsonConvert.DeserializeObject<List<AgentImportHeader>>(System.IO.File.ReadAllText(botsHeaderFilePath)); var agents = JsonConvert.DeserializeObject<List<AgentImportHeader>>(System.IO.File.ReadAllText(botsHeaderFilePath));
var rasa = new RasaAi(); var rasa = new BotSharpAi();
var agentHeader = agents.First(x => x.Id == agentId); var agentHeader = agents.First(x => x.Id == agentId);
rasa.RestoreAgent<AgentImporterInDialogflow>(agentHeader); rasa.RestoreAgent<AgentImporterInSebis>(agentHeader);
return Ok(); return Ok();
} }

View file

@ -0,0 +1,4 @@
{
"description": "NLU Evaluation Corpora",
"language": "en"
}

File diff suppressed because it is too large Load diff

View file

@ -13,5 +13,13 @@
"AccessToken": "EAAJym8gGQFMBAPnsxTw6rZBE2WVfYtCJeGkCQ2NZC3VbQd45SyUlXjjgUf1gAkCOWq7v0blxTb1xLZAeMAXYhQj3btcMlMO9YbG7StMyx8ussz5TnD1tjjIZCye5u66PZAuFxSyIPXtTCin8GPiJ19cYB8ZCyH3rr5sro7OxUSyAZDZD" "AccessToken": "EAAJym8gGQFMBAPnsxTw6rZBE2WVfYtCJeGkCQ2NZC3VbQd45SyUlXjjgUf1gAkCOWq7v0blxTb1xLZAeMAXYhQj3btcMlMO9YbG7StMyx8ussz5TnD1tjjIZCye5u66PZAuFxSyIPXtTCin8GPiJ19cYB8ZCyH3rr5sro7OxUSyAZDZD"
} }
] ]
},
{
"Id": "bff7605c-3db5-44dc-9ba7-1c9be2832318",
"Name": "Airport",
"UserId": "8da9e1e0-42dc-420a-8016-79b04c1297d0",
"ClientAccessToken": "6ba8a06865944f14981ce18d229283f5",
"DeveloperAccessToken": "f12fbdb0da5a4616b18fa7582d32f6e3",
"Integrations": []
} }
] ]

View file

@ -14,6 +14,8 @@ using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.Swagger;
using BotSharp.Core.Engines.BotSharp; using BotSharp.Core.Engines.BotSharp;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace BotSharp.WebHost namespace BotSharp.WebHost
{ {
@ -114,7 +116,6 @@ namespace BotSharp.WebHost
loader.Load(); loader.Load();
/*Runcmd(); /*Runcmd();
var ai = new BotSharpAi(); var ai = new BotSharpAi();
ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725"); ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725");
ai.Train();*/ ai.Train();*/
@ -160,4 +161,4 @@ namespace BotSharp.WebHost
Console.WriteLine(output); Console.WriteLine(output);
} }
} }
} }