Skip to content

Commit

Permalink
Added Java, C#, C++ and Python updates (#519)
Browse files Browse the repository at this point in the history
* Added Java, C#, C++ and Python updates

* Commented out the code, and updated comment to 'uncomment' :)

* Made all main entry points in C# task-based, to remove .Wait()

* Converted if / else to switch

* Adjusted line length, to avoid scrollbar.

* Removed unnecessary .ConfigureAwait(false) invocations.
  • Loading branch information
IEvangelist authored Mar 19, 2020
1 parent 2f795ae commit 1aca8ee
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ void recognizeIntent()
// Once you've obtained it, replace with below with your own Language Understanding subscription key
// and service region (e.g., "westus").
// The default recognition language is "en-us".
auto config = SpeechConfig::FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");

auto config = SpeechConfig::FromSubscription(
"YourLanguageUnderstandingSubscriptionKey",
"YourLanguageUnderstandingServiceRegion");

// Creates an intent recognizer using microphone as audio input.
auto recognizer = IntentRecognizer::FromConfig(config);

Expand All @@ -33,6 +35,9 @@ void recognizeIntent()
recognizer->AddIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
recognizer->AddIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

// To add all of the possible intents from a LUIS model to the recognizer, uncomment the line below:
// recognizer->AddAllIntents(model);

cout << "Say something...\n";

// Starts intent recognition, and returns after a single utterance is recognized. The end of a
Expand Down
5 changes: 3 additions & 2 deletions quickstart/csharp/dotnet/from-blob/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class Program

static async Task Main()
{
// For non-Windows 10 users.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Cognitive Services follows security best practices.
// If you experience connectivity issues, see:
// https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls

await TranscribeAsync();
}
Expand Down
4 changes: 2 additions & 2 deletions quickstart/csharp/dotnet/from-file/helloworld/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static async Task RecognizeSpeechAsync()
}
}

static void Main()
static async Task Main()
{
RecognizeSpeechAsync().Wait();
await RecognizeSpeechAsync();
Console.WriteLine("Please press <Return> to continue.");
Console.ReadLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static async Task RecognizeSpeechAsync()
}
}

static void Main()
static async Task Main()
{
RecognizeSpeechAsync().Wait();
await RecognizeSpeechAsync();
Console.WriteLine("Please press <Return> to continue.");
Console.ReadLine();
}
Expand Down
61 changes: 33 additions & 28 deletions quickstart/csharp/dotnet/intent-recognition/helloworld/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public static async Task RecognizeIntentAsync()
// Once you've obtained it, replace with below with your own Language Understanding subscription key
// and service region (e.g., "westus").
// The default language is "en-us".
var config = SpeechConfig.FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");
var config = SpeechConfig.FromSubscription(
"YourLanguageUnderstandingSubscriptionKey",
"YourLanguageUnderstandingServiceRegion");

// Creates an intent recognizer using microphone as audio input.
using (var recognizer = new IntentRecognizer(config))
Expand All @@ -34,6 +36,9 @@ public static async Task RecognizeIntentAsync()
recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

// To add all of the possible intents from a LUIS model to the recognizer, uncomment the line below:
// recognizer.AddAllIntents(model);

// Starts recognizing.
Console.WriteLine("Say something...");

Expand All @@ -43,42 +48,42 @@ public static async Task RecognizeIntentAsync()
// Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
// shot recognition like command or query.
// For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);
var result = await recognizer.RecognizeOnceAsync();

// Checks result.
if (result.Reason == ResultReason.RecognizedIntent)
{
Console.WriteLine($"RECOGNIZED: Text={result.Text}");
Console.WriteLine($" Intent Id: {result.IntentId}.");
Console.WriteLine($" Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
}
else if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"RECOGNIZED: Text={result.Text}");
Console.WriteLine($" Intent not recognized.");
}
else if (result.Reason == ResultReason.NoMatch)
{
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
}
else if (result.Reason == ResultReason.Canceled)
switch (result.Reason)
{
var cancellation = CancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
case ResultReason.RecognizedIntent:
Console.WriteLine($"RECOGNIZED: Text={result.Text}");
Console.WriteLine($" Intent Id: {result.IntentId}.");
var json = result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
Console.WriteLine($" Language Understanding JSON: {json}.");
break;
case ResultReason.RecognizedSpeech:
Console.WriteLine($"RECOGNIZED: Text={result.Text}");
Console.WriteLine($" Intent not recognized.");
break;
case ResultReason.NoMatch:
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
break;
case ResultReason.Canceled:
var cancellation = CancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}

static void Main()
static async Task Main()
{
RecognizeIntentAsync().Wait();
await RecognizeIntentAsync();
Console.WriteLine("Please press <Return> to continue.");
Console.ReadLine();
}
Expand Down
4 changes: 2 additions & 2 deletions quickstart/csharp/dotnet/text-to-speech/helloworld/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public static async Task SynthesisToSpeakerAsync()
}
}

static void Main()
static async Task Main()
{
SynthesisToSpeakerAsync().Wait();
await SynthesisToSpeakerAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ public static async Task TranslationContinuousRecognitionAsync()

// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
Console.WriteLine("Say something...");
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
await recognizer.StartContinuousRecognitionAsync();

do
{
Console.WriteLine("Press Enter to stop");
} while (Console.ReadKey().Key != ConsoleKey.Enter);

// Stops continuous recognition.
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
await recognizer.StopContinuousRecognitionAsync();
}
}

static void Main(string[] args)
static async Task Main(string[] args)
{
TranslationContinuousRecognitionAsync().Wait();
await TranslationContinuousRecognitionAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public static async Task RecognizeSpeechAsync()
}
}

static void Main()
static async Task Main()
{
RecognizeSpeechAsync().Wait();
await RecognizeSpeechAsync();
Console.WriteLine("Please press <Return> to continue.");
Console.ReadLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static async Task SynthesisToSpeakerAsync()
}
}

static void Main()
static async Task Main()
{
SynthesisToSpeakerAsync().Wait();
await SynthesisToSpeakerAsync();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ public static async Task TranslationContinuousRecognitionAsync()

// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
Console.WriteLine("Say something...");
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
await recognizer.StartContinuousRecognitionAsync();

do
{
Console.WriteLine("Press Enter to stop");
} while (Console.ReadKey().Key != ConsoleKey.Enter);

// Stops continuous recognition.
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
await recognizer.StopContinuousRecognitionAsync();
}
}

static void Main(string[] args)
static async Task Main(string[] args)
{
TranslationContinuousRecognitionAsync().Wait();
await TranslationContinuousRecognitionAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public static void main(String[] args) {
// and service region. Replace with your own subscription (endpoint) key
// and service region (e.g., "westus2").
// The default language is "en-us".
SpeechConfig config = SpeechConfig.fromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");
SpeechConfig config = SpeechConfig.fromSubscription(
"YourLanguageUnderstandingSubscriptionKey",
"YourLanguageUnderstandingServiceRegion");

// Creates an intent recognizer using microphone as audio input.
IntentRecognizer recognizer = new IntentRecognizer(config);
Expand All @@ -35,6 +37,9 @@ public static void main(String[] args) {
recognizer.addIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
recognizer.addIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

// To add all of the possible intents from a LUIS model to the recognizer, uncomment the line below:
// recognizer.addAllIntents(model);

System.out.println("Say something...");

// Starts recognition. It returns when the first utterance has been recognized.
Expand Down
7 changes: 6 additions & 1 deletion quickstart/python/intent-recognition/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"""performs one-shot intent recognition from input from the default microphone"""

# Set up the config for the intent recognizer (remember that this uses the Language Understanding key, not the Speech Services key)!
intent_config = speechsdk.SpeechConfig(subscription="YourLanguageUnderstandingSubscriptionKey", region="YourLanguageUnderstandingServiceRegion")
intent_config = speechsdk.SpeechConfig(
subscription="YourLanguageUnderstandingSubscriptionKey",
region="YourLanguageUnderstandingServiceRegion")

# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config)
Expand All @@ -26,6 +28,9 @@
]
intent_recognizer.add_intents(intents)

# To add all of the possible intents from a LUIS model to the recognizer, uncomment the line below:
# intent_recognizer.add_all_intents(model);

# Starts intent recognition, and returns after a single utterance is recognized. The end of a
# single utterance is determined by listening for silence at the end or until a maximum of 15
# seconds of audio is processed. It returns the recognition text as result.
Expand Down

0 comments on commit 1aca8ee

Please sign in to comment.