static async Task EnsureUtterance(this LUISAuthoringClient client, ApplicationInfoResponse app, string intent, string utterance, List <string> entitiesFound) { var elos = new List <EntityLabelObject>(); var effectiveUtterance = utterance; while (true) { var entityInfoCaptured = entityInfoRegex.Match(effectiveUtterance); if (!entityInfoCaptured.Success) { break; } var entityInfo = entityInfoCaptured.Groups["entityInfo"].Value.Split(':'); var entityValue = entityInfo[0]; var entityName = entityInfo[1]; if (!entitiesFound.Contains(entityName)) { // brute force insert (throws ex if exists) try { await client.Model.AddEntityAsync(app.Id.Value, app.ActiveVersion, new ModelCreateObject { Name = entityName }); } catch (Exception ex) { } finally { entitiesFound.Add(entityName); } } var elo = new EntityLabelObject { EntityName = entityName, StartCharIndex = entityInfoCaptured.Index, EndCharIndex = entityInfoCaptured.Index + entityValue.Length - 1 }; elos.Add(elo); effectiveUtterance = effectiveUtterance.Replace(entityInfoCaptured.Groups[0].Value, entityValue); } await client.Examples.AddAsync(app.Id.Value, app.ActiveVersion, new ExampleLabelObject { Text = effectiveUtterance, IntentName = intent, EntityLabels = elos }); }
/// <summary> /// Carries out an operation on an <see cref="App"/> with respect to the tenant identified by the request. /// </summary> /// <param name="appRequest">The request.</param> /// <returns>The response.</returns> public ApplicationInfoResponse ApplicationOperation(ApplicationOperationRequest appRequest) { using (Profiler.Measure("CastActivityService.ApplicationOperation")) { var response = new ApplicationInfoResponse(); try { if (appRequest == null) { throw new ArgumentNullException("appRequest"); } if (!CastService.GetIsCastConfigured()) { throw new InvalidOperationException(); } var tenant = appRequest.Tenant; switch (appRequest.Operation) { case Contracts.ApplicationOperation.Install: ApplicationService.Install(tenant, appRequest.Id, appRequest.Version); break; case Contracts.ApplicationOperation.Uninstall: ApplicationService.Uninstall(tenant, appRequest.Id); break; default: throw new NotSupportedException(appRequest.Operation.ToString()); } var apps = ApplicationService.GetInstalledApps(tenant); if (apps != null) { response.Installed = apps.ToList(); } } catch (Exception e) { response.IsError = true; response.Error = e.Message; } return(response); } }