コード例 #1
0
        public void TestTrainingQueries()
        {
            DetailedResponse <DocumentAccepted> addDocumentResult = null;

            using (FileStream fs = File.OpenRead(filepathToIngest))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);

                    service.WithHeader("X-Watson-Test", "1");
                    addDocumentResult = service.AddDocument(
                        projectId: projectId,
                        collectionId: collectionId,
                        file: ms,
                        filename: "watson_beats_jeopardy.html",
                        fileContentType: "text/html",
                        metadata: metadata,
                        xWatsonDiscoveryForce: false
                        );
                }
            }

            var documentId = addDocumentResult.Result.DocumentId;

            service.WithHeader("X-Watson-Test", "1");
            var listTrainingQueriesResult = service.ListTrainingQueries(
                projectId: projectId
                );

            Assert.IsNotNull(listTrainingQueriesResult.Result);
            Assert.IsNotNull(listTrainingQueriesResult.Result.Queries);

            var naturalLanguageQuery = "What is IBM's stock price?";
            var filters  = "entities.text:IBM";
            var examples = new List <TrainingExample>()
            {
                new TrainingExample()
                {
                    DocumentId   = documentId,
                    CollectionId = collectionId,
                    Relevance    = 1
                }
            };

            service.WithHeader("X-Watson-Test", "1");
            var createTrainingQueryResult = service.CreateTrainingQuery(
                projectId: projectId,
                naturalLanguageQuery: naturalLanguageQuery,
                filter: filters,
                examples: examples
                );

            var queryId = createTrainingQueryResult.Result.QueryId;

            Assert.IsNotNull(createTrainingQueryResult.Result);
            Assert.IsNotNull(createTrainingQueryResult.Result.QueryId);
            Assert.IsNotNull(createTrainingQueryResult.Result.Created);
            Assert.IsNotNull(createTrainingQueryResult.Result.Updated);
            Assert.IsNotNull(createTrainingQueryResult.Result.NaturalLanguageQuery);
            Assert.IsNotNull(createTrainingQueryResult.Result.Examples);
            Assert.IsTrue(createTrainingQueryResult.Result.NaturalLanguageQuery == naturalLanguageQuery);

            service.WithHeader("X-Watson-Test", "1");
            var getTrainingQueryResult = service.GetTrainingQuery(
                projectId: projectId,
                queryId: queryId
                );

            Assert.IsNotNull(getTrainingQueryResult.Result);
            Assert.IsTrue(getTrainingQueryResult.Result.QueryId == queryId);
            Assert.IsTrue(getTrainingQueryResult.Result.NaturalLanguageQuery == naturalLanguageQuery);

            var updatedNaturalLanguageQuery = "Who did Watson beat on Jeopardy?";
            var updatedFilter   = "entities.text:Jeopardy";
            var updatedExamples = new List <TrainingExample>()
            {
                new TrainingExample()
                {
                    DocumentId   = documentId,
                    CollectionId = collectionId,
                    Relevance    = 2
                }
            };

            service.WithHeader("X-Watson-Test", "1");
            var updateTrainingQueryResult = service.UpdateTrainingQuery(
                projectId: projectId,
                queryId: queryId,
                naturalLanguageQuery: updatedNaturalLanguageQuery,
                filter: updatedFilter,
                examples: updatedExamples
                );

            queryId = updateTrainingQueryResult.Result.QueryId;

            Assert.IsTrue(updateTrainingQueryResult.Result.QueryId == queryId);
            Assert.IsTrue(updateTrainingQueryResult.Result.NaturalLanguageQuery == updatedNaturalLanguageQuery);

            service.WithHeader("X-Watson-Test", "1");
            var deleteTrainingQueryResult = service.DeleteTrainingQueries(
                projectId: projectId
                );

            Assert.IsTrue(deleteTrainingQueryResult.StatusCode == 204);

            service.WithHeader("X-Watson-Test", "1");
            var deleteDocumentResult = service.DeleteDocument(
                projectId: projectId,
                collectionId: collectionId,
                documentId: documentId,
                xWatsonDiscoveryForce: false
                );
        }