public static async Task TestSpeechWithTextScore() { var test = "the quick brown fox jumped over the lazy dog entity"; var testFile = "somefile"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.RecognizeSpeechAsync( It.Is <string>(speechFile => speechFile == testFile), It.IsAny <PredictionRequest>(), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new SpeechPredictionResponse( new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", }, }, 0.5))); using (var luis = builder.Build()) { var result = await luis.TestSpeechAsync(testFile).ConfigureAwait(false); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.As <ScoredLabeledUtterance>().TextScore.Should().Be(0.5); result.As <ScoredLabeledUtterance>().Score.Should().Be(0); } }
public static async Task TestSpeechWithTextScore() { var testFile = "somefile"; var test = "the quick brown fox jumped over the lazy dog entity"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.RecognizeSpeechAsync( It.Is <string>(speechFile => speechFile == testFile), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new SpeechLuisResult( new LuisResult { Query = test, TopScoringIntent = new IntentModel { Intent = "intent" }, }, 0.5))); using (var luis = builder.Build()) { var result = await luis.TestSpeechAsync(testFile).ConfigureAwait(false); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.GetTextScore().Should().Be(0.5); result.GetScore().Should().BeNull(); } }
public static async Task WithLabeledIntentScore() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Intents = new Dictionary <string, Intent> { { "intent", new Intent { Score = 0.42 } }, }, }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Should().BeOfType(typeof(ScoredLabeledUtterance)); result.As <ScoredLabeledUtterance>().Score.Should().Be(0.42); } }
public static async Task NoLabeledIntentScore() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.GetScore().Should().BeNull(); } }
public static void ThrowsInvalidOperationWhenMissingMetadata() { var test = "foo"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = new Dictionary <string, object> { { "entityType", new JArray { 42 } }, }, }, })); using (var luis = builder.Build()) { Func <Task> testAsync = () => luis.TestAsync(test); testAsync.Should().Throw <InvalidOperationException>(); } }
public static async Task WithLabeledIntentScore() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <string>(query => query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new LuisResult { Query = test, TopScoringIntent = new IntentModel { Intent = "intent", Score = 0.42 }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Should().BeOfType(typeof(JsonLabeledUtterance)); result.GetScore().Should().Be(0.42); } }
public static async Task TestSpeechAsyncNoMatchResponse() { var utterance = Guid.NewGuid().ToString(); var builder = new LuisNLUTestClientBuilder(); using (var luis = builder.Build()) { var results = await luis.TestSpeechAsync(utterance).ConfigureAwait(false); results.Intent.Should().BeNull(); results.Text.Should().BeNull(); results.Entities.Should().BeNull(); } }
public static async Task UtteranceWithNestedMLEntity() { var test = "i want to request sick leave for 6 days starting march 5"; var predictionJson = File.ReadAllText("Resources/nested.json"); var prediction = JObject.Parse(predictionJson).ToObject <PredictionResponse>(); var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(prediction)); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Text.Should().Be(test); result.Intent.Should().Be("RequestVacation"); result.Entities.Count.Should().Be(7); result.Entities[0].EntityType.Should().Be("leave-type"); result.Entities[0].EntityValue.Should().BeEquivalentTo(@"[ ""sick"" ]"); result.Entities[0].MatchText.Should().Be("sick leave"); result.Entities[0].MatchIndex.Should().Be(0); result.Entities[1].EntityType.Should().Be("days-number"); result.Entities[1].EntityValue.Should().BeEquivalentTo("6"); result.Entities[1].MatchText.Should().Be("6"); result.Entities[1].MatchIndex.Should().Be(0); result.Entities[2].EntityType.Should().Be("days-duration"); result.Entities[2].EntityValue.Should().BeEquivalentTo(@"{ ""days-number"": [ 6 ] }"); result.Entities[2].MatchText.Should().Be("6 days"); result.Entities[2].MatchIndex.Should().Be(0); result.Entities[3].EntityType.Should().Be("start-date"); result.Entities[3].MatchText.Should().Be("starting march 5"); result.Entities[3].MatchIndex.Should().Be(0); result.Entities[4].EntityType.Should().Be("vacation-request"); result.Entities[4].EntityValue.Should().ContainSubtree(@"{ ""leave-type"": [ [ ""sick"" ] ], ""days-duration"": [ { ""days-number"": [ 6 ] } ], ""start-date"": [ { ""type"": ""daterange"" } ] }"); result.Entities[4].MatchText.Should().Be("sick leave for 6 days starting march 5"); result.Entities[4].MatchIndex.Should().Be(0); result.Entities[5].EntityType.Should().Be("datetimeV2"); result.Entities[5].EntityValue.Should().ContainSubtree(@"{ ""type"": ""duration"" }"); result.Entities[5].MatchText.Should().Be("6 days"); result.Entities[5].MatchIndex.Should().Be(0); result.Entities[6].EntityType.Should().Be("number"); result.Entities[6].EntityValue.Should().BeEquivalentTo("5"); result.Entities[6].MatchText.Should().Be("5"); result.Entities[6].MatchIndex.Should().Be(0); } }
public static async Task TestWithPrebuiltEntity() { var test = "the quick brown fox jumped over the lazy dog"; var builtinType = Guid.NewGuid().ToString(); var prebuiltEntityTypes = new Dictionary <string, string> { { "type", "test" }, }; var builder = new LuisNLUTestClientBuilder(); builder.LuisSettings = new LuisSettings(prebuiltEntityTypes); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = ToEntityDictionary(new[] { new EntityModel { Entity = "the", Type = "builtin.test", StartIndex = 32, EndIndex = 34 }, }), }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be("type"); result.Entities[0].EntityValue.Should().BeEquivalentTo(new JValue("the")); result.Entities[0].EntityResolution.Should().BeNull(); result.Entities[0].MatchText.Should().Be("the"); result.Entities[0].MatchIndex.Should().Be(1); } }
public static async Task TestSpeech() { var test = "the quick brown fox jumped over the lazy dog entity"; var testFile = "somefile"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.RecognizeSpeechAsync( It.Is <string>(speechFile => speechFile == testFile), It.IsAny <PredictionRequest>(), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new SpeechPredictionResponse( new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = ToEntityDictionary(new[] { new EntityModel { Entity = "entity", Type = "type", StartIndex = 45, EndIndex = 50, }, }), }, }, 0))); using (var luis = builder.Build()) { var result = await luis.TestSpeechAsync(testFile).ConfigureAwait(false); result.Should().BeOfType <JsonLabeledUtterance>(); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be("type"); result.Entities[0].MatchText.Should().Be("entity"); result.Entities[0].MatchIndex.Should().Be(0); } }
public static async Task TestModel() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = ToEntityDictionary(new[] { new EntityModel { Entity = "the", Type = "type", StartIndex = 32, EndIndex = 34, }, }), }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Should().BeOfType <JsonLabeledUtterance>(); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be("type"); result.Entities[0].EntityValue.Should().BeEquivalentTo(new JValue("the")); result.Entities[0].MatchText.Should().Be("the"); result.Entities[0].MatchIndex.Should().Be(1); } }
public static async Task WithMultipleIntents() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Intents = new Dictionary <string, Intent> { { "intent", new Intent { Score = 0.42 } }, { "foo", new Intent { Score = 0.07 } }, }, }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Should().BeOfType(typeof(JsonLabeledUtterance)); var serializer = JsonSerializer.CreateDefault(); serializer.ContractResolver = new CamelCasePropertyNamesContractResolver(); var intents = JArray.FromObject(result.GetProperty <object>("intents"), serializer); intents.Count.Should().Be(2); intents[0].Value <string>("intent").Should().Be("intent"); intents[0].Value <double>("score").Should().Be(0.42); intents[1].Value <string>("intent").Should().Be("foo"); intents[1].Value <double>("score").Should().Be(0.07); } }
public static async Task WithEntityScore() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = ToEntityDictionary(new[] { new EntityModel { Entity = "the", Type = "type", StartIndex = 32, EndIndex = 34, AdditionalProperties = new Dictionary <string, object> { { "score", 0.42 }, }, }, }), } })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(1); result.Entities[0].Should().BeOfType(typeof(ScoredEntity)); result.Entities[0].As <ScoredEntity>().Score.Should().Be(0.42); } }
public static async Task TestModel() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <string>(query => query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new LuisResult { Query = test, TopScoringIntent = new IntentModel { Intent = "intent" }, Entities = new[] { new EntityModel { Entity = "the", Type = "type", StartIndex = 32, EndIndex = 34, }, }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Text.Should().Be(test); result.Intent.Should().Be("intent"); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be("type"); result.Entities[0].EntityValue.Should().BeNull(); result.Entities[0].EntityResolution.Should().BeNull(); result.Entities[0].MatchText.Should().Be("the"); result.Entities[0].MatchIndex.Should().Be(1); } }
public static async Task WithEmptyRole() { var test = "the quick brown fox jumped over the lazy dog"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <string>(query => query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new LuisResult { Query = test, TopScoringIntent = new IntentModel { Intent = "intent" }, Entities = new[] { new EntityModel { Entity = "the", Type = "type", StartIndex = 32, EndIndex = 34, AdditionalProperties = new Dictionary <string, object> { { "role", string.Empty }, }, }, }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be("type"); } }
public static async Task EntityTextDoesNotMatch() { var test = "show me past - due my past-due tasks"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = test, Prediction = new Prediction { TopIntent = "intent", Entities = ToEntityDictionary(new[] { new EntityModel { Entity = "past - due", Type = "type", StartIndex = 22, EndIndex = 29, }, }), }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(1); result.Entities[0].MatchText.Should().Be("past-due"); result.Entities[0].MatchIndex.Should().Be(0); } }
public static async Task MatchTextContainsRegex() { var test = "foo(bar)"; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <string>(query => query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new LuisResult { Query = test, TopScoringIntent = new IntentModel { Intent = "intent" }, Entities = new[] { new EntityModel { Entity = "foo ( bar )", Type = "type", StartIndex = 0, EndIndex = 7, }, }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(1); result.Entities[0].MatchText.Should().Be("foo(bar)"); result.Entities[0].MatchIndex.Should().Be(0); } }
public static async Task TestModelWithEntityResolution() { var test = "the quick brown fox jumped over the lazy dog"; var resolution = new JObject { { "value", "Fox" }, }; var entityValues = new JToken[] { "2018-11-16", new JArray { new JArray { "Fox" } }, new JArray { new JArray { "foo", "bar" } }, new JArray { new JObject() }, }; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <PredictionRequest>(query => query.Query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new PredictionResponse { Query = "the quick brown fox jumped over the lazy dog today", Prediction = new Prediction { Entities = new Dictionary <string, object> { { "type", new JArray(entityValues) }, { "$instance", new JObject { { "type", new JArray { new JObject { { "startIndex", 45 }, { "length", 5 }, { "resolution", resolution.DeepClone() }, }, new JObject { { "startIndex", 10 }, { "length", 9 }, }, new JObject { { "startIndex", 0 }, { "length", 3 }, }, new JObject { { "startIndex", 4 }, { "length", 5 }, }, } }, } }, } }, })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(4); result.Entities[0].MatchText.Should().Be("today"); result.Entities[0].EntityValue.Should().BeEquivalentTo(entityValues[0]); result.Entities[0].EntityResolution.Should().BeEquivalentTo(resolution); result.Entities[1].MatchText.Should().Be("brown fox"); result.Entities[1].EntityValue.Should().BeEquivalentTo(entityValues[1]); result.Entities[1].EntityResolution.Should().BeNull(); result.Entities[2].MatchText.Should().Be("the"); result.Entities[2].EntityValue.Should().BeEquivalentTo(entityValues[2]); result.Entities[2].EntityResolution.Should().BeNull(); result.Entities[3].MatchText.Should().Be("quick"); result.Entities[3].EntityValue.Should().BeEquivalentTo(entityValues[3]); result.Entities[3].EntityResolution.Should().BeNull(); } }
public static async Task TestModelWithEntityResolution() { var test = "the quick brown fox jumped over the lazy dog"; var valueResolution = new JObject { { "value", "THE" } }; var valuesStringResolution = new JObject { { "values", new JArray { "Fox" } } }; var valuesValueResolution = new JObject { { "values", new JArray { new JObject { { "value", "2018-11-16" } } } }, }; var builder = new LuisNLUTestClientBuilder(); builder.LuisTestClientMock .Setup(luis => luis.QueryAsync( It.Is <string>(query => query == test), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new LuisResult { Query = "the quick brown fox jumped over the lazy dog today", Entities = new[] { new EntityModel { Entity = "the", StartIndex = 0, EndIndex = 2, AdditionalProperties = new Dictionary <string, object> { { "resolution", valueResolution.DeepClone() }, }, }, new EntityModel { Entity = "brown fox", StartIndex = 10, EndIndex = 18, AdditionalProperties = new Dictionary <string, object> { { "resolution", valuesStringResolution.DeepClone() }, }, }, new EntityModel { Entity = "today", StartIndex = 45, EndIndex = 49, AdditionalProperties = new Dictionary <string, object> { { "resolution", valuesValueResolution.DeepClone() }, }, }, } })); using (var luis = builder.Build()) { var result = await luis.TestAsync(test).ConfigureAwait(false); result.Entities.Count.Should().Be(3); result.Entities[0].EntityValue.Should().BeEquivalentTo(valueResolution); result.Entities[1].EntityValue.Should().BeEquivalentTo(valuesStringResolution); result.Entities[2].EntityValue.Should().BeEquivalentTo(valuesValueResolution); } }