using Azure.AI.TextAnalytics;
using System.Collections.Generic;
private static readonly AzureKeyCredential credentials = new AzureKeyCredential("c813f3176d41490cb4dc912d7724ad7e");
private static readonly Uri endpoint = new Uri("https://ecd-connect-ai.cognitiveservices.azure.com/");
private static TextAnalyticsClient AuthenticateClient()
return new TextAnalyticsClient(endpoint, credentials);
static void Main(string[] args)
var client = AuthenticateClient();
string question = "What food does your child like to eat?";
List<string> questionsList = new List<string>
"How is your child doing overall?",
"How would you describe your child’s mood and behavior lately?",
"Are there any recent changes in your child's behavior or routine?",
"How is your child’s general health?",
"Has your child had any recent illnesses or injuries?",
"Is your child up to date with their vaccinations?",
"How would you describe your child’s eating habits?",
"Are there any foods your child particularly likes or dislikes?",
"Do you have any concerns about your child’s nutrition?",
var similarQuestions = FindSimilarQuestions(client, question, questionsList);
Console.WriteLine("Input question: {0}", question);
foreach (var q in similarQuestions)
Console.WriteLine("{0} || {1} || {2}", q.Question, q.KeyPhrases.FirstOrDefault(), q.Similarity);
static List<Result> FindSimilarQuestions(TextAnalyticsClient client, string question, List<string> questionsList)
var documentBatch = new List<string> { question };
documentBatch.AddRange(questionsList);
var response = client.ExtractKeyPhrasesBatch(documentBatch);
var results = new List<Result>();
foreach (var resp in response.Value)
var q = documentBatch[qIndex];
AskQuestion = qIndex == 0,
KeyPhrases = resp.KeyPhrases,
var askQuestionResult = results[0];
results = results.Where(r => !r.AskQuestion).ToList();
foreach (var result in results)
result.Similarity = CalculateSimilarity(askQuestionResult.KeyPhrases, result.KeyPhrases);
return results.OrderByDescending(r => r.Similarity).ToList();
static double CalculateSimilarity(IReadOnlyCollection<string> keyPhrases1, IReadOnlyCollection<string> keyPhrases2)
if (!keyPhrases1.Any() || !keyPhrases2.Any())
var intersection = keyPhrases1.Intersect(keyPhrases2).Count();
var union = keyPhrases1.Union(keyPhrases2).Count();
return (double)intersection / union;
public KeyPhraseCollection KeyPhrases;
public double Similarity;