예제 #1
0
        /// <summary>
        /// Adds a ListEntity to an existing Luis instance.
        /// The name of the ListEntity has to be unused in the instance.
        /// Elsewhere this method has no effect.
        /// </summary>
        /// <param name="_this">The Luis service definition.</param>
        /// <param name="name">The name of the ListEntity.</param>
        /// <param name="sublistsWithCanonicalAndList">The entries of the ListEntity. The first value of tuple defines the canonical string. The second one the synonyms.</param>
        /// <returns>The Id of the newly created ListEntity.</returns>
        public static string AddListEntity(this LuisServiceDefinition _this, string name, List <Tuple <string, List <string> > > sublistsWithCanonicalAndList)
        {
            string request = $"https://{_this.Region}.api.cognitive.microsoft.com/luis/api/v2.0/apps/{_this.AppId}/versions/{_this.Version}/closedlists";

            ListEntity le = new ListEntity
            {
                Name     = name,
                Sublists = new List <Dictionary <string, object> >()
            };

            foreach (var sl in sublistsWithCanonicalAndList)
            {
                Dictionary <string, object> element = new Dictionary <string, object>
                {
                    { "canonicalForm", sl.Item1 },
                    { "list", sl.Item2 }
                };
                le.Sublists.Add(element);
            }

            string json = JsonConvert.SerializeObject(le);

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _this.AuthoringKey);
                var content  = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
                var response = client.PostAsync(request, content).GetAwaiter().GetResult();
                return(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
            }
        }
예제 #2
0
 /// <summary>
 /// Creates a classifier with Luis.
 /// </summary>
 /// <param name="lsd">Definition of a Luis service.</param>
 /// <param name="trySpellcheck">Indicates if spellchecker is on or off.</param>
 public LuisClassifier(LuisServiceDefinition lsd, bool trySpellcheck = true)
 {
     _withoutCorrection = new LuisRecognizer(
         new LuisRecognizerOptionsV2(new LuisApplication(lsd.GetLuisService()))
     {
         PredictionOptions = new LuisPredictionOptions()
         {
             IncludeAllIntents = true
         }
     }
         );
     if (trySpellcheck && lsd.SpellCheckerKey != null)
     {
         _withCorrection = new LuisRecognizer(
             new LuisRecognizerOptionsV2(new LuisApplication(lsd.GetLuisService()))
         {
             PredictionOptions = lsd.GetPredictOpts()
         }
             );
     }
 }
예제 #3
0
        /// <summary>
        /// Extracts the intents of a Luis instance.
        /// </summary>
        /// <param name="_this">The definition of the Luis service.</param>
        /// <returns>A list of intents.</returns>
        public static List <LuisIntent> GetIntents(this LuisServiceDefinition _this)
        {
            if (_this == null)
            {
                return(new List <LuisIntent>());
            }

            List <LuisIntent> intents = new List <LuisIntent>();
            string            request = $"https://{_this.Region}.api.cognitive.microsoft.com/luis/api/v2.0/apps/{_this.AppId}/versions/{_this.Version}/intents?skip=0&take=100";

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _this.AuthoringKey);
                var response = client.GetAsync(request).GetAwaiter().GetResult();
                if (!response.IsSuccessStatusCode)
                {
                    return(new List <LuisIntent>());
                }
                var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                intents = JsonConvert.DeserializeObject <List <LuisIntent> >(data);
            }
            return(intents);
        }