Exemple #1
0
        /// <summary>
        /// Analyses the sentiment of a given set of sentences
        /// </summary>
        /// <param name="sentences">The sentences to be analysed.</param>
        private static async void analyseSentiment(List <Sentence> sentences)
        {
            //instantiate a new instance of the SentimentAnalysis class
            SentimentAnalysis analyser = new SentimentAnalysis();
            string            text     = "";

            Console.WriteLine("\n\nAnalysing Sentiment, Please Wait...\n");

            //loop through the list of sentences, sentence by sentence
            foreach (Sentence sentence in sentences)
            {
                //concatenate each sentence into one string
                text += sentence.getSentenceContent() + " ";
            }
            //call the getSentiment method and wait for it to finish
            string response = await analyser.getSentiment(text);

            //convert the JSON response to a dynamic object so we don't have to create classes for the response ourselves
            dynamic responseObj = JsonConvert.DeserializeObject(response);

            //initialise a score variable
            double score = 0;

            try {
                //try to read the score from the dynamic object, if it exists. multiply by 100 to make it a percentage
                score = responseObj.documents[0].score * 100;
            } catch {
                //the score does not exist, something went wrong with the API call
                Console.WriteLine("Sentiment analysis failed!");
                return;
            }

            Console.Clear();
            //Output the score
            outputUserFriendlyScore(score);
            Console.WriteLine("\n\n\tPress enter to exit...");
        }
        static void Main(string[] args)
        {
            string id = Guid.NewGuid().ToString();

            Console.WriteLine("Enter text to analyze:");
            string text = Console.ReadLine();

            Console.WriteLine("Document Id: {0}", id);
            Console.WriteLine("Text Analyzed: {0}", text);

            SentimentAnalysis.AnalyzeSentimentAsync(id, text, "en").Wait();

            Console.WriteLine();

            KeyPhraseAnalysis.AnalyzeKeyPhraseAsync(id, text, "en").Wait();

            Console.WriteLine();

            LanguageAnalysis.AnalyzeLanguageAsync(id, text).Wait();

            Console.WriteLine();
            Console.WriteLine("Press any key");
            Console.ReadLine();
        }