|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using BotSharp.Platform.Abstraction; |
| 8 | +using BotSharp.Platform.Models; |
| 9 | +using BotSharp.Platform.Models.Intents; |
| 10 | +using BotSharp.Platform.Rasa.Models; |
| 11 | +using Newtonsoft.Json; |
| 12 | + |
| 13 | +namespace BotSharp.Platform.Rasa |
| 14 | +{ |
| 15 | + public class AgentImporterInRasa<TAgent> : IAgentImporter<TAgent> where TAgent : AgentModel, new() |
| 16 | + { |
| 17 | + public string AgentDir { get; set; } |
| 18 | + |
| 19 | + public async Task<TAgent> LoadAgent(AgentImportHeader agentHeader) |
| 20 | + { |
| 21 | + var agent = new TAgent |
| 22 | + { |
| 23 | + Id = agentHeader.Id, |
| 24 | + Name = agentHeader.Name |
| 25 | + }; |
| 26 | + |
| 27 | + return agent; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task LoadBuildinEntities(TAgent agent) |
| 31 | + { |
| 32 | + agent.Intents.ForEach(intent => |
| 33 | + { |
| 34 | + /*if (intent.UserSays != null) |
| 35 | + { |
| 36 | + intent.UserSays.ForEach(us => |
| 37 | + { |
| 38 | + us.Data.Where(data => data.Meta != null) |
| 39 | + .ToList() |
| 40 | + .ForEach(data => |
| 41 | + { |
| 42 | + LoadBuildinEntityTypePerUserSay(agent, data); |
| 43 | + }); |
| 44 | + }); |
| 45 | + }*/ |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private void LoadBuildinEntityTypePerUserSay(TAgent agent, IntentExpressionPart data) |
| 50 | + { |
| 51 | + /*var existedEntityType = agent.Entities.FirstOrDefault(x => x.Name == data.Meta); |
| 52 | +
|
| 53 | + if (existedEntityType == null) |
| 54 | + { |
| 55 | + existedEntityType = new EntityType |
| 56 | + { |
| 57 | + Name = data.Meta, |
| 58 | + Entries = new List<EntityEntry>(), |
| 59 | + IsOverridable = true |
| 60 | + }; |
| 61 | +
|
| 62 | + agent.Entities.Add(existedEntityType); |
| 63 | + } |
| 64 | +
|
| 65 | + var entries = existedEntityType.Entries.Select(x => x.Value.ToLower()).ToList(); |
| 66 | + if (!entries.Contains(data.Text.ToLower())) |
| 67 | + { |
| 68 | + existedEntityType.Entries.Add(new EntityEntry |
| 69 | + { |
| 70 | + Value = data.Text, |
| 71 | + Synonyms = new List<EntrySynonym> |
| 72 | + { |
| 73 | + new EntrySynonym |
| 74 | + { |
| 75 | + Synonym = data.Text |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + }*/ |
| 80 | + } |
| 81 | + |
| 82 | + public async Task LoadCustomEntities(TAgent agent) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + public async Task LoadIntents(TAgent agent) |
| 87 | + { |
| 88 | + string data = File.ReadAllText(Path.Combine(AgentDir, "corpus.json")); |
| 89 | + var rasa = JsonConvert.DeserializeObject<RasaAgentImportModel>(data); |
| 90 | + |
| 91 | + agent.Intents = rasa.Data.Intents; |
| 92 | + agent.Entities = rasa.Data.Entities; |
| 93 | + } |
| 94 | + |
| 95 | + private void ImportIntentUserSays(RasaIntentExpression intent, List<RasaIntentExpression> sentences) |
| 96 | + { |
| 97 | + var intents = new List<RasaIntentExpression>(); |
| 98 | + |
| 99 | + var userSays = sentences.Where(x => x.Intent == intent.Intent).ToList(); |
| 100 | + |
| 101 | + userSays.ForEach(say => |
| 102 | + { |
| 103 | + var expression = new IntentExpression(); |
| 104 | + |
| 105 | + say.Entities = say.Entities.OrderBy(x => x.Start).ToList(); |
| 106 | + |
| 107 | + expression.Data = new List<IntentExpressionPart>(); |
| 108 | + |
| 109 | + int pos = 0; |
| 110 | + for (int entityIdx = 0; entityIdx < say.Entities.Count; entityIdx++) |
| 111 | + { |
| 112 | + var entity = say.Entities[entityIdx]; |
| 113 | + |
| 114 | + // previous |
| 115 | + if (entity.Start > 0) |
| 116 | + { |
| 117 | + expression.Data.Add(new IntentExpressionPart |
| 118 | + { |
| 119 | + Text = say.Text.Substring(pos, entity.Start - pos), |
| 120 | + Start = pos |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + // self |
| 125 | + expression.Data.Add(new IntentExpressionPart |
| 126 | + { |
| 127 | + Alias = entity.Entity, |
| 128 | + Meta = entity.Entity, |
| 129 | + Text = say.Text.Substring(entity.Start, entity.Value.Length), |
| 130 | + Start = entity.Start |
| 131 | + }); |
| 132 | + |
| 133 | + pos = entity.End + 1; |
| 134 | + |
| 135 | + if (pos < say.Text.Length && entityIdx == say.Entities.Count - 1) |
| 136 | + { |
| 137 | + // end |
| 138 | + expression.Data.Add(new IntentExpressionPart |
| 139 | + { |
| 140 | + Text = say.Text.Substring(pos), |
| 141 | + Start = pos |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (say.Entities.Count == 0) |
| 147 | + { |
| 148 | + expression.Data.Add(new IntentExpressionPart |
| 149 | + { |
| 150 | + Text = say.Text.Substring(pos) |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + int second = 0; |
| 155 | + expression.Data.ForEach(x => x.UpdatedTime = DateTime.UtcNow.AddSeconds(second++)); |
| 156 | + |
| 157 | + intents.Add(say); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + /*public void AssembleTrainData(TAgent agent) |
| 162 | + { |
| 163 | + // convert agent to training corpus |
| 164 | + agent.Corpus = new TrainingCorpus |
| 165 | + { |
| 166 | + Entities = new List<TrainingEntity>(), |
| 167 | + UserSays = new List<TrainingIntentExpression<TrainingIntentExpressionPart>>() |
| 168 | + }; |
| 169 | +
|
| 170 | + agent.Intents.ForEach(intent => |
| 171 | + { |
| 172 | + intent.UserSays.ForEach(say => { |
| 173 | + agent.Corpus.UserSays.Add(new TrainingIntentExpression<TrainingIntentExpressionPart> |
| 174 | + { |
| 175 | + Intent = intent.Name, |
| 176 | + Text = String.Join("", say.Data.Select(x => x.Text)), |
| 177 | + Entities = say.Data.Where(x => !String.IsNullOrEmpty(x.Meta)) |
| 178 | + .Select(x => new TrainingIntentExpressionPart |
| 179 | + { |
| 180 | + Value = x.Text, |
| 181 | + Entity = x.Meta, |
| 182 | + Start = x.Start |
| 183 | + }) |
| 184 | + .ToList() |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + }*/ |
| 189 | + } |
| 190 | +} |
0 commit comments