예제 #1
0
        public static async Task AddsRoleToEntities()
        {
            var text            = Guid.NewGuid().ToString();
            var entityTypeName1 = Guid.NewGuid().ToString();
            var entityTypeName2 = Guid.NewGuid().ToString();
            var roles           = new Dictionary <string, string>
            {
                { entityTypeName1, entityTypeName2 },
            };

            var builder = new LuisNLUTrainClientBuilder();

            builder.LuisSettings = new LuisSettings(null, null, roles);
            using (var luis = builder.Build())
            {
                var entity1   = new Entity(entityTypeName1, null, text, 0);
                var entity2   = new Entity(entityTypeName2, null, text, 0);
                var utterance = new Models.LabeledUtterance(text, string.Empty, new[] { entity1, entity2 });
                await luis.TrainAsync(new[] { utterance }).ConfigureAwait(false);

                // Ensure LUIS app intent still has role
                var importRequest = builder.MockLuisTrainClient.Invocations.FirstOrDefault(request => request.Method.Name == nameof(ILuisTrainClient.ImportVersionAsync));
                importRequest.Should().NotBeNull();
                var luisApp = importRequest.Arguments[2].As <LuisApp>();
                luisApp.Utterances.Should().Contain(u => u.Text == text);
                luisApp.Utterances.First(u => u.Text == text).Entities.Count().Should().Be(2);
                luisApp.Utterances.First(u => u.Text == text).Entities[0].Entity.Should().Be(entityTypeName2);
                luisApp.Utterances.First(u => u.Text == text).Entities[1].Entity.Should().Be(entityTypeName2);
                luisApp.Utterances.First(u => u.Text == text).Entities.OfType <JSONEntityWithRole>().Single().Role.Should().Be(entityTypeName1);
            }
        }
예제 #2
0
        public static async Task TagsPrebuiltEntityWithReplacementName()
        {
            var text                = Guid.NewGuid().ToString();
            var entityTypeName1     = Guid.NewGuid().ToString();
            var entityTypeName2     = Guid.NewGuid().ToString();
            var prebuiltEntityTypes = new Dictionary <string, string>
            {
                { entityTypeName1, entityTypeName2 },
            };

            var mockClient = new MockLuisClient();
            var builder    = GetTestLuisBuilder();

            builder.LuisClient   = mockClient;
            builder.LuisSettings = new LuisSettings(prebuiltEntityTypes);

            using (var luis = builder.Build())
            {
                var entity1   = new Entity(entityTypeName1, null, text, 0);
                var entity2   = new Entity(entityTypeName2, null, text, 0);
                var utterance = new Models.LabeledUtterance(text, string.Empty, new[] { entity1, entity2 });
                await luis.TrainAsync(new[] { utterance }).ConfigureAwait(false);

                // Ensure LUIS app intent still has role
                var importRequest = mockClient.Requests.FirstOrDefault(request => request.Method == nameof(ILuisClient.ImportVersionAsync));
                importRequest.Should().NotBeNull();
                var luisApp = importRequest.Arguments[2].As <LuisApp>();
                luisApp.Utterances.Should().Contain(u => u.Text == text);
                luisApp.Utterances.First(u => u.Text == text).Entities.Count().Should().Be(2);
                luisApp.Utterances.First(u => u.Text == text).Entities.Should().Contain(e => e.Entity == entityTypeName2);
                luisApp.Utterances.First(u => u.Text == text).Entities.Should().Contain(e => e.Entity == entityTypeName2);
            }
        }
        public static JSONUtterance ToJSONUtterance(this Models.LabeledUtterance utterance, IReadOnlyDictionary <string, string> prebuiltEntityTypes)
        {
            JSONEntity toJSONEntity(Entity entity)
            {
                var entityType = prebuiltEntityTypes.TryGetValue(entity.EntityType, out var builtinType)
                    ? builtinType
                    : entity.EntityType;
                var startPos = entity.StartCharIndexInText(utterance.Text);

                return(new JSONEntity(startPos, startPos + entity.MatchText.Length - 1, entityType));
            }

            return(new JSONUtterance(
                       utterance.Text,
                       utterance.Intent,
                       utterance.Entities?.Select(toJSONEntity).ToArray() ?? Array.Empty <JSONEntity>()));
        }
        public static JSONUtterance ToJSONUtterance(this Models.LabeledUtterance utterance, LuisApp luisApp)
        {
            JSONEntity toJSONEntity(Entity entity)
            {
                var startPos = entity.StartCharIndexInText(utterance.Text);
                var endPos   = startPos + entity.MatchText.Length - 1;

                if (luisApp.TryGetEntityTypeForRole(entity.EntityType, out var entityType))
                {
                    return(new JSONEntityWithRole(startPos, endPos, entityType, entity.EntityType));
                }

                return(new JSONEntity(startPos, endPos, entity.EntityType));
            }

            return(new JSONUtterance(
                       utterance.Text,
                       utterance.Intent,
                       utterance.Entities?.Select(toJSONEntity).ToArray() ?? Array.Empty <JSONEntity>()));
        }
예제 #5
0
        public static async Task DoesNotOverwriteTemplateIntents()
        {
            var role        = Guid.NewGuid().ToString();
            var intentName  = Guid.NewGuid().ToString();
            var appTemplate = new LuisApp
            {
                ClosedLists = new List <ClosedList>(),
                Entities    = new List <HierarchicalModel>(),
                Intents     = new List <HierarchicalModel>
                {
                    new HierarchicalModel
                    {
                        Name  = intentName,
                        Roles = new List <string> {
                            role
                        },
                    },
                },
                ModelFeatures    = new List <JSONModelFeature>(),
                PrebuiltEntities = new List <PrebuiltEntity>(),
            };

            var mockClient = new MockLuisClient();
            var builder    = GetTestLuisBuilder();

            builder.LuisSettings = new LuisSettings(appTemplate);
            builder.LuisClient   = mockClient;
            using (var luis = builder.Build())
            {
                var utterance = new Models.LabeledUtterance(null, intentName, null);
                await luis.TrainAsync(new[] { utterance }).ConfigureAwait(false);

                // Ensure LUIS app intent still has role
                var importRequest = mockClient.Requests.FirstOrDefault(request => request.Method == nameof(ILuisClient.ImportVersionAsync));
                importRequest.Should().NotBeNull();
                var luisApp = importRequest.Arguments[2].As <LuisApp>();
                luisApp.Intents.Should().Contain(intent => intent.Name == intentName);
                luisApp.Intents.First(intent => intent.Name == intentName).Roles.Count.Should().Be(1);
                luisApp.Intents.First(intent => intent.Name == intentName).Roles.First().Should().Be(role);
            }
        }
예제 #6
0
        public static JSONUtterance ToJSONUtterance(this Models.LabeledUtterance utterance, LuisSettings luisSettings)
        {
            JSONEntity toJSONEntity(Entity entity)
            {
                var startPos = entity.StartCharIndexInText(utterance.Text);
                var endPos   = startPos + entity.MatchText.Length - 1;

                if (luisSettings.Roles.TryGetValue(entity.EntityType, out var entityType))
                {
                    return(new JSONEntityWithRole(startPos, endPos, entityType, entity.EntityType));
                }

                entityType = luisSettings.PrebuiltEntityTypes.TryGetValue(entity.EntityType, out var builtinType)
                    ? builtinType
                    : entity.EntityType;
                return(new JSONEntity(startPos, endPos, entityType));
            }

            return(new JSONUtterance(
                       utterance.Text,
                       utterance.Intent,
                       utterance.Entities?.Select(toJSONEntity).ToArray() ?? Array.Empty <JSONEntity>()));
        }