private async Task InsertPostTopics(TopicsRequest topicsReq, string MSCognitiveServicesAccessToken) { PTI.CognitiveServicesClient.CognitiveServicesClient objCSClient = new PTI.CognitiveServicesClient.CognitiveServicesClient(MSCognitiveServicesAccessToken); int totalPages = (int)Math.Ceiling((decimal)topicsReq.documents.Count() / (decimal)1000); for (int iPage = 0; iPage < totalPages; iPage++) { var reqBatch = topicsReq.documents.Skip(iPage * 1000).Take(1000); var tmpreq = new PTI.CognitiveServicesClient.MSCognitiveServices.Topics.TopicsRequest(); tmpreq.stopWords = new string[0]; tmpreq.topicsToExclude = new string[0]; tmpreq.documents = reqBatch.ToArray(); await objCSClient.GetTopics(tmpreq); //var topicsResponse = await objCSClient.GetTopics(tmpreq); //using (MoodDetector.DataAccess.MoodDetectorContext ctx = // new DataAccess.MoodDetectorContext()) //{ // ctx.Configuration.AutoDetectChangesEnabled = false; // foreach (var singleTopicRecord in topicsResponse.documents) // { // var userPost = ctx.FacebookUserPosts.Where(p => p.PostId == singleTopicRecord.id).FirstOrDefault(); // var objNewSentimentRecord = new DataAccess.FacebookUserPostSentiment(); // objNewSentimentRecord.FacebookUserPostId = userPost.FacebookUserPostId; // objNewSentimentRecord.Score = singleTopicRecord.score; // ctx.FacebookUserPostSentiments.Add(objNewSentimentRecord); // } // ctx.SaveChanges(); //} } }
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(); } } }
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(); } } }