예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Sentiment Analysis CLI tool.");
            Console.WriteLine("API Location: {0}", Config.ConfigReader.ApiLocation);
            Console.WriteLine("API Key: {0}", string.IsNullOrWhiteSpace(Config.ConfigReader.ApiKey) ? "Not set!" : "Set");

            var cliArguments = new CliArguments(args);

            if (cliArguments.ArgumentType == ArgType.Invalid)
            {
                Console.WriteLine("Options: \n\t-k: Include Keyphrase Analysis\n\t-f {filename.ext} : Process contents of file.\n\t-t \"Text to analyse\" : Process text as entered.");
                Console.WriteLine("\nFormat: \n\tSentimentAnalysisConsoleTestTool -f {filename.txt}");
                Console.WriteLine("\tSentimentAnalysisConsoleTestTool -t \"Text to analyse\"");
                Console.WriteLine("\tSentimentAnalysisConsoleTestTool -f {filename.txt} -k\n");

                return;
            }

            if (string.IsNullOrWhiteSpace(Config.ConfigReader.ApiKey) || string.IsNullOrWhiteSpace(Config.ConfigReader.ApiLocation))
            {
                Console.WriteLine("No API location or key is present. Cannot continue.");
                return;
            }


            string textToAnalyse = GetAnalysisInput(cliArguments);

            if (textToAnalyse == null)
            {
                return;
            }

            var location = (LocationKeyIdentifier)System.Enum.Parse(typeof(LocationKeyIdentifier), Config.ConfigReader.ApiLocation, true);

            try
            {
                Console.WriteLine("Sentiment Analysis: Submitting {0} characters of text for analysis.", textToAnalyse.Length);
                if (cliArguments.Options == ArgOptions.IncludeKeyphraseAnalysis)
                {
                    Console.WriteLine("Including Keyphrase analysis");
                }
                var analysis = TextAnalyticConfigurationSettings.CreateUsingConfigurationKeys(Config.ConfigReader.ApiKey, location)
                               .AddConsoleDiagnosticLogging()
                               .UsingHttpCommunication()
                               .WithTextAnalyticAnalysisActions()
                               .AddSentimentAnalysisSplitIntoSentences(textToAnalyse);
                if (cliArguments.Options == ArgOptions.IncludeKeyphraseAnalysis)
                {
                    analysis = analysis.AddKeyPhraseAnalysis(textToAnalyse);
                }
                var result = analysis.AnalyseAllAsync().Result;

                ProcessResults(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error calling the Cognitive service. [{0}]", ex.Message);
            }
        }
예제 #2
0
 private static string GetAnalysisInput(CliArguments cliArgs)
 {
     if (cliArgs.ArgumentType == ArgType.ManualText)
     {
         return(cliArgs.TextToAnalyse);
     }
     else
     {
         if (System.IO.File.Exists(cliArgs.Filename))
         {
             return(System.IO.File.ReadAllText(cliArgs.Filename));
         }
         else
         {
             Console.WriteLine("Error: File [{0}] does not exist", cliArgs.Filename);
             return(null);
         }
     }
 }