public async Task <bool> AddOrUpdateAsync(KeyPhrase keyPhrase) { var result = await Connection.GetAsync <KeyPhrase>(keyPhrase.FileName); if (result != null) { return(await Connection.UpdateAsync(keyPhrase)); } return(await Connection.InsertAsync(keyPhrase) >= 0); }
private KeyPhrase CreateWithoutDuplicates(KeyPhrase keyPhrase) { try { var phrase = GetKeyPhrase(keyPhrase.Phrase); if (phrase != null) { return(phrase); } var createdPhrase = _keyPhraseRepo.Create(keyPhrase); return(createdPhrase); } catch (Exception) { return(null); } }
static async Task KeyPhraseOrSentiment(byte[] data, List <YammerMessage> messages, bool sentiment = false) { HttpClient cli = new HttpClient(); cli.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", TextAnalyticsKey); var uri = (sentiment == true) ? "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment" : "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"; var response = await cli.PostAsync( uri, new ByteArrayContent(data)); JObject analysis = JObject.Parse(response.Content.AsString()); foreach (var document in analysis["documents"]) { var TargetMessage = messages.Where( m => m.Id.Equals(document["id"].ToString())) .SingleOrDefault(); if (TargetMessage != null) { if (sentiment) { TargetMessage.SentimentScore = Convert.ToDouble(document["score"]); } else { JArray KeyPhrases = JArray.Parse(document["keyPhrases"].ToString()); List <string> kp = new List <string>(); foreach (var KeyPhrase in KeyPhrases) { kp.Add(KeyPhrase.ToString()); } TargetMessage.KeyPhrases = kp; } } } }
private List <KeyPhrase> KeyPhraseExtraction(string textInput) { List <KeyPhrase> keys = new List <KeyPhrase>(); var response = client.ExtractKeyPhrases(textInput); // Printing key phrases Console.WriteLine("Key phrases:"); foreach (string keyphrase in response.Value) { KeyPhrase key = new KeyPhrase(); Console.WriteLine($"\t{keyphrase}"); key.Id = keyphrase; key.Text = keyphrase; keys.Add(key); } return(keys); }
public IEnumerable <KeyPhrase> CreateKeyPhraseList(List <ReviewRoot> topThreeReviewList) { var firstReviewSet = topThreeReviewList.ElementAt(0).Reviews.ElementAt(0).Text + topThreeReviewList.ElementAt(0).Reviews.ElementAt(1).Text + topThreeReviewList.ElementAt(0).Reviews.ElementAt(2).Text; var secondReviewSet = topThreeReviewList.ElementAt(1).Reviews.ElementAt(0).Text + topThreeReviewList.ElementAt(1).Reviews.ElementAt(1).Text + topThreeReviewList.ElementAt(1).Reviews.ElementAt(2).Text; var thirdReviewSet = topThreeReviewList.ElementAt(2).Reviews.ElementAt(0).Text + topThreeReviewList.ElementAt(2).Reviews.ElementAt(1).Text + topThreeReviewList.ElementAt(2).Reviews.ElementAt(2).Text; var firstYelpId = topThreeReviewList.ElementAt(0).Reviews.ElementAt(1).YelpId; var secondYelpId = topThreeReviewList.ElementAt(1).Reviews.ElementAt(1).YelpId; var thirdYelpId = topThreeReviewList.ElementAt(2).Reviews.ElementAt(1).YelpId; KeyPhrase firstKeyPhrase = new KeyPhrase("en", firstYelpId, firstReviewSet); KeyPhrase secondKeyPhrase = new KeyPhrase("en", secondYelpId, secondReviewSet); KeyPhrase thirdKeyPhrase = new KeyPhrase("en", thirdYelpId, thirdReviewSet); IEnumerable <KeyPhrase> keyPhraseList = new KeyPhrase[] { firstKeyPhrase, secondKeyPhrase, thirdKeyPhrase }; return(keyPhraseList); }
public async Task SetDiscoverSomethingTwitterAsync(TwitAuthenticateResponse twitAuthResponse, string screenName, long idMax, string idLogin) { var item = await DocumentDBRepository <Login> .GetItemAsync(idLogin); // Busca uma lista de tweets do usuário, neste caso os 100 primeiros. Após isso, é feito por thread, pegando os 100 tweets mais antigos que estes, e assim sucessivamente var timelineFormat = ""; var timelineUrl = ""; if (idMax > 0) { timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&max_id={1}&include_rts=0&exclude_replies=1&count=100"; timelineUrl = string.Format(timelineFormat, screenName, idMax); } else { timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=0&exclude_replies=1&count=100"; timelineUrl = string.Format(timelineFormat, screenName); } HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl); var timelineHeaderFormat = "{0} {1}"; timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.TokenType, twitAuthResponse.AccessToken)); timeLineRequest.Method = "Get"; WebResponse timeLineResponse = timeLineRequest.GetResponse(); var timeLineJson = string.Empty; using (timeLineResponse) { using (var reader = new StreamReader(timeLineResponse.GetResponseStream())) { timeLineJson = reader.ReadToEnd(); } } dynamic tweets = new JavaScriptSerializer().DeserializeObject(timeLineJson); int count = 0; List <string> textTweets = new List <string>(); List <string> idTweets = new List <string>(); foreach (dynamic tweet in tweets) { //Joga todos os textos dos tweets dentro de uma lista, e os ids em outra textTweets.Add(tweet["text"]); long id = tweet["id"]; idTweets.Add(id.ToString()); count++; } //Busca token de acesso para API de Translate string authToken = await GetAccessTokenTranslateAsync(); //Chama API de Tradução, traduzindo o tweet de pt para en string[] translates = TranslateArray(authToken, "pt", "en", textTweets.ToArray()); string[] ids = idTweets.ToArray(); //Monta objeto para fazer a chamada da API de análise do texto do tweet KeyPhrases KeyPhrases = new KeyPhrases(); for (int i = 0; i < translates.Length; i++) { Document document = new Document(); document.Id = ids[i]; document.Language = "en"; document.Text = translates[i]; KeyPhrases.Documents.Add(document); } //Chama API de Análise de Texto, para buscar as palavras chave da frase tweetada dynamic keyPhrasesObj = GetKeyPhrases(KeyPhrases); List <string> keyPhrases = new List <string>(); foreach (var doc in keyPhrasesObj["documents"]) { //Coloca num stringão todas as palavras chaves separadas por ", " keyPhrases.Add(string.Join(", ", doc["keyPhrases"])); } //Busca token de acesso para API de Translate authToken = await GetAccessTokenTranslateAsync(); //Chama API de Tradução, agora traduzindo as palavras chave em en para pt para mostrar corretamente pro usuário string[] translateskeyPhrases = TranslateArray(authToken, "en", "pt", keyPhrases.ToArray()); for (int i = 0; i < translateskeyPhrases.Length; i++) { References reference = new References(); reference.IdTweet = ids[i]; //tweet.TextTweet = foreach (string keyPhraseTranslate in translateskeyPhrases[i].Split(new[] { ", " }, StringSplitOptions.None)) { //Verifica se já tem no objeto uma palavra chave com o mesmo texto if (item.KeyPhrases.Count(k => k.Text == keyPhraseTranslate) > 0) { //Se já existe, somente inclui uma nova referência item.KeyPhrases.FirstOrDefault(k => k.Text == keyPhraseTranslate).References.Add(reference); } else { //Caso não exista, cria uma nova KeyPhrase keyPhrase = new KeyPhrase(); keyPhrase.Text = keyPhraseTranslate; keyPhrase.References.Add(reference); item.KeyPhrases.Add(keyPhrase); } } } await DocumentDBRepository <Login> .UpdateItemAsync(idLogin, item); //ID do último tweet retornado, para consultar os mais antigos a partir desse, na próxima vez var lastTweet = tweets[count - 1]; long id_max = lastTweet["id"]; //Inicia thread para ir enchendo a base do usuário com mais tweets /*new Thread(async () => * { * await SetDiscoverSomethingTwitterAsync(twitAuthResponse, screenName, id_max - 1, idLogin); * }).Start();*/ }
public async Task <ActionResult> StoreComment(ContactRequest request) { Contact contact = new Contact() { Id = Guid.NewGuid().ToString(), RequestDate = DateTime.Now, Sentiment = null, EmailAddress = request.EmailAddress, Name = request.Name, Surname = request.Surname, Comment = request.Comment }; if (request.ContactType == ContactType.Comment) { if (request.Comment == null || request.Comment.Length < 5) { throw new Exception("Comment length must be greater than 4 characters."); } var sentiment = await _sentimentAnalysisService.Analyze(request.Comment, "en"); contact.Sentiment = new Sentiment() { Id = Guid.NewGuid().ToString(), ContactId = contact.Id, Score = decimal.Parse(sentiment.Score.ToString()) }; var entities = await _sentimentAnalysisService.Entities(request.Comment, "en"); foreach (var entity in entities.Entities) { TextEntity tE = new TextEntity() { Id = Guid.NewGuid().ToString(), SentimentId = contact.Sentiment.Id, Name = entity.Name, Type = entity.Type, SubType = entity.SubType ?? "N/A" }; await _context.TextEntities.AddAsync(tE); foreach (var match in entity.Matches) { TextEntityMatch teM = new TextEntityMatch() { Id = Guid.NewGuid().ToString(), Score = Decimal.Parse(match.EntityTypeScore.ToString()), TextEntityId = tE.Id }; await _context.TextEntitiesMatch.AddAsync(teM); } } var keyPhrases = await _sentimentAnalysisService.KeyPhrases(request.Comment, "en"); foreach (string keyphrase in keyPhrases.KeyPhrases) { var k = new KeyPhrase() { Id = Guid.NewGuid().ToString(), SentimentId = contact.Sentiment.Id, Key = keyphrase }; await _context.KeyPhrases.AddAsync(k); } } await _context.Contacts.AddAsync(contact); await _context.SaveChangesAsync(); return(Ok()); }