コード例 #1
0
        public async Task Run()
        {
            // read files from dir
            var files = await _storageService.ListFilesAsync();

            // loop on files
            var fileTasks = files.Select(async file =>
            {
                //  parse file (extract utterances)
                var fileStream = await _storageService.ReadFileAsync(file);
                var transcript = await _transcriptParser.ParseTranscriptAsync(fileStream);

                var luisDictionary          = new ConcurrentDictionary <long, CustomLuisResponse>();
                var textAnalyticsDictionary = new ConcurrentDictionary <long, DocumentSentiment>();
                var tasks = transcript.Utterances.Select(async utterance =>
                {
                    // run luis prediction endpoint
                    luisDictionary[utterance.Timestamp] = await _luisPredictionService.Predict(utterance.Text);
                    // run TA prediction endpoint
                    textAnalyticsDictionary[utterance.Timestamp] = await _textAnalyticsService.PredictSentimentAsync(utterance.Text, opinionMining: true);
                });
                await Task.WhenAll(tasks);

                // concatenate result
                var processedTranscript = _resultGenerator.GenerateResult(luisDictionary, textAnalyticsDictionary, transcript.Channel, transcript.Id);

                // write result file
                var outString = JsonConvert.SerializeObject(processedTranscript, Formatting.Indented);
                await _storageService.StoreDataAsync(outString, "test.json");
            });
            await Task.WhenAll(fileTasks);
        }
コード例 #2
0
        public async Task <ResultTranscript> RunAsync(Stream file, bool enableTA = false)
        {
            //  parse file (extract utterances)
            var transcript = await _transcriptParser.ParseTranscriptAsync(file);

            var luisDictionary          = new ConcurrentDictionary <long, CustomLuisResponse>();
            var textAnalyticsDictionary = new ConcurrentDictionary <long, DocumentSentiment>();

            using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(Constants.MaxConcurrency))
            {
                var tasks = transcript.Utterances.Select(async(utterance, index) =>
                {
                    concurrencySemaphore.Wait();
                    try
                    {
                        await GetLuisResponse(utterance, luisDictionary, index);
                        if (enableTA)
                        {
                            await GetTextAnalyticsResponse(utterance, textAnalyticsDictionary);
                        }
                    }
                    finally
                    {
                        concurrencySemaphore.Release();
                    }
                });
                await Task.WhenAll(tasks);
            }

            // concatenate result
            return(_resultGenerator.GenerateResult(luisDictionary, textAnalyticsDictionary, transcript.Channel, transcript.Id));
        }