Exemple #1
0
        public void SaveReaction(LuisExample example, string action, long _exampleTextId)
        {
            string entity_values = "";

            foreach (var label in example.EntityLabels)
            {
                var value = example.Text.Substring(label.StartCharIndex, (label.EndCharIndex - label.StartCharIndex + 1)) + ",";
                entity_values += value;
            }
            entity_values = entity_values.Trim(',');

            var model = SynAppsIntentModel.New();

            model.SynAppsDeviceId = this.DeviceId;
            model.Name            = example.IntentName;
            model.Entity          = entity_values;
            model.ReactionBody    = "{\"Talk\": \"" + action + "\"}";
            model.IsSynAppsLinked = false;
            model.Save();

            if (_exampleTextId > 0)
            {
                var m = ConversationHistoryModel.FindById(_exampleTextId);
                m.ToMessage       = action;
                m.IsSynAppsLinked = false;
                m.Save();
            }
        }
Exemple #2
0
        public ApiResult AdvancedLearning(string learningJson, string exampleText, long _exampleTextId)
        {
            var result = new ApiResult {
                StatusCode = StatusCode.Success, Message = ""
            };

            try
            {
                var learningObject = JsonConvert.DeserializeObject <JObject>(learningJson);
                var luisExample    = new LuisExample();
                luisExample.Text       = exampleText;
                luisExample.IntentName = (learningObject["intents"] ?? "").ToString();
                if (luisExample.IntentName == "")
                {
                    throw new Exception("intent must have some value.");
                }
                var action = (learningObject["action"] ?? "").ToString();
                if (action == "")
                {
                    throw new Exception("action must have some value.");
                }
                var entityLabels = new List <LuisEntityLabel>();
                foreach (var entity in learningObject["entities"])
                {
                    var luisEntityLabel = new LuisEntityLabel();
                    luisEntityLabel.EntityName = (entity["name"] ?? "").ToString();
                    var entityValue = (entity["value"] ?? "").ToString();
                    if (luisEntityLabel.EntityName == "" || entityValue == "")
                    {
                        throw new Exception("entity name and value must have some value.");
                    }
                    if (!exampleText.Contains(entityValue))
                    {
                        throw new Exception("json text must contain entity value in example text.");
                    }

                    luisEntityLabel.StartCharIndex = exampleText.IndexOf(entityValue);
                    luisEntityLabel.EndCharIndex   = luisEntityLabel.StartCharIndex + entityValue.Length - 1;

                    entityLabels.Add(luisEntityLabel);
                }
                luisExample.EntityLabels = entityLabels;

                result = AddExample(luisExample);
                if (result.StatusCode == StatusCode.Success)
                {
                    SaveReaction(luisExample, action, _exampleTextId);
                }
            }
            catch (Exception e)
            {
                result.StatusCode = StatusCode.Error;
                result.Message    = e.StackTrace;
                Trace.TraceError(e.Message);
                Trace.TraceError(e.StackTrace);
            }

            return(result);
        }
Exemple #3
0
        public ApiResult AddExample(LuisExample example)
        {
            var result = new ApiResult {
                StatusCode = StatusCode.Success, Message = ""
            };

            byte[] payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(example, new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
            var content = new ByteArrayContent(payload);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = this.httpClient.PostAsync($"{EndPoint}{this.luisAppId}/versions/{this.luisVersionId}/example", content).Result;

            if (!response.IsSuccessStatusCode)
            {
                result.StatusCode = (int)response.StatusCode;
                result.Message    = response.ReasonPhrase;
            }

            return(result);
        }