public PreviewLabeledExampleDto(JToken _json)
        {
            this.Id          = (long)_json["id"];
            this.IntentLabel = (_json["intentLabel"] ?? "").ToString();

            var entitiLabels = new List <PredictionEntityDto>();

            foreach (var entity in _json["entityPredictions"])
            {
                var dto = new PredictionEntityDto(entity);
                entitiLabels.Add(dto);
            }
            this.EntityLabels = entitiLabels;
        }
Ejemplo n.º 2
0
        public RobotBehavior(LuisResult luisResult, List <SynAppsIntentModel> synappsIntents, Dictionary <long, List <PredictionIntentModel> > _predictions = null, Dictionary <string, List <PredictionEntityModel> > _predictionEntities = null)
        {
            this.IntentName     = None;
            this.Score          = 0.0;
            this.FreeAction     = ErrorAction;
            this.LinkedEntities = None;
            this.LuisEntities   = new List <string>();
            this.IsContinues    = false;
            if (luisResult != null && luisResult.TopScoringIntent != null)
            {
                this.ExampleText = luisResult.OriginalQuery;
                this.IntentName  = luisResult.TopScoringIntent.Name;
                this.Score       = luisResult.TopScoringIntent.Score;
                var entities     = luisResult.GetAllEntities();
                var entityValues = entities.Select(entity => ToNarrow(Regex.Replace(Regex.Replace(entity.Value, " ", "", RegexOptions.Singleline), " ", "", RegexOptions.Singleline)));
                this.LuisEntities = entityValues.ToList();
                var targetsWithEntities = synappsIntents.Where(intent =>
                                                               intent.Name == this.IntentName &&
                                                               entityValues.Any(Regex.Replace(ToNarrow(intent.Entity.ToLower()), " ", "", RegexOptions.Singleline).Split(',').Contains)
                                                               ).ToList();
                targetsWithEntities = targetsWithEntities.OrderByDescending(intent =>
                                                                            entityValues.Count(intent.Entity.Split(',').Contains)
                                                                            ).ThenBy(intent =>
                                                                                     intent.Entity.Split(',').Count()
                                                                                     ).ToList();

                var resultWithEntities = getLinkedAction(targetsWithEntities, entities);
                if (resultWithEntities.Status == Status.Success)
                {
                    this.FreeAction     = resultWithEntities.FreeAction;
                    this.LinkedEntities = resultWithEntities.LinkedEntities;
                }
                else
                {
                    var targets = synappsIntents.Where(intent =>
                                                       intent.Name == this.IntentName &&
                                                       intent.Entity == ""
                                                       ).ToList();
                    var result = getLinkedAction(targets, entities);
                    if (result.Status == Status.Success)
                    {
                        this.FreeAction     = result.FreeAction;
                        this.LinkedEntities = result.LinkedEntities;
                    }
                    else
                    {
                        var preview = new PreviewLabeledExampleDto();
                        preview.IntentLabel = this.IntentName;
                        var entityLabels = new List <PredictionEntityDto>();
                        foreach (var e in entities)
                        {
                            var entityLabel = new PredictionEntityDto();
                            entityLabel.EntityName  = e.Name;
                            entityLabel.EntityValue = e.Value;

                            entityLabels.Add(entityLabel);
                        }
                        preview.EntityLabels = entityLabels;

                        this.FreeAction     = Predictions(preview, _predictions, _predictionEntities, targetsWithEntities) ?? this.FreeAction;
                        this.LinkedEntities = result.LinkedEntities;
                    }
                }

                string talk = "";
                this.IsContinues = (bool)(this.FreeAction["IsContinuance"] ?? false);
                if (this.IsContinues)
                {
                    talk = (this.FreeAction["Talk1"] ?? "").ToString();
                }
                else
                {
                    talk = (this.FreeAction["Talk"] ?? "").ToString();
                }
                if (talk != "")
                {
                    talk = Regex.Replace(talk, "\\\\rspd=.+?\\\\", "", RegexOptions.Singleline);
                    talk = Regex.Replace(talk, "\\\\vct=.+?\\\\", "", RegexOptions.Singleline);
                    this.NaturalTalkText = talk.Trim();
                }
            }
        }