public static async Task <DetectLanguageResult> GetDetectedLanguageAsync(string input) { DetectLanguageResult languageResult = new DetectLanguageResult() { Language = new Dictionary <string, string>() }; if (!string.IsNullOrEmpty(input)) { LanguageBatchResult result = await AnalyticsClient.DetectLanguageAsync(new BatchInput( new List <Input>() { new Input("0", input) })); if (result.Documents != null) { languageResult.Language.Add("iso6391Name", result.Documents[0].DetectedLanguages[0].Iso6391Name); languageResult.Language.Add("name", result.Documents[0].DetectedLanguages[0].Name); languageResult.Language.Add("score", result.Documents[0].DetectedLanguages[0].Score.ToString()); } if (result.Errors != null) { // Just return the empty Dictionary } } return(languageResult); }
/// <summary> /// This method gets the list of inputs and sends them off to the API /// to get a list of language detection results returned. /// </summary> /// <param name="Inputs"></param> public void DetectLanguages(List <Input> Inputs) { if (Inputs == null) { return; } List <List <Input> > Lists = SplitIntoChunks(Inputs, 1000); try { for (int i = 0; i < Lists.Count; i++) { LanguageBatchResult result = null; result = client.DetectLanguageAsync(new BatchInput(Lists[i])).Result; foreach (var document in result.Documents) { if (document.DetectedLanguages[0].Name != null) { LanguagesList.Add("Document ID: " + document.Id + ", Language: " + document.DetectedLanguages[0].Name); LanguageOccurrences.Add(document.DetectedLanguages[0].Name); } } System.Threading.Thread.Sleep(delay); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Error: " + ex.Message); } }
private async Task <double?> MakeAnalysisRequest(string text) { LanguageBatchResult result = await client.DetectLanguageAsync( new BatchInput( new List <Input>() { new Input("1", text) })); var detectedLanguage = result.Documents.First().DetectedLanguages.First(); var englishProbability = detectedLanguage.Name == "English" ? detectedLanguage.Score : 0; SentimentBatchResult sentimentResult = await client.SentimentAsync( new MultiLanguageBatchInput(new List <MultiLanguageInput>() { new MultiLanguageInput(detectedLanguage.Iso6391Name, "1", text) })); double?sentiment = 0; if (sentimentResult.Documents.Any()) { sentiment = sentimentResult.Documents.First().Score; } return((englishProbability + sentiment) / 2); }
public void printLanguage(LanguageBatchResult result) { // Print languages Console.WriteLine("===== LANGUAGE EXTRACTION ======"); foreach (var document in result.Documents) { Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name); } }
private void DetectLanguage(string userInput) { LanguageBatchResult language = client.DetectLanguage(new BatchInput(new List <Input> { new Input("1", userInput) })); foreach (LanguageBatchResultItem document in language.Documents) { Console.WriteLine("Inserted text is written in {0} language.\n", document.DetectedLanguages[0].Name); } }
private void getLanguage() { LanguageBatchResult result = Client.DetectLanguage( new BatchInput( new List <Input>() { new Input(Id, Text) })); Lang = result.Documents[0].DetectedLanguages[0]; getKeyWords(); }
public string GetLanguage(string text) { LanguageBatchResult result = _AnalyticsClient.DetectLanguage( new BatchInput( new List <Input>() { new Input("1", text) })); if (result.Documents.Any()) { return(result.Documents.First().DetectedLanguages.First().Iso6391Name); } return("en"); }
public void DetectLanguage() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { HttpMockServer.Initialize(this.GetType().FullName, "DetectLanguage"); ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance()); LanguageBatchResult result = client.DetectLanguage( "I love my team mates"); Assert.Equal("English", result.Documents[0].DetectedLanguages[0].Name); Assert.Equal("en", result.Documents[0].DetectedLanguages[0].Iso6391Name); Assert.True(result.Documents[0].DetectedLanguages[0].Score > 0.7); context.Stop(); } }
public LanguageBatchResult DetectLanguageServiceForAString(string text) { ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; client.SubscriptionKey = ""; LanguageBatchResult result = client.DetectLanguage(new BatchInput( new List <Input> { new Input("0", text) })); return(result); }
public MultiLanguageBatchInput getMultiLanguageBatchInput() { LanguageBatchResult langResult = getLanguage(); MultiLanguageBatchInput bi = new MultiLanguageBatchInput(); bi.Documents = new List <MultiLanguageInput>(); int i = 0; foreach (string s in text) { bi.Documents.Add(new MultiLanguageInput(langResult.Documents[i].DetectedLanguages[0].Iso6391Name, i.ToString(), s)); i++; } return(bi); }
/// <summary> /// Analyzes all the given sentences and queries them against the different apis /// </summary> /// <param name="origin"></param> /// <param name="URLsources"></param> /// <param name="Sources"></param> /// <returns></returns> public List <Plagiat <string> > Check(List <string> origin, List <Uri> URLsources, List <String> Sources) { //google search with custom search GoogleSearch googleSearch = new GoogleSearch(new Uri("https://www.googleapis.com/customsearch/v1")); //europa pmc search for academic literatur in life science EuropaPMCSearch europaPMCSearch = new EuropaPMCSearch(new Uri("https://www.ebi.ac.uk/europepmc/webservices/rest/search?")); //create document statistics DocumentStatistics documentStatistics = new DocumentStatistics(origin); googleSearch.Check(documentStatistics); europaPMCSearch.Check(documentStatistics); //starting azure congitive services to interpret sentence // Create a client ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; client.SubscriptionKey = "<placekey>"; // Extracting language LanguageBatchResult languagesDetected = client.DetectLanguage( new BatchInput(documentStatistics.getBatchInput()) ); //store results foreach (var document in languagesDetected.Documents) { documentStatistics.updateSentenceLanguage(document.Id, document.DetectedLanguages[0].Iso6391Name); } // Getting key-phrases KeyPhraseBatchResult keyPhares = client.KeyPhrases( new MultiLanguageBatchInput(documentStatistics.getMultiLanguageBatchInput()) ); // Printing keyphrases foreach (var document in keyPhares.Documents) { documentStatistics.updateKeyPhares(document.Id, (List <string>)document.KeyPhrases); } return(documentStatistics.getPossiblePlagiates()); }
public async Task <string> DetectLanguageAsync(string text) { Console.OutputEncoding = System.Text.Encoding.UTF8; LanguageBatchResult result = await _client.DetectLanguageAsync( new BatchInput( new List <Input>() { new Input("1", text) }), numberOfLanguagesToDetect : 1); if (result.Documents.FirstOrDefault() != null && result.Documents.First().DetectedLanguages.FirstOrDefault() != null) { return(result.Documents.First().DetectedLanguages.First().Name); } return(_defaultLanguage); }
public async Task <string> GetMessageLanguageAsync(string message) { var language = string.Empty; LanguageBatchResult result = await client.DetectLanguageAsync( new BatchInput( new List <Input>() { new Input("1", message) } )); if (result.Documents.Count > 0) { language = result.Documents[0].DetectedLanguages[0].Iso6391Name; } return(language); }
public async Task LanguageBatchAsync() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "LanguageBatchAsync"); ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance()); LanguageBatchResult result = await client.DetectLanguageBatchAsync( new LanguageBatchInput( new List <LanguageInput>() { new LanguageInput("en", "id", "I love my team mates") })); Assert.Equal("English", result.Documents[0].DetectedLanguages[0].Name); Assert.Equal("en", result.Documents[0].DetectedLanguages[0].Iso6391Name); Assert.True(result.Documents[0].DetectedLanguages[0].Score > 0.7); context.Stop(); } }
public LanguageBatchResult DetectLanguageService(IList <string> textList) { ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; //SK not valid anymore client.SubscriptionKey = ""; var listInput = new List <Input>(); for (int i = 0; i < textList.Count; i++) { var input = new Input(i.ToString(), textList[i]); listInput.Add(input); } LanguageBatchResult result = client.DetectLanguage(new BatchInput(listInput)); return(result); }
public void Language() { // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); LanguageBatchResult result = client.DetectLanguage( 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", "这是一个用中文写的文件") })); // Printing language results. foreach (var document in result.Documents) { Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name); } }
private static void DetectLanguage(ITextAnalyticsAPI client) { // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); var inputs1 = new List <Input>() { new Input("1", "This is a document written in English."), new Input("2", "C'est un fichier écrit en français."), new Input("3", "这是一个用中文写的文件") }; LanguageBatchResult result = client.DetectLanguage(new BatchInput(inputs1)); // Printing language results. foreach (var document in result.Documents) { Console.WriteLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name} , Text: {inputs1.FirstOrDefault(i => i.Id == document.Id).Text}"); } }
public static async Task <double?> GetSentiment(string inputText, string analyticsID) { // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westcentralus; client.SubscriptionKey = "2763d3dab1404c1e99b75c283f9642b0"; // Extracting language LanguageBatchResult resultLanguage = client.DetectLanguage( new BatchInput( new List <Input>() { new Input(analyticsID, inputText), })); // Printing language results. LanguageBatchResultItem docItem = resultLanguage.Documents[0]; // perchè analizzo solo una frase per volta string language = string.Empty; if (docItem.DetectedLanguages[0].Name.Equals("english")) { language = "en"; } //Extracting sentiment SentimentBatchResult resultSentiment = client.Sentiment( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(language, docItem.Id, inputText), })); return(resultSentiment.Documents[0].Score); }
public TranslatedItem DetectLanguage(String wordToDetect) { LanguageBatchResult result = _client.DetectLanguage( new BatchInput( new List <Input>() { new Input("1", wordToDetect) })); // transform the results into a recognizeable model TranslatedItem word = new TranslatedItem { OrignalWord = wordToDetect, DetectedLanguage = result.Documents[0].DetectedLanguages[0].Name, NonEnglishWord = false, ISOName = result.Documents[0].DetectedLanguages[0].Iso6391Name }; if (result.Documents[0].DetectedLanguages[0].Name != "English") { word.NonEnglishWord = true; } return(word); }
public TextAnalyticsModel TextAnalysis(string value) { TextAnalyticsModel objTextAnalyticsModel = new TextAnalyticsModel(); // Extracting language //"===== LANGUAGE EXTRACTION ======"); LanguageBatchResult result = client.DetectLanguage( new BatchInput( new List <Input>() { new Input("1", value) }) ); StringBuilder strbld = new StringBuilder(); string Language = string.Empty; var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures); string TwoLetterISOLanguageName = string.Empty; // Printing language results. foreach (var document in result.Documents) { //strbld.Append(" Language: " + document.DetectedLanguages[0].Name); Language = document.DetectedLanguages[0].Name; objTextAnalyticsModel.Language = document.DetectedLanguages[0].Name; TwoLetterISOLanguageName = allCultures.FirstOrDefault(c => c.DisplayName == Language).Name; } // Getting key-phrases //Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); KeyPhraseBatchResult result2 = client.KeyPhrases( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(TwoLetterISOLanguageName, "1", value) })); // Printing keyphrases foreach (var document in result2.Documents) { // strbld.Append(" Key Phrases: "); foreach (string keyphrase in document.KeyPhrases) { strbld.Append(keyphrase + ", "); //Console.WriteLine("\t\t" + keyphrase); objTextAnalyticsModel.KeyPhrases = strbld.ToString(); } } // Extracting sentiment //Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======"); SentimentBatchResult result3 = client.Sentiment( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(TwoLetterISOLanguageName, "0", value) })); // Printing sentiment results foreach (var document in result3.Documents) { //Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score); strbld.Append(" Sentiment Score: " + document.Score); objTextAnalyticsModel.SentimentScore = document.Score.ToString(); } return(objTextAnalyticsModel); }
public static async Task RunSampleAsync(string endpoint, string key) { // Create a client. ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; Console.OutputEncoding = System.Text.Encoding.UTF8; // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); LanguageBatchResult result = await 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", "这是一个用中文写的文件") })); // Printing language results. foreach (var document in result.Documents) { Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name); } // Getting key-phrases Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); KeyPhraseBatchResult result2 = await client.KeyPhrasesAsync( new MultiLanguageBatchInput( 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!") })); // Printing keyphrases foreach (var document in result2.Documents) { Console.WriteLine("Document ID: {0} ", document.Id); Console.WriteLine("\t Key phrases:"); foreach (string keyphrase in document.KeyPhrases) { Console.WriteLine("\t\t" + keyphrase); } } // Extracting sentiment Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======"); SentimentBatchResult result3 = await client.SentimentAsync( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput("en", "0", "I had the best day of my life."), new MultiLanguageInput("en", "1", "This was a waste of my time. The speaker put me to sleep."), new MultiLanguageInput("es", "2", "No tengo dinero ni nada que dar..."), new MultiLanguageInput("it", "3", "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."), })); // Printing sentiment results foreach (var document in result3.Documents) { Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score); } // Extracting entities Console.WriteLine("\n\n===== Entity Extraction ======"); EntitiesBatchResultV2dot1 result4 = await client.EntitiesAsync( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput("en", "0", "Microsoft released win10. Microsoft also released Hololens"), new MultiLanguageInput("en", "1", "Microsoft is an IT company."), new MultiLanguageInput("es", "2", "Microsoft lanzó win10. Microsoft también lanzó Hololens"), new MultiLanguageInput("es", "3", "Microsoft es una empresa de TI."), })); // Printing entity extraction results foreach (var document in result4.Documents) { Console.WriteLine("Document ID: {0} ", document.Id); Console.WriteLine("\t Entities:"); foreach (EntityRecordV2dot1 entity in document.Entities) { Console.WriteLine("\t\tEntity Name: {0}", entity.Name); Console.WriteLine("\t\tWikipedia Language: {0}", entity.WikipediaLanguage); Console.WriteLine("\t\tWikipedia Url: {0}", entity.WikipediaUrl); Console.WriteLine("\t\tNumber of times appeared on the text: {0}", entity.Matches.Count); Console.WriteLine("\t\tEntity Type: {0}", entity.Type); Console.WriteLine("\t\tEntity SubType: {0}", entity.SubType); Console.WriteLine("\n"); } } }
public static StoryLine createStory(string userInput) { // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westus; client.SubscriptionKey = "49bd3a3a1a244fd289aa30b7a5594b05"; Console.OutputEncoding = System.Text.Encoding.UTF8; // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); // CALLING TAMMY's FUNCTION TO GET THE USER INPUT STRING string inputString = "I live in HongKong for nine years now. After that, I went to UCSD for college. I miss food from hometown."; // Split each line according to period. Afterr the speech ends, return an empty string. string [] singleLine = inputString.Split('.'); string [] keyWordResult = new string[singleLine.Length]; double [] sentimentResult = new double[singleLine.Length]; List <Input> inputLine = new List <Input>(); int count = 0; foreach (var line in singleLine) { //Console.WriteLine($"<{line}>"); inputLine.Add(new Input(count.ToString(), line)); count++; } string[] languages = new string[inputLine.Count]; LanguageBatchResult result = client.DetectLanguage( new BatchInput( inputLine )); // Updating language results. count = 0; foreach (var document in result.Documents) { //Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Iso6391Name); languages[count] = document.DetectedLanguages[0].Iso6391Name; count++; } // Getting key-phrases Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); int languageCount = 0; count = 0; List <MultiLanguageInput> inputKeywordLine = new List <MultiLanguageInput>(); foreach (var key in singleLine) { //Console.WriteLine("The language is: {0}, The count is {1}, the key is {2}", languages[languageCount], count.ToString(),key); inputKeywordLine.Add(new MultiLanguageInput(languages[languageCount], count.ToString(), key)); count++; } KeyPhraseBatchResult result2 = client.KeyPhrases( new MultiLanguageBatchInput( inputKeywordLine )); // Printing keyphrases foreach (var document in result2.Documents) { //Console.WriteLine("Document ID: {0} ", document.Id); //Console.WriteLine("\t Key phrases: {0}", document.KeyPhrases[0]); keyWordResult[Int32.Parse(document.Id)] = document.KeyPhrases[0]; //Console.WriteLine(keyWordResult[Int32.Parse(document.Id)]); /* * foreach (string keyphrase in document.KeyPhrases) * { * Console.WriteLine("\t\t" + keyphrase); * } */ } // Extracting sentiment Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======"); SentimentBatchResult result3 = client.Sentiment( new MultiLanguageBatchInput( inputKeywordLine /* * new List<MultiLanguageInput>() * { * new MultiLanguageInput("en", "1", "I live in HongKong for nine years now."), * new MultiLanguageInput("en", "2", "After that, I went to UCSD for college."), * new MultiLanguageInput("en", "3", " I miss food from hometown."), * }*/ )); // Printing sentiment results foreach (var document in result3.Documents) { sentimentResult[Int32.Parse(document.Id)] = Convert.ToDouble(document.Score); //Console.WriteLine(sentimentResult[Int32.Parse(document.Id)]); } return(new StoryLine(languages, keyWordResult, sentimentResult)); }
//public List<SentimentResults> extractingKeyPhrases(List<GuestGeek_DBService.CrimeTweets> crimeTweets) //{ // // Create a client.--------------------------------------------------------------------------------------------------------------------------------- // ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) // { // Endpoint = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/" // }; // System.Console.OutputEncoding = System.Text.Encoding.UTF8; // // Getting key phrases.--------------------------------------------------------------------------------------------------------------------------------- // Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); // List<MultiLanguageInput> keyPhrases = new List<MultiLanguageInput>(); // foreach (GuestGeek_DBService.CrimeTweets ct in crimeTweets) // { // MultiLanguageInput inp = new MultiLanguageInput(; // myInp.Add(inp); // } // KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput( // 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; // // Printing key phrases. // foreach (var document in result2.Documents) // { // Console.WriteLine("Document ID: {0} ", document.Id); // Console.WriteLine("\t Key phrases:"); // foreach (string keyphrase in document.KeyPhrases) // { // Console.WriteLine("\t\t" + keyphrase); // } // } //} //static async void MakeRequest() //{ // var client = new HttpClient(); // var queryString = HttpUtility.ParseQueryString(string.Empty); // // Request headers // client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", TwitterKeys.getTextAnalyticsKey()); // var uri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages?" + queryString; // HttpResponseMessage response; // // Request body // byte[] byteData = Encoding.UTF8.GetBytes("{body}"); // using (var content = new ByteArrayContent(byteData)) // { // content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // response = await client.PostAsync(uri, content); // } //} public List <SentimentResults> fullAnalysis(List <DB_Service.CrimeTweets> crimeTweets) { // Create a client.--------------------------------------------------------------------------------------------------------------------------------- ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) { Endpoint = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/" }; System.Console.OutputEncoding = System.Text.Encoding.UTF8; // Extracting language.--------------------------------------------------------------------------------------------------------------------------------- Console.WriteLine("===== LANGUAGE EXTRACTION ======"); List <Input> myInp = new List <Input>();//Languages foreach (DB_Service.CrimeTweets ct in crimeTweets) { //Sentiment.Sentiment.sanitize(ct.message); Input inp = new Input(ct.tweet_id.ToString(), ct.message); myInp.Add(inp); } LanguageBatchResult result = null; try { result = client.DetectLanguageAsync(new BatchInput(myInp)).Result; //Task.Run(() => client.DetectLanguageAsync(new BatchInput(myInp)).Result).Wait(); } catch (AggregateException aex) { string messages = ""; foreach (Exception ex in aex.InnerExceptions) { messages += ex.Message + "\r\n"; } Debug.WriteLine(messages); } // Printing language results. List <SentimentResults> tweetLangs = new List <SentimentResults>();//Language foreach (var document in result.Documents) { SentimentResults tr = new SentimentResults(); tr.language_short = document.DetectedLanguages[0].Iso6391Name; tr.tweet_id = Int32.Parse(document.Id); tr.language = document.DetectedLanguages[0].Name; tweetLangs.Add(tr); } // Getting key phrases.--------------------------------------------------------------------------------------------------------------------------------- Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); List <MultiLanguageInput> keyPhrases = new List <MultiLanguageInput>();//Key phrases int count = 0; foreach (DB_Service.CrimeTweets ct in crimeTweets) { string tempLang = tweetLangs.ElementAt <SentimentResults>(count).language_short; MultiLanguageInput inp = new MultiLanguageInput(tempLang, ct.tweet_id.ToString(), ct.message); keyPhrases.Add(inp); count++; } KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(keyPhrases)).Result; // Printing key phrases. List <string> phrases = new List <string>(); List <SentimentResults> tweetKeyPhrases = new List <SentimentResults>(); count = 0; foreach (var document in result2.Documents) { //Console.WriteLine("Document ID: {0} ", document.Id); //Console.WriteLine("\t Key phrases:"); foreach (string keyphrase in document.KeyPhrases) { //Console.WriteLine("\t\t" + keyphrase); phrases.Add(keyphrase); } SentimentResults sr = new SentimentResults(); sr = tweetLangs.ElementAt <SentimentResults>(count); sr.keyPhrases = phrases; tweetKeyPhrases.Add(sr); count++; } // Analyzing sentiment.--------------------------------------------------------------------------------------------------------------------------------- Console.WriteLine("\n\n===== 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).language_short; MultiLanguageInput inp = new MultiLanguageInput(tempLang, ct.tweet_id.ToString(), ct.message); sentiAni.Add(inp); 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) { //Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score); SentimentResults sr = new SentimentResults(); sr = tweetKeyPhrases.ElementAt <SentimentResults>(count); sr.senti_score = (double)document.Score; tweetSentiments.Add(sr); count++; } List <DB_Service.Sentiments> completeSentiments = new List <DB_Service.Sentiments>(); foreach (SentimentResults finalResults in tweetSentiments) { DB_Service.Sentiments newSenti = new DB_Service.Sentiments(); newSenti.tweet_id = finalResults.tweet_id; newSenti.sentiment_total = finalResults.senti_score; newSenti.category_primary = finalResults.language + ", " + finalResults.language_short; StringBuilder wholePhrase = new StringBuilder(""); count = 0; foreach (String word in finalResults.keyPhrases) { count++; if (finalResults.keyPhrases.Count > count) { wholePhrase.Append(word + ","); } else { wholePhrase.Append(word); } } completeSentiments.Add(newSenti); } DB_Service.ServiceClient service = new DB_Service.ServiceClient(); service.addSentiments(completeSentiments); return(tweetSentiments); // Linking entities--------------------------------------------------------------------------------------------------------------------------------- //Console.WriteLine("\n\n===== ENTITY LINKING ======"); //EntitiesBatchResult result4 = client.EntitiesAsync( // new MultiLanguageBatchInput( // new List<MultiLanguageInput>() // { // new MultiLanguageInput("en", "0", "I really enjoy the new XBox One S. It has a clean look, it has 4K/HDR resolution and it is affordable."), // new MultiLanguageInput("en", "1", "The Seattle Seahawks won the Super Bowl in 2014."), // })).Result; //// Printing entity results. //foreach (var document in result4.Documents) //{ // Console.WriteLine("Document ID: {0} , Entities: {1}", document.Id, String.Join(", ", document.Entities.Select(entity => entity.Name))); //} }
static void Main(string[] args) { // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI { AzureRegion = AzureRegions.Westus, SubscriptionKey = "your subscription key" }; Console.OutputEncoding = System.Text.Encoding.UTF8; // Extracting language Console.WriteLine("===== LANGUAGE EXTRACTION ======"); LanguageBatchResult result = client.DetectLanguage( 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", "这是一个用中文写的文件") })); // Printing language results. foreach (var document in result.Documents) { Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name); } // Getting key-phrases Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======"); KeyPhraseBatchResult result2 = client.KeyPhrases( new MultiLanguageBatchInput( 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!") })); // Printing keyphrases foreach (var document in result2.Documents) { Console.WriteLine("Document ID: {0} ", document.Id); Console.WriteLine("\t Key phrases:"); foreach (string keyphrase in document.KeyPhrases) { Console.WriteLine("\t\t" + keyphrase); } } // Extracting sentiment Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======"); SentimentBatchResult result3 = client.Sentiment( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput("en", "0", "I had the best day of my life."), new MultiLanguageInput("en", "1", "This was a waste of my time. The speaker put me to sleep."), new MultiLanguageInput("es", "2", "No tengo dinero ni nada que dar..."), new MultiLanguageInput("it", "3", "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."), })); // Printing sentiment results foreach (var document in result3.Documents) { Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score); } }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; client.SubscriptionKey = Environment.GetEnvironmentVariable("clientSubscriptionKey"); _inputCount++; //Luis var httpClient = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); var luisAppId = Environment.GetEnvironmentVariable("luisAppId"); var subscriptionKey = Environment.GetEnvironmentVariable("subscriptionKey"); // The request header contains your subscription key httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // The "q" parameter contains the utterance to send to LUIS queryString["q"] = activity.Text; // These optional request parameters are set to their default values queryString["timezoneOffset"] = "0"; queryString["verbose"] = "false"; queryString["spellCheck"] = "false"; queryString["staging"] = "false"; var uri = "https://westeurope.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString; var response = await httpClient.GetAsync(uri); string lang = null; var dataFromLuis = JsonConvert.DeserializeObject <LuisResponse>(response.Content.ReadAsStringAsync().Result); if (dataFromLuis.entities.Length > 0) { lang = dataFromLuis.entities[0].entity; } //Finne språket LanguageBatchResult res = client.DetectLanguage( new BatchInput( new List <Input> { new Input(_inputCount.ToString(), activity.Text) })); // calculate something for us to return int length = (activity.Text ?? string.Empty).Length; StringBuilder keyWordBuilder = new StringBuilder(); keyWordBuilder.Append(" "); // Printing language results. foreach (var document in res.Documents) { //Finne nøkkelfraser KeyPhraseBatchResult res2 = client.KeyPhrases( new MultiLanguageBatchInput( new List <MultiLanguageInput> { new MultiLanguageInput(document.DetectedLanguages[0].Iso6391Name, _inputCount.ToString(), activity.Text) })); // Printing keyphrases foreach (var doc2 in res2.Documents) { foreach (string keyphrase in doc2.KeyPhrases) { keyWordBuilder.Append(keyphrase + " "); } if (doc2.KeyPhrases.Count == 0) { keyWordBuilder.Append("Fant ingen nøkkelfraser"); } } // Extracting sentiment SentimentBatchResult res3 = client.Sentiment( new MultiLanguageBatchInput( new List <MultiLanguageInput> { new MultiLanguageInput(document.DetectedLanguages[0].Iso6391Name, _inputCount.ToString(), activity.Text) })); // Printing sentiment results foreach (var doc3 in res3.Documents) { ConsultantResponse dataFromResponsefromConsultant = null; var httpConsultant = new HttpClient(); httpConsultant .DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header if (lang != null) { var responsefromConsultant = await httpConsultant.GetAsync( $"http://37.139.15.166/consultants?skill={dataFromLuis.entities[0].entity}"); dataFromResponsefromConsultant = JsonConvert.DeserializeObject <ConsultantResponse>( responsefromConsultant.Content.ReadAsStringAsync().Result); } string returnConsultantIfData(ConsultantResponse cr) { int count = 0; if (cr != null && cr.consultants.Length > 0) { StringBuilder cnBuilder = new StringBuilder(); cnBuilder.AppendLine( $"I hear you are looking for people that know {dataFromLuis.entities[0].entity} programming language. In our resource database i found: "); foreach (var c in cr.consultants) { cnBuilder.Append(c.name); count++; if (count < cr.consultants.Length) { cnBuilder.Append(", "); } } return(cnBuilder.ToString()); } return(null); } var textInput = activity.Text; var langIs = document.DetectedLanguages[0].Name; var keyFrases = keyWordBuilder.ToString().TrimEnd(); var emotionScoreIs = $"{doc3.Score:0.00}"; bool onlyLuisApi = true; StringBuilder responsBuilder = new StringBuilder(); if (onlyLuisApi) { //Only luis fetch programming skills responsBuilder. Append(returnConsultantIfData(dataFromResponsefromConsultant)); } else { //With detect language, sentiment, key frases and luis programming skills responsBuilder. Append("Hello! You wrote "). AppendLine(textInput + "."). Append("The language is most likely: "). AppendLine(langIs + "."). Append("The key frases are: "). AppendLine(keyFrases + "."). Append("Based what you wrote i detected the sentiment score: "). AppendLine(emotionScoreIs + " On a scale between 0-1, where 0 is the most negative(sad) and 1 is most positive(happy)."). Append(returnConsultantIfData(dataFromResponsefromConsultant)); } // return our reply to the user if (responsBuilder.Length > 0) { await context.PostAsync(responsBuilder.ToString()); } } } context.Wait(MessageReceivedAsync); }
public string Get(string textToInspect) { // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; client.SubscriptionKey = config["TextAnalysisKey"]; StringBuilder sb = new StringBuilder(); // Extracting language sb.AppendLine("===== LANGUAGE EXTRACTION ======"); LanguageBatchResult result = client.DetectLanguage( new BatchInput( new List <Input>() { new Input("1", textToInspect), })); // Printing language results. foreach (var document in result.Documents) { sb.AppendLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}"); // Getting key-phrases sb.AppendLine("\n\n===== KEY-PHRASE EXTRACTION ======"); var isoLanguageName = document.DetectedLanguages[0].Iso6391Name; KeyPhraseBatchResult phraseResult = client.KeyPhrases( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(isoLanguageName, "1", textToInspect), })); var phrasesFound = phraseResult.Documents.FirstOrDefault(); if (phrasesFound == null) { throw new Exception("Failed processing message - no phrase result"); } sb.AppendLine($"Document ID: {phrasesFound.Id} "); sb.AppendLine("\t Key phrases:"); foreach (string keyphrase in phrasesFound.KeyPhrases) { sb.AppendLine("\t\t" + keyphrase); var entitySearchApi = new EntitySearchAPI(new ApiKeyServiceClientCredentials(config["EntityKey"])); var entityData = entitySearchApi.Entities.Search(keyphrase); if (entityData?.Entities?.Value?.Count > 0) { // find the entity that represents the dominant one var mainEntity = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DominantEntity).FirstOrDefault(); if (mainEntity != null) { sb.AppendLine($"Searched for {keyphrase} and found a dominant entity with this description:"); sb.AppendLine(mainEntity.Description); } else { sb.AppendLine($"Couldn't find a main entity for {keyphrase}"); } } else { sb.AppendLine($"No data returned for entity {keyphrase}"); } } // Extracting sentiment sb.AppendLine("\n\n===== SENTIMENT ANALYSIS ======"); SentimentBatchResult sentimentResult = client.Sentiment( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(isoLanguageName, "0", textToInspect), })); var sentiment = sentimentResult.Documents.FirstOrDefault(); if (sentiment == null) { throw new Exception("Failed processing message - no sentiment result"); } // Printing sentiment results sb.AppendLine($"Document ID: {sentiment.Id} , Sentiment Score: {sentiment.Score}"); } return(sb.ToString()); }