public async Task <KeyPhrasesResponse> GetKeyPhrases(KeyPhrasesRequest request) { string getKeyPhrasesUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"; HttpClient objHttpClient = new HttpClient(); string jsonRequest = Newtonsoft.Json.JsonConvert.SerializeObject(request); StringContent objStrContent = new StringContent(jsonRequest); objStrContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); objStrContent.Headers.Add("Ocp-Apim-Subscription-Key", AccessToken); var result = await objHttpClient.PostAsync(getKeyPhrasesUrl, objStrContent); var resultString = await result.Content.ReadAsStringAsync(); KeyPhrasesResponse objKeyPhrasesResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <KeyPhrasesResponse>(resultString); return(objKeyPhrasesResponse); }
private async Task InsertKeyPhrases(KeyPhrasesRequest keyPhrasesReq, string MSCognitiveServicesAccessToken) { PTI.CognitiveServicesClient.CognitiveServicesClient objCSClient = new PTI.CognitiveServicesClient.CognitiveServicesClient(MSCognitiveServicesAccessToken); int totalPages = (int)Math.Ceiling((decimal)keyPhrasesReq.documents.Count() / (decimal)1000); for (int iPage = 0; iPage < totalPages; iPage++) { var reqBatch = keyPhrasesReq.documents.Skip(iPage * 1000).Take(1000); var tmpreq = new PTI.CognitiveServicesClient.MSCognitiveServices.KeyPhrases.KeyPhrasesRequest(); tmpreq.documents = reqBatch.ToArray(); var keyPhrasesResponse = await objCSClient.GetKeyPhrases(tmpreq); using (MoodDetector.DataAccess.MoodDetectorContext ctx = new MoodDetector.DataAccess.MoodDetectorContext()) { ctx.Configuration.AutoDetectChangesEnabled = false; foreach (var singleKeyPhraseRecord in keyPhrasesResponse.documents) { if (singleKeyPhraseRecord.keyPhrases != null && singleKeyPhraseRecord.keyPhrases.Count() > 0) { var userPost = ctx.FacebookUserPosts.Where(p => p.PostId == singleKeyPhraseRecord.id).FirstOrDefault(); foreach (var singleKeyPhrase in singleKeyPhraseRecord.keyPhrases) { if (!String.IsNullOrWhiteSpace(singleKeyPhrase)) { var objNewKeyPhraseRecord = new DataAccess.FacebookUserPostKeyPhras(); objNewKeyPhraseRecord.FacebookUserPostId = userPost.FacebookUserPostId; objNewKeyPhraseRecord.KeyPhrases = singleKeyPhrase; ctx.FacebookUserPostKeyPhrases.Add(objNewKeyPhraseRecord); } else { //TODO: Add Log to this cases } } } } ctx.SaveChanges(); } } }
// 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; } }