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); }
/// <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 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 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 static async Task <TextAnalyticsResults> AnalyseSentiment(string input) { var client = new TextAnalyticsAPI { AzureRegion = AzureRegions.Westus, SubscriptionKey = ConfigurationManager.AppSettings["TextAnalyticsSubscriptionKey"] }; var language = client.DetectLanguage( new BatchInput( new List <Input>() { new Input("1", input) })); var result = new TextAnalyticsResults { LanguageName = language.Documents[0].DetectedLanguages[0].Name, LanguageIso6391Name = language.Documents[0].DetectedLanguages[0].Iso6391Name }; var keyPhrases = await client.KeyPhrasesAsync(new MultiLanguageBatchInput( new[] { new MultiLanguageInput(language: result.LanguageIso6391Name, id: "1", text: input) })); result.KeyPhrases = keyPhrases.Documents[0].KeyPhrases; var sentiment = await client.SentimentAsync(new MultiLanguageBatchInput( new[] { new MultiLanguageInput(language: result.LanguageIso6391Name, id: "1", text: input) })); result.Score = sentiment.Documents[0].Score; return(result); }
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); }
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); } }
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 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()); }
public async Task <TextAnalyticsDocumentSentimentModel> CallTextAnalytics(string utterance) { // Create a client. ITextAnalyticsAPI client = new TextAnalyticsAPI(); client.AzureRegion = AzureRegions.Westeurope; client.SubscriptionKey = _textAnalyticsSubscriptionKey; var keysList = ""; var language = client.DetectLanguage( new BatchInput( new List <Input>() { new Input("0", utterance) })); foreach (var document in language.Documents) { Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name); } var lang = language.Documents.FirstOrDefault()?.DetectedLanguages.FirstOrDefault()?.Iso6391Name; var keys = client.KeyPhrases( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput(lang, "0", utterance), })); var sentiment = client.Sentiment(new MultiLanguageBatchInput(new List <MultiLanguageInput>() { new MultiLanguageInput(lang, "0", utterance) })); //Si les sentiments sont nulls, on renvoie un objet vide if (sentiment.Documents == null) { return(new TextAnalyticsDocumentSentimentModel()); } { var document = new TextAnalyticsDocumentSentimentModel { Text = utterance, Score = sentiment.Documents.FirstOrDefault(x => x.Id == "0")?.Score, Id = sentiment.Documents.FirstOrDefault()?.Id, Language = lang }; if (keys.Documents != null) { foreach (var item in keys.Documents.SelectMany(x => x.KeyPhrases).ToList()) { document.KeyWords += item; } } if (language.Documents == null) { return(document); } { foreach (var item in language.Documents.SelectMany(x => x.DetectedLanguages).ToList()) { document.DetectedLanguage += item.Name; } } return(document); } }