Ejemplo n.º 1
0
        private static ProcessedConversation GetSentiment(RawConversation rawConversation)
        {
            Console.WriteLine("Processing sentiment for phone_number {0} data...", rawConversation.PhoneNumber);
            ProcessedConversation processedConversation = new ProcessedConversation();
            processedConversation.PhoneNumber = rawConversation.PhoneNumber;
            processedConversation.Location = rawConversation.Location;
            processedConversation.Nationality = rawConversation.Nationality;
            processedConversation.Gender = rawConversation.Gender;
            processedConversation.AgeGroup = rawConversation.AgeGroup;
            processedConversation.Exchanges = rawConversation.Exchanges;
            Random random = new Random();
            processedConversation.Category = random.Next(0, 8);

            string IODApiKey = "Put your Api Key here.";

            try
            {
                IODJobBatchRequest req = new IODJobBatchRequest();
                req.ApiKey = IODApiKey;

                if (String.IsNullOrEmpty(rawConversation.VolunteerMessage))
                {
                    processedConversation.VolunteerMsgSenti = 0;
                }
                else
                {
                    SentimentRequest srVM = new SentimentRequest(IODSource.text, rawConversation.VolunteerMessage);
                    req.AddRequest(srVM);
                    srVM.OnResponse = (status, response) =>
                    {
                        processedConversation.VolunteerMsgSenti = response.aggregate.score;
                    };
                }

                if (String.IsNullOrEmpty(rawConversation.ReporterMessage))
                {
                    processedConversation.ReporterMsgSenti = 0;
                }
                else
                {
                    SentimentRequest srRM = new SentimentRequest(IODSource.text, rawConversation.ReporterMessage);
                    req.AddRequest(srRM);
                    srRM.OnResponse = (status, response) =>
                    {
                        processedConversation.ReporterMsgSenti = response.aggregate.score;
                    };
                }

                if (String.IsNullOrEmpty(rawConversation.VolunteerMessage + rawConversation.ReporterMessage))
                {
                    processedConversation.OverallMsgSenti = 0;
                }
                else
                {
                    SentimentRequest srOM = new SentimentRequest(IODSource.text, rawConversation.VolunteerMessage + rawConversation.ReporterMessage);
                    req.AddRequest(srOM);
                    srOM.OnResponse = (status, response) =>
                    {
                        processedConversation.OverallMsgSenti = response.aggregate.score;
                    };
                }

                if (!String.IsNullOrEmpty(rawConversation.VolunteerMessage + rawConversation.ReporterMessage))
                {
                    Console.WriteLine("Making request ...");
                    req.MakeRequest();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("\r\nException: {0}", e.Message));
            }

            return processedConversation;
        }
        static void Main(string[] args)
        {
            string IODApiKey = "PUT YOUR IDOL OnDemand API key here";

            List<string> negativeTopics = new List<string>();
            List<string> positiveTopics = new List<string>();
            List<string> people = new List<string>();
            List<string> companies = new List<string>();

            try
            {
                string testFileFolder = @".\TestFiles";

                IODJobBatchRequest req = new IODJobBatchRequest();
                req.ApiKey = IODApiKey;

                string filePath1 = System.IO.Path.Combine(testFileFolder, "TheFile01.txt");
                req.AddFile("file01", filePath1);

                string filePath2 = System.IO.Path.Combine(testFileFolder, "TheFile02.txt");
                req.AddFile("file02", filePath2);

                SentimentRequest sr1 = new SentimentRequest(IODSource.text, "The excellent Lebron James jumped over the lazy Kobe Bryant");
                req.AddRequest(sr1);
                sr1.OnResponse = (status, response) =>
                {
                    RecordSentiment(status, response, negativeTopics, positiveTopics);
                };

                SentimentRequest sr2 = new SentimentRequest(IODSource.file, "file01");
                req.AddRequest(sr2);
                sr2.OnResponse = (status, response) =>
                {
                    RecordSentiment(status, response, negativeTopics, positiveTopics);
                };

                EntityRequest er1 = new EntityRequest(IODSource.text, "The excellent Lebron James jumped over the lazy Kobe Bryant", "people_eng");
                req.AddRequest(er1);
                er1.OnResponse = (status, response) =>
                {
                    RecordPeople(status, response, people);
                };

                EntityRequest er2 = new EntityRequest(IODSource.file, "file01", "people_eng");
                req.AddRequest(er2);
                er2.OnResponse = (status, response) =>
                {
                    RecordPeople(status, response, people);
                };

                EntityRequest er3 = new EntityRequest(IODSource.file, "file02", "companies_eng");
                req.AddRequest(er3);
                er3.OnResponse = (status, response) =>
                {
                    RecordCompanies(status, response, companies);
                };

                EntityRequest er4 = new EntityRequest(IODSource.url, "http://www.bbc.co.uk/news/business/companies/", "companies_eng");
                req.AddRequest(er4);
                er4.OnResponse = (status, response) =>
                {
                    RecordCompanies(status, response, companies);
                };

                Console.WriteLine("Making request ...");
                req.MakeRequest();

                Console.WriteLine("\r\nResponse:");
                ListToScreen("Negative sentiment", negativeTopics);
                ListToScreen("Positive sentiment", positiveTopics);
                ListToScreen("People", people);
                ListToScreen("Companies", companies);

            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("\r\nException: {0}", e.Message));
            }

            Console.WriteLine("\r\nPress key to exit");
            Console.ReadKey();
        }