Пример #1
0
        private static async Task InsertPostsSentiment(PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequest req, string MSCognitiveServicesAccessToken)
        {
            PTI.CognitiveServicesClient.CognitiveServicesClient objCSClient =
                new PTI.CognitiveServicesClient.CognitiveServicesClient(MSCognitiveServicesAccessToken);
            int totalPages = (int)Math.Ceiling((decimal)req.documents.Count() / (decimal)1000);

            for (int iPage = 0; iPage < totalPages; iPage++)
            {
                var reqBatch = req.documents.Skip(iPage * 1000).Take(1000);
                var tmpreq   = new PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequest();
                tmpreq.documents = reqBatch.ToArray();
                var sentimentResponse = await objCSClient.GetSentiment(tmpreq);

                using (MoodDetector.DataAccess.MoodDetectorContext ctx =
                           new DataAccess.MoodDetectorContext())
                {
                    ctx.Configuration.AutoDetectChangesEnabled = false;
                    foreach (var singleSentimentRecord in sentimentResponse.documents)
                    {
                        var userPost = ctx.FacebookUserPosts.Where(p => p.PostId == singleSentimentRecord.id).FirstOrDefault();
                        var objNewSentimentRecord = new DataAccess.FacebookUserPostSentiment();
                        objNewSentimentRecord.FacebookUserPostId = userPost.FacebookUserPostId;
                        objNewSentimentRecord.Score = singleSentimentRecord.score;
                        ctx.FacebookUserPostSentiments.Add(objNewSentimentRecord);
                    }
                    ctx.SaveChanges();
                }
            }
        }
Пример #2
0
        // GET: Facebook
        public async Task ImportMyPosts()
        {
            string MSCognitiveServicesAccessToken =
                System.Configuration.ConfigurationManager.AppSettings[
                    GlobalConstants.MSCSTextAnalyticsKey
                ];
            string WatsonPIUserName =
                ConfigurationManager.AppSettings[GlobalConstants.WatsonPIUserName];
            string WatsonPIPassword =
                ConfigurationManager.AppSettings[GlobalConstants.WatsonPIPassword];
            //Check https://developers.facebook.com/docs/graph-api/reference/v2.8/user/feed
            FacebookClient objClient = CreateFacebookClient();
            dynamic        myPosts   = await objClient.GetTaskAsync(@"me/posts?fields=id,application,caption,created_time,description,from,message,name,status_type,story,type");

            string myPostsJson = myPosts.ToString();

            Models.Entities.Facebook.MyPosts objMyPosts =
                Newtonsoft.Json.JsonConvert.DeserializeObject <Models.Entities.Facebook.MyPosts>
                    (myPostsJson);
            List <Models.Entities.Facebook.MyPosts> lstMyPosts =
                new List <Models.Entities.Facebook.MyPosts>();

            lstMyPosts.Add(objMyPosts);
            string userId     = objMyPosts.data.First().from.id;
            int    count      = 0;
            int    totalPosts = 0;

            //while (count++ <= 2)
            while (objMyPosts.paging != null && !String.IsNullOrWhiteSpace(objMyPosts.paging.next))
            {
                myPosts = await objClient.GetTaskAsync(objMyPosts.paging.next);

                myPostsJson = myPosts.ToString();
                objMyPosts  =
                    Newtonsoft.Json.JsonConvert.DeserializeObject <Models.Entities.Facebook.MyPosts>
                        (myPostsJson);
                lstMyPosts.Add(objMyPosts);
                //totalPosts += objMyPosts.data.Where(p => !string.IsNullOrWhiteSpace(p.message)).Count();
            }
            var allNotEmptyMessages = lstMyPosts.SelectMany(p => p.data.Where(x => !String.IsNullOrWhiteSpace(x.message)));

            totalPosts = allNotEmptyMessages.Count();
            PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequest
                sentimentReq = new PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequest();
            sentimentReq.documents =
                new PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequestDocument[totalPosts];
            TopicsRequest topicsReq = new TopicsRequest();

            topicsReq.documents = new TopicsRequestDocument[totalPosts];
            KeyPhrasesRequest keyPhrasesReq = new KeyPhrasesRequest();

            keyPhrasesReq.documents = new KeyPhrasesRequestDocument[totalPosts];
            PersonalityInsightsRequest personalityInsightsReq =
                new PersonalityInsightsRequest();

            personalityInsightsReq.contentItems =
                new Contentitem[totalPosts];
            int iPos = 0;
            var detectedLanguages =
                (await DetectLanguage(allNotEmptyMessages, MSCognitiveServicesAccessToken)).SelectMany(p => p.documents);

            foreach (var singlePost in allNotEmptyMessages)
            {
                if (!string.IsNullOrWhiteSpace(singlePost.message))
                {
                    sentimentReq.documents[iPos]      = new PTI.CognitiveServicesClient.MSCognitiveServices.Sentiment.SentimentRequestDocument();
                    sentimentReq.documents[iPos].id   = singlePost.id;
                    sentimentReq.documents[iPos].text = singlePost.message;

                    topicsReq.documents[iPos]      = new TopicsRequestDocument();
                    topicsReq.stopWords            = new string[0];
                    topicsReq.topicsToExclude      = new string[0];
                    topicsReq.documents[iPos].id   = singlePost.id;
                    topicsReq.documents[iPos].text = singlePost.message;

                    keyPhrasesReq.documents[iPos]      = new KeyPhrasesRequestDocument();
                    keyPhrasesReq.documents[iPos].id   = singlePost.id;
                    keyPhrasesReq.documents[iPos].text = singlePost.message;

                    personalityInsightsReq.contentItems[iPos] =
                        new Contentitem();
                    personalityInsightsReq.contentItems[iPos].content = singlePost.message;
                    personalityInsightsReq.contentItems[iPos].id      = singlePost.id;

                    var language = detectedLanguages.Where(p => p.id == singlePost.id).FirstOrDefault();
                    if (language != null && language.detectedLanguages.Count() > 0)
                    {
                        string languageCode = language.detectedLanguages.First().iso6391Name;
                        personalityInsightsReq.contentItems[iPos].language = languageCode;
                        sentimentReq.documents[iPos].language  = languageCode;
                        keyPhrasesReq.documents[iPos].language = languageCode;
                    }
                    iPos++;
                }
            }
            try
            {
                SaveToDatabase(lstMyPosts);
                await InsertPostsSentiment(sentimentReq, MSCognitiveServicesAccessToken);
                await InsertPostTopics(topicsReq, MSCognitiveServicesAccessToken);
                await InsertKeyPhrases(keyPhrasesReq, MSCognitiveServicesAccessToken);

                var piLanguageGroup = personalityInsightsReq.contentItems.GroupBy(p => p.language)
                                      .Select(x =>
                                              new
                {
                    Language = x.Key,
                    Value    = new PersonalityInsightsRequest()
                    {
                        contentItems = x.ToArray()
                    }
                }
                                              ).Where(p => p.Language == "en" || p.Language == "es");
                foreach (var singleLanguageRequest in piLanguageGroup)
                {
                    await InsertPersonalityInsights(singleLanguageRequest.Value, piUsername : WatsonPIUserName, piPassword : WatsonPIPassword, facebookUserId : userId);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }