コード例 #1
0
        public void GetTopics()
        {
            var randomText = new RandomText();

            var request = new TopicRequest();

            for (int i = 1; i <= 200; i++)
            {
                request.Documents.Add(new TopicDocument()
                {
                    Id = i.ToString(), Text = randomText.Next()
                });
            }

            var client      = new TopicClient(apiKey);
            var opeationUrl = client.StartTopicProcessing(request);

            TopicResponse response       = null;
            var           startTime      = DateTime.Now;
            var           timeoutTime    = startTime.AddMinutes(30);
            var           doneProcessing = false;

            while (!doneProcessing)
            {
                response = client.GetTopicResponse(opeationUrl);

                if (response.Status == TopicOperationStatus.Cancelled)
                {
                    Assert.Fail("Operation Cancelled");
                }
                else if (response.Status == TopicOperationStatus.Failed)
                {
                    Assert.Fail("Operation Failed");
                }
                else if (response.Status == TopicOperationStatus.Succeeded)
                {
                    doneProcessing = true;
                }
                else
                {
                    if (DateTime.Now >= timeoutTime)
                    {
                        Assert.Fail("Operation Timeout");
                    }
                    else
                    {
                        Thread.Sleep(60000);
                    }
                }
            }

            Assert.IsTrue(response.OperationProcessingResult.TopicAssignments.Count > 0);
            Assert.IsTrue(response.OperationProcessingResult.Topics.Count > 0);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var apiKey = "YOUR-TEXT-ANALYTICS-API-SUBSCRIPTION-KEY";

            var randomText = new RandomText();

            var request = new TopicRequest();

            for (int i = 1; i <= 200; i++)
            {
                request.Documents.Add(new TopicDocument()
                {
                    Id = i.ToString(), Text = randomText.Next()
                });
            }

            var client       = new TopicClient(apiKey);
            var operationUrl = client.StartTopicProcessing(request);

            TopicResponse response       = null;
            var           doneProcessing = false;

            while (!doneProcessing)
            {
                response = client.GetTopicResponse(operationUrl);

                switch (response.Status)
                {
                case TopicOperationStatus.Cancelled:
                    Console.WriteLine("Status: Operation Cancelled");
                    doneProcessing = true;
                    break;

                case TopicOperationStatus.Failed:
                    Console.WriteLine("Status: Operation Failed");
                    doneProcessing = true;
                    break;

                case TopicOperationStatus.NotStarted:
                    Console.WriteLine("Status: Operation Not Started");
                    Thread.Sleep(60000);
                    break;

                case TopicOperationStatus.Running:
                    Console.WriteLine("Status: Operation Running");
                    Thread.Sleep(60000);
                    break;

                case TopicOperationStatus.Succeeded:
                    Console.WriteLine("Status: Operation Succeeded");
                    doneProcessing = true;
                    break;
                }
            }

            foreach (var topic in response.OperationProcessingResult.Topics)
            {
                Console.WriteLine("{0} | {1}", topic.KeyPhrase, topic.Score);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }