コード例 #1
0
        public async Task <Intent> GetIntentAsync(string utterance)
        {
            var headers = new List <KeyValuePair <string, string> >();

            headers.Add(new KeyValuePair <string, string>(_subscriptionKeyHeaderName, _subscriptionKey));

            string uri = CreateLuisQueryString(utterance, 0, true, false, true);

            var client   = _serviceProvider.GetService <IHttpProxy>();
            var response = await client.GetAsync(uri, headers);

            var luisResponse = response.AsLuisResponse();

            var nextBestIntent = luisResponse.intents
                                 .OrderByDescending(i => i.score).Skip(1).Take(1).Single();

            var intentBuilder = new IntentBuilder()
                                .AddName(luisResponse.topScoringIntent.intent)
                                .AddScore(luisResponse.topScoringIntent.score, nextBestIntent.score)
                                .AddResponse(luisResponse.AsJson())
                                .AddUtterance(utterance);

            foreach (var luisEntity in luisResponse.entities)
            {
                var hasEntityValues = luisEntity?.resolution?.values?.Any();
                if (hasEntityValues.HasValue && hasEntityValues.Value)
                {
                    var values = new List <IntentEntityValue>();
                    foreach (var v in luisEntity.resolution.values)
                    {
                        values.Add(new IntentEntityValue()
                        {
                            // TODO: Put the real values for ValueType and Pattern back
                            ValueType = "unknown",
                            Value     = v,
                            Pattern   = ""
                        });
                    }
                    intentBuilder.AddEntity(luisEntity.entity, luisEntity.type, values);
                }
                else
                {
                    intentBuilder.AddEntity(luisEntity.entity, luisEntity.type);
                }
            }

            return(intentBuilder.Build());
        }
コード例 #2
0
        public void ReturnAnIntentWithAnEntityThatHasAEmptyValuesCollection()
        {
            string expectedName = string.Empty.GetRandom();
            string expectedType = string.Empty.GetRandom();

            var target = new IntentBuilder();
            var actual = target
                         .AddEntity(expectedName, expectedType)
                         .Build();

            var actualEntity = actual.IntentEntities.Single();

            Assert.False(actualEntity.Values.Any());
        }
コード例 #3
0
        public void ReturnAnIntentWithAnEntity()
        {
            string expectedName = string.Empty.GetRandom();
            string expectedType = string.Empty.GetRandom();

            var target = new IntentBuilder();
            var actual = target
                         .AddEntity(expectedName, expectedType)
                         .Build();

            var actualEntity = actual.IntentEntities.Single();

            Assert.Equal(expectedName, actualEntity.Name);
            Assert.Equal(expectedType, actualEntity.Type);
        }
コード例 #4
0
        public void ReturnAnIntentWithTwoEntities()
        {
            string expectedName1 = string.Empty.GetRandom();
            string expectedType1 = string.Empty.GetRandom();
            string expectedName2 = string.Empty.GetRandom();
            string expectedType2 = string.Empty.GetRandom();

            var target = new IntentBuilder();
            var actual = target
                         .AddEntity(expectedName1, expectedType1)
                         .AddEntity(expectedName2, expectedType2)
                         .Build();

            var actualEntity1 = actual.IntentEntities.Single(e => e.Type == expectedType1);
            var actualEntity2 = actual.IntentEntities.Single(e => e.Type == expectedType2);

            Assert.Equal(expectedName1, actualEntity1.Name);
            Assert.Equal(expectedName2, actualEntity2.Name);
        }
コード例 #5
0
        public void ReturnAnIntentWithAnEntityThatHasASingleValueInTheValuesCollection()
        {
            string expectedName = string.Empty.GetRandom();
            string expectedType = string.Empty.GetRandom();

            string expectedPattern   = string.Empty.GetRandom();
            string expectedValue     = string.Empty.GetRandom();
            string expectedValueType = string.Empty.GetRandom();

            var expectedValues = new IntentEntityValuesBuilder()
                                 .Add(expectedValueType, expectedValue, expectedPattern)
                                 .Build();

            var target = new IntentBuilder();
            var actual = target
                         .AddEntity(expectedName, expectedType, expectedValues)
                         .Build();

            var actualEntity = actual.IntentEntities.Single();
            var actualValue  = actualEntity.Values.Single();

            Assert.Equal(expectedValue, actualValue.Value);
        }