public static List <SentimentResults> analyseLanguageUsed() {//https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/quickstarts/csharp // Create a client. ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) { Endpoint = "https://westeurope.api.cognitive.microsoft.com" //https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0 }; //Replace 'westus' with the correct region for your Text Analytics subscription Console.OutputEncoding = System.Text.Encoding.UTF8; // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); var result = client.DetectLanguageAsync(new BatchInput( new List <Input>() { new Input("1", "This is a document written in English."), new Input("2", "Este es un document escrito en Español."), new Input("3", "这是一个用中文写的文件") })).Result; List <SentimentResults> tweetLangs = new List <SentimentResults>(); // Printing language results. foreach (var document in result.Documents) { SentimentResults sr = new SentimentResults(); sr.setTweet_id(Int32.Parse(document.Id)); sr.setLanguage_short(document.DetectedLanguages[0].Iso6391Name); sr.setLanguage(document.DetectedLanguages[0].Name); tweetLangs.Add(sr); } return(tweetLangs); }
public static List <SentimentResults> fullAnalysis(List <DB_Service.CrimeTweets> crimeTweets) {//------------------------------------------------------------------------------------------------------------------------------------- // Create a client. ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) { Endpoint = "https://westeurope.api.cognitive.microsoft.com" //https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0 }; //------------------------------------------------------------------------------------------------------------------------------------- // Extracting language List <SentimentResults> tweetLangs = new List <SentimentResults>(); List <Input> myInp = new List <Input>(); foreach (DB_Service.CrimeTweets ct in crimeTweets) { Input inp = new Input(ct.tweet_id.ToString(), ct.message); SentimentResults sr = new SentimentResults(); sr.setTweet_id(ct.tweet_id); tweetLangs.Add(sr); myInp.Add(inp); } var result = client.DetectLanguageAsync(new BatchInput(myInp)).Result; // Printing language results. int count = 0; foreach (var document in result.Documents) { SentimentResults sr = new SentimentResults(); sr = tweetLangs.ElementAt <SentimentResults>(count); sr.setLanguage_short(document.DetectedLanguages[0].Iso6391Name); sr.setLanguage(document.DetectedLanguages[0].Name); tweetLangs.Add(sr); if ((count + 1) != tweetLangs.Count) { count++; } } //------------------------------------------------------------------------------------------------------------------------------------- // Getting key-phrases List <MultiLanguageInput> keyPhrases = new List <MultiLanguageInput>();//Key phrases count = 0; foreach (DB_Service.CrimeTweets ct in crimeTweets) { string tempLang = tweetLangs.ElementAt <SentimentResults>(count).getLanguage_short(); MultiLanguageInput inp = new MultiLanguageInput(tempLang, ct.tweet_id.ToString(), ct.message); keyPhrases.Add(inp); if ((count + 1) != tweetLangs.Count) { count++; } } //new List<MultiLanguageInput>() // { // new MultiLanguageInput("ja", "1", "猫は幸せ"), // new MultiLanguageInput("de", "2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu."), // new MultiLanguageInput("en", "3", "My cat is stiff as a rock."), // new MultiLanguageInput("es", "4", "A mi me encanta el fútbol!") // })).Result; KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(keyPhrases)).Result; // Printing keyphrases List <SentimentResults> tweetKeyPhrases = new List <SentimentResults>(); count = 0; foreach (var document in result2.Documents) { List <string> phrases = new List <string>(); foreach (string keyphrase in document.KeyPhrases) { phrases.Add(keyphrase); } SentimentResults sr = new SentimentResults(); sr = tweetLangs.ElementAt <SentimentResults>(count); sr.setKeyPhrases(phrases); tweetKeyPhrases.Add(sr); if ((count + 1) != tweetLangs.Count) { count++; } } //------------------------------------------------------------------------------------------------------------------------------------- // Getting Sentiment Analysis List <MultiLanguageInput> sentiAni = new List <MultiLanguageInput>();//Sentiment Analysis count = 0; foreach (DB_Service.CrimeTweets ct in crimeTweets) { string tempLang = tweetKeyPhrases.ElementAt <SentimentResults>(count).getLanguage_short(); MultiLanguageInput inp = new MultiLanguageInput(tempLang, ct.tweet_id.ToString(), ct.message); sentiAni.Add(inp); if ((count + 1) != tweetKeyPhrases.Count) { count++; } } SentimentBatchResult result3 = client.SentimentAsync(new MultiLanguageBatchInput(sentiAni)).Result; // Printing sentiment results List <SentimentResults> tweetSentiments = new List <SentimentResults>(); count = 0; foreach (var document in result3.Documents) { SentimentResults sr = new SentimentResults(); sr = tweetKeyPhrases.ElementAt <SentimentResults>(count); sr.setSenti_score((double)document.Score); tweetSentiments.Add(sr); if ((count + 1) != tweetKeyPhrases.Count) { count++; } } //------------------------------------------------------------------------------------------------------------------------------------- // Getting Entities //Continue using the same list so languages wont change EntitiesBatchResult result4 = client.EntitiesAsync(new MultiLanguageBatchInput(sentiAni)).Result; // Printing entities results List <SentimentResults> tweetEntities = new List <SentimentResults>(); count = 0; foreach (var document in result4.Documents) { List <string> entitiySet = new List <string>(); foreach (EntityRecord entitiy in document.Entities) { entitiySet.Add(entitiy.Name); } SentimentResults sr = new SentimentResults(); sr = tweetSentiments.ElementAt <SentimentResults>(count); sr.setEntities(entitiySet); tweetEntities.Add(sr); if ((count + 1) != tweetSentiments.Count) { count++; } } //------------------------------------------------------------------------------------------------------------------------------------- //Add Data to Database Service List <DB_Service.Sentiments> completeSentiments = new List <DB_Service.Sentiments>(); List <DB_Service.Entities> completeEntities = new List <DB_Service.Entities>(); foreach (SentimentResults finalResults in tweetEntities) { //Start building Sentiment class DB_Service.Sentiments newSenti = new DB_Service.Sentiments(); newSenti.tweet_id = finalResults.getTweet_id(); newSenti.sentiment_total = finalResults.getSenti_score(); newSenti.category_primary = finalResults.getLanguage() + ", " + finalResults.getLanguage_short(); List <string> entList = finalResults.getEntities(); List <string> phraseList = finalResults.getKeyPhrases(); StringBuilder wholePhrase = new StringBuilder(""); count = 0; //Start building Entity Class if (entList != null && entList.Count > 0) { foreach (string entity in entList) { wholePhrase.Append(entity + ","); //DB_Service.Entities newEntity = new DB_Service.Entities(); //newEntity.name = entity; //newEntity.sentiment_id = -1//this is a programming design problem } } if (phraseList != null && phraseList.Count > 0) { foreach (string word in phraseList) { count++; if (phraseList.Count > count) { wholePhrase.Append(word + ","); } else { wholePhrase.Append(word); } } } newSenti.key_phrases = wholePhrase.ToString(); //List<string> EntList = finalResults.getEntities(); //if(EntList != null && EntList.Count > 0) //{ // //newSenti.category_primary = EntList.ElementAt<string>(0); //} //else //{ // newSenti.category_primary = ""; //} //Finish building Sentiment Class completeSentiments.Add(newSenti); } //Add to service now DB_Service.ServiceClient service = new DB_Service.ServiceClient(); service.addSentiments(completeSentiments); return(tweetEntities); }