public async Task WriteAnalyseReportAsync(NLPAnalyseReport analyseReport) { var sortedAnalysis = analyseReport.AnalysisResponses.OrderBy(a => a.Text); using (var writer = new StreamWriter(analyseReport.FullReportFileName)) { await writer.WriteLineAsync("Text\tIntentionId\tIntentionScore\tEntities"); foreach (var item in sortedAnalysis) { await writer.WriteLineAsync(AnalysisResponseToString(item)); } } }
public async Task WriteAnalyseReportAsync(NLPAnalyseReport analyseReport, bool append = false) { bool writeHeader = !File.Exists(analyseReport.FullReportFileName); using (var writer = new StreamWriter(analyseReport.FullReportFileName, append)) { if (writeHeader) { await writer.WriteLineAsync("Id\tText\tIntentionId\tIntentionScore\tEntities\tAnswer"); } foreach (var item in analyseReport.ReportDataLines) { await writer.WriteLineAsync(AnalysisResponseToString(item)); } } }
private async Task BuildResult(NLPAnalyseDataBlock dataBlock) { lock (_locker) { _count++; if (_count % 100 == 0) { _logger.LogDebug($"{_count}/{_total}"); } } var input = dataBlock.Input; var analysis = dataBlock.NLPAnalysisResponse; var content = dataBlock.ContentFromProvider; if (analysis == null) { return; } var resultData = new NLPAnalyseReportDataLine { Id = dataBlock.Id, Input = input, Intent = analysis.Intentions?[0].Id, Confidence = analysis.Intentions?[0].Score, Entities = analysis.Entities?.ToList().ToReportString(), }; if (content != null) { resultData.Answer = ExtractAnswer(content); } var report = new NLPAnalyseReport { ReportDataLines = new List <NLPAnalyseReportDataLine> { resultData }, FullReportFileName = dataBlock.ReportOutputFile }; await _fileService.WriteAnalyseReportAsync(report, true); _logger.LogTrace($"\"{resultData.Input}\"\t{resultData.Intent}:{resultData.Confidence:P}\t{resultData.Entities}\t{CropText(resultData.Answer, 50)}"); }
public async Task AnalyseAsync(string authorization, string inputSource, string reportOutput) { if (string.IsNullOrEmpty(authorization)) { throw new ArgumentNullException("You must provide the target bot (node) for this action."); } if (string.IsNullOrEmpty(inputSource)) { throw new ArgumentNullException("You must provide the input source (phrase or file) for this action."); } if (string.IsNullOrEmpty(reportOutput)) { throw new ArgumentNullException("You must provide the full output's report file name for this action."); } _fileService.CreateDirectoryIfNotExists(reportOutput); var client = _blipClientFactory.GetInstanceForAI(authorization); bool isPhrase = false; var isDirectory = _fileService.IsDirectory(inputSource); var isFile = _fileService.IsFile(inputSource); if (isFile) { _logger.LogDebug("\tÉ arquivo\n"); isPhrase = false; } else if (isDirectory) { _logger.LogDebug("\tÉ diretório\n"); throw new ArgumentNullException("You must provide the input source (phrase or file) for this action. Your input was a direcory."); } else { _logger.LogDebug("\tÉ frase\n"); isPhrase = true; } var responses = new List <AnalysisResponse>(); string EntitiesToString(List <EntityResponse> entities) { if (entities == null || entities.Count < 1) { return(string.Empty); } var toString = string.Join(", ", entities.Select(e => $"{e.Id}:{e.Value}")); return(toString); } async Task <AnalysisResponse> AnalyseForMetrics(string request) { return(await client.AnalyseForMetrics(request)); } void ShowResult(AnalysisResponse item) { if (item == null) { return; } responses.Add(item); _logger.LogDebug($"\"{item.Text}\"\t{item.Intentions?[0].Id}:{item.Intentions?[0].Score:P}\t{EntitiesToString(item.Entities?.ToList())}\n"); } var options = new ExecutionDataflowBlockOptions { BoundedCapacity = DataflowBlockOptions.Unbounded, MaxDegreeOfParallelism = 10 }; var analyseBlock = new TransformBlock <string, AnalysisResponse>((Func <string, Task <AnalysisResponse> >)AnalyseForMetrics, options); var actionBlock = new ActionBlock <AnalysisResponse>((Action <AnalysisResponse>)ShowResult, options); analyseBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true }); if (isPhrase) { await analyseBlock.SendAsync(inputSource); } else { var inputList = await _fileService.GetInputsToAnalyseAsync(inputSource); foreach (var input in inputList) { await analyseBlock.SendAsync(input); } } analyseBlock.Complete(); await actionBlock.Completion; var report = new NLPAnalyseReport { AnalysisResponses = responses, FullReportFileName = reportOutput }; await _fileService.WriteAnalyseReportAsync(report); }