private async void Detect_Topics(string text) { try { var source = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList(); var randomText = new RandomText(source); var request = new TopicRequest(); for (int i = 0; i < 100; i++) { request.Documents.Add(new TopicDocument() { Id = i.ToString(), Text = randomText.Next() }); } MainWindow mainWindow = Window.GetWindow(this) as MainWindow; var client = new TopicClient(mainWindow._scenariosControl.SubscriptionKey); var opeationUrl = await client.StartTopicProcessingAsync(request); TopicResponse response = null; var doneProcessing = false; while (!doneProcessing) { response = await client.GetTopicResponseAsync(opeationUrl); switch (response.Status) { case TopicOperationStatus.Cancelled: Log("Operation Status: Cancelled"); doneProcessing = true; break; case TopicOperationStatus.Failed: Log("Operation Status: Failed"); doneProcessing = true; break; case TopicOperationStatus.NotStarted: Log("Operation Status: Not Started"); Thread.Sleep(100); break; case TopicOperationStatus.Running: Log("Operation Status: Running"); Thread.Sleep(100); break; case TopicOperationStatus.Succeeded: Log("Operation Status: Succeeded"); doneProcessing = true; break; } } foreach (var topic in response.OperationProcessingResult.Topics) { var score = topic.Score * 100; Log(string.Format("{0} ({1}%)", topic.KeyPhrase, score)); } } catch (Exception ex) { Log(ex.Message); } }
static async Task MainAsync() { 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 = await client.StartTopicProcessingAsync(request); TopicResponse response = null; var doneProcessing = false; while (!doneProcessing) { response = await client.GetTopicResponseAsync(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(); }