Ejemplo n.º 1
0
        public async override Task <int> RunAsync(string[] args)
        {
            if ((!From.IsSet && !FromAuthorization.IsSet) || (!To.IsSet && !ToAuthorization.IsSet))
            {
                throw new ArgumentNullException("You must provide from and to parameters for this action. Use '-f' [--from] (or '--fa' [--fromAuthorization]) and '-t' [--to] (or '--ta' [--toAuthorization]) parameters");
            }

            string fromAuthorization = FromAuthorization.Value;
            string toAuthorization   = ToAuthorization.Value;

            if (From.IsSet && string.IsNullOrEmpty(fromAuthorization))
            {
                fromAuthorization = _settingsFile.GetNodeCredentials(Node.Parse(From.Value)).Authorization;
            }

            if (To.IsSet && string.IsNullOrEmpty(toAuthorization))
            {
                toAuthorization = _settingsFile.GetNodeCredentials(Node.Parse(To.Value)).Authorization;
            }

            IBlipBucketClient sourceBlipBucketClient = new BlipHttpClientAsync(fromAuthorization);
            IBlipBucketClient targetBlipBucketClient = new BlipHttpClientAsync(toAuthorization);

            IBlipAIClient sourceBlipAIClient = _blipAIClientFactory.GetInstanceForAI(fromAuthorization);
            IBlipAIClient targetBlipAIClient = _blipAIClientFactory.GetInstanceForAI(toAuthorization);

            foreach (var content in Contents.Value)
            {
                //if IAModel handle in a different way
                if (content.Equals(BucketNamespace.AIModel))
                {
                    await CopyAIModelAsync(fromAuthorization, toAuthorization, sourceBlipAIClient, targetBlipAIClient);
                }
                else
                {
                    var documentKeysToCopy = await sourceBlipBucketClient.GetAllDocumentKeysAsync(content) ?? new DocumentCollection();

                    var documentPairsToCopy = await sourceBlipBucketClient.GetAllDocumentsAsync(documentKeysToCopy, content);

                    if (documentPairsToCopy != null)
                    {
                        foreach (var resourcePair in documentPairsToCopy)
                        {
                            await targetBlipBucketClient.AddDocumentAsync(resourcePair.Key, resourcePair.Value, content);
                        }
                    }
                }
            }

            return(0);
        }
Ejemplo n.º 2
0
        public async Task ExportNLPModelAsync(string authorization, string outputFilePath, string excel = null)
        {
            if (string.IsNullOrEmpty(authorization))
            {
                throw new ArgumentNullException(nameof(authorization));
            }

            if (string.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentNullException(nameof(outputFilePath));
            }

            var blipAIClient = _blipClientFactory.GetInstanceForAI(authorization);

            _logger.LogDebug("NLP Export\n");

            var intentions = await blipAIClient.GetAllIntentsAsync();

            var entities = await blipAIClient.GetAllEntities();

            _fileManagerService.CreateDirectoryIfNotExists(outputFilePath);

            if (!string.IsNullOrEmpty(excel))
            {
                List <NLPExportModel> excelExportModels = new List <NLPExportModel>
                {
                    WriteIntentionExcel(intentions),
                    WriteQuestionsExcel(intentions),
                    WriteAnswersExcel(intentions),
                    WriteEntitiesExcel(entities)
                };

                _excelGeneratorService.WriteContentOnExcel(excelExportModels, outputFilePath, excel);
            }
            else
            {
                var csvExportModels = new List <NLPExportModel>
                {
                    WriteIntentionCSV(intentions, outputFilePath),
                    WriteAnswersCSV(intentions, outputFilePath),
                    WriteEntitiesCSV(entities, outputFilePath)
                };
                _csvGeneratorService.WriteContentOnCSV(csvExportModels, outputFilePath);
            }

            _logger.LogDebug("DONE");
        }
        public override async Task <int> RunAsync(string[] args)
        {
            if (!Node.IsSet && !Authorization.IsSet)
            {
                throw new ArgumentNullException("You must provide the target bot (node) for this action. Use '-n' [--node] (or '-a' [--authorization]) parameters");
            }

            string authorization = Authorization.Value;

            if (Node.IsSet)
            {
                authorization = _settingsFile.GetNodeCredentials(Lime.Protocol.Node.Parse(Node.Value)).Authorization;
            }

            _blipAIClient = _blipClientFactory.GetInstanceForAI(authorization);

            var intents = new List <Intention>();

            if (IntentsFilePath.IsSet)
            {
                Console.WriteLine("Starting intents import task...");
                intents = await ImportIntentions();

                Console.WriteLine("Intents imported with success...");
            }

            if (AnswersFilePath.IsSet && intents != null && intents.Count > 0)
            {
                Console.WriteLine("Starting answers import task...");
                await ImportAnswers(intents);

                Console.WriteLine("Answers imported with success...");
            }


            if (EntitiesFilePath.IsSet)
            {
                Console.WriteLine("Starting entities import task...");
                await ImportEntities();

                Console.WriteLine("Entities imported with success...");
            }

            return(0);
        }
        public async Task AnalyseAsync(string authorization, string inputSource, string reportOutput, bool doContentCheck = false, bool rawContent = false)
        {
            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.");
            }

            _logger.LogDebug("COMEÇOU!");

            _fileService.CreateDirectoryIfNotExists(reportOutput);

            var client = _blipClientFactory.GetInstanceForAI(authorization);
            IContentManagerApiClient contentClient = new ContentManagerApiClient(authorization);
            var allIntents = new List <Intention>();

            if (doContentCheck)
            {
                _logger.LogDebug("\tCarregando intencoes...");
                allIntents = await client.GetAllIntentsAsync();

                _logger.LogDebug("\tCarregadas!");
            }

            var inputType = InputType.Phrase;

            inputType = DetectInputType(inputSource);

            var options = new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = DataflowBlockOptions.Unbounded,
                MaxDegreeOfParallelism = 20,
            };

            var analyseBlock     = new TransformBlock <DataBlock, DataBlock>((Func <DataBlock, Task <DataBlock> >)AnalyseForMetrics, options);
            var checkBlock       = new TransformBlock <DataBlock, DataBlock>((Func <DataBlock, DataBlock>)CheckResponse, options);
            var contentBlock     = new TransformBlock <DataBlock, DataBlock>((Func <DataBlock, Task <DataBlock> >)GetContent, options);
            var buildResultBlock = new ActionBlock <DataBlock>(BuildResult, new ExecutionDataflowBlockOptions
            {
                BoundedCapacity    = DataflowBlockOptions.Unbounded,
                MaxMessagesPerTask = 1
            });

            analyseBlock.LinkTo(checkBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            checkBlock.LinkTo(contentBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            contentBlock.LinkTo(buildResultBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            _count = 0;

            var inputList = await GetInputList(inputType, inputSource, client, contentClient, reportOutput, allIntents, doContentCheck, rawContent);

            _total = inputList.Count;
            foreach (var input in inputList)
            {
                await analyseBlock.SendAsync(input);
            }

            analyseBlock.Complete();
            await buildResultBlock.Completion;

            _logger.LogDebug("TERMINOU!");
        }
Ejemplo n.º 5
0
        public async Task AnalyseAsync(string authorization, string inputSource, string reportOutput, bool doContentCheck = false)
        {
            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.");
            }

            _logger.LogDebug("COMEÇOU!");

            _fileService.CreateDirectoryIfNotExists(reportOutput);

            var bucketStorage   = new BucketStorage("Key " + authorization);
            var contentProvider = new Take.ContentProvider.ContentProvider(bucketStorage, 5);
            var client          = _blipClientFactory.GetInstanceForAI(authorization);

            var allIntents = new List <Intention>();

            if (doContentCheck)
            {
                _logger.LogDebug("\tCarregando intencoes...");
                allIntents = await client.GetAllIntentsAsync();

                _logger.LogDebug("\tCarregadas!");
            }
            bool isPhrase = false;

            var isDirectory = _fileService.IsDirectory(inputSource);
            var isFile      = _fileService.IsFile(inputSource);

            if (isFile)
            {
                _logger.LogDebug("\tA entrada é um arquivo");
                isPhrase = false;
            }
            else
            if (isDirectory)
            {
                _logger.LogError("\tA entrada é um diretório");
                throw new ArgumentNullException("You must provide the input source (phrase or file) for this action. Your input was a direcory.");
            }
            else
            {
                _logger.LogDebug("\tA entrada é uma frase");
                isPhrase = true;
            }

            var options = new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = DataflowBlockOptions.Unbounded,
                MaxDegreeOfParallelism = 20,
            };

            var analyseBlock    = new TransformBlock <NLPAnalyseDataBlock, NLPAnalyseDataBlock>((Func <NLPAnalyseDataBlock, Task <NLPAnalyseDataBlock> >)AnalyseForMetrics, options);
            var checkBlock      = new TransformBlock <NLPAnalyseDataBlock, NLPAnalyseDataBlock>((Func <NLPAnalyseDataBlock, NLPAnalyseDataBlock>)CheckResponse, options);
            var contentBlock    = new TransformBlock <NLPAnalyseDataBlock, NLPAnalyseDataBlock>((Func <NLPAnalyseDataBlock, Task <NLPAnalyseDataBlock> >)GetContent, options);
            var showResultBlock = new ActionBlock <NLPAnalyseDataBlock>(BuildResult, new ExecutionDataflowBlockOptions
            {
                BoundedCapacity    = DataflowBlockOptions.Unbounded,
                MaxMessagesPerTask = 1
            });

            analyseBlock.LinkTo(checkBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            checkBlock.LinkTo(contentBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            contentBlock.LinkTo(showResultBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            _count = 0;

            var inputList = await GetInputList(isPhrase, inputSource, client, reportOutput, allIntents, contentProvider, doContentCheck);

            _total = inputList.Count;
            foreach (var input in inputList)
            {
                await analyseBlock.SendAsync(input);
            }

            analyseBlock.Complete();
            await showResultBlock.Completion;

            _logger.LogDebug("TERMINOU!");
        }
Ejemplo n.º 6
0
        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);
        }