Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: move up the inner methods for readability #2037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions samples/python/console/intent_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,6 @@ def recognize_intent_once_from_file():


def recognize_intent_once_async_from_mic():
"""performs one-shot asynchronous intent recognition from input from the default microphone"""
# Set up the config for the intent recognizer
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)

# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)

# Add callbacks to the recognition events

# Set up a flag to mark when asynchronous recognition is done
done = False

def recognized_callback(evt: speechsdk.intent.IntentRecognitionEventArgs):
"""
Callback that is called on successful recognition of a full utterance by both speech
Expand All @@ -161,6 +148,19 @@ def recognizing_callback(evt: speechsdk.intent.IntentRecognitionEventArgs):
result = evt.result
print("Intermediate transcription: \"{}\"".format(result.text))

"""performs one-shot asynchronous intent recognition from input from the default microphone"""
# Set up the config for the intent recognizer
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)

# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)

# Add callbacks to the recognition events

# Set up a flag to mark when asynchronous recognition is done
done = False

# Connect the callbacks
intent_recognizer.recognized.connect(recognized_callback)
intent_recognizer.canceled.connect(canceled_callback)
Expand Down Expand Up @@ -192,6 +192,12 @@ def recognizing_callback(evt: speechsdk.intent.IntentRecognitionEventArgs):


def recognize_intent_continuous():
def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

"""performs continuous intent recognition from input from an audio file"""
# <IntentContinuousRecognitionWithFile>
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
Expand All @@ -215,12 +221,6 @@ def recognize_intent_continuous():
# Connect callback functions to the signals the intent recognizer fires.
done = False

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

intent_recognizer.session_started.connect(lambda evt: print("SESSION_START: {}".format(evt)))
intent_recognizer.speech_end_detected.connect(lambda evt: print("SPEECH_END_DETECTED: {}".format(evt)))
# event for intermediate results
Expand Down
44 changes: 22 additions & 22 deletions samples/python/console/speech_language_detection_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,6 @@ def speech_language_detection_once_from_file():


def speech_language_detection_once_from_continuous():
"""performs continuous speech language detection with input from an audio file"""
# <SpeechContinuousLanguageDetectionWithFile>
# Creates an AutoDetectSourceLanguageConfig, which defines a number of possible spoken languages
auto_detect_source_language_config = \
speechsdk.languageconfig.AutoDetectSourceLanguageConfig(languages=["zh-CN", "en-US"])

# Creates a SpeechConfig from your speech key and region
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# Set continuous language detection (override the default of "AtStart")
speech_config.set_property(
property_id=speechsdk.PropertyId.SpeechServiceConnection_LanguageIdMode, value='Continuous')

audio_config = speechsdk.audio.AudioConfig(filename=multilingual_wav_file)

source_language_recognizer = speechsdk.SourceLanguageRecognizer(
speech_config=speech_config,
auto_detect_source_language_config=auto_detect_source_language_config,
audio_config=audio_config)

done = False

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
Expand Down Expand Up @@ -171,6 +149,28 @@ def audio_recognized(evt: speechsdk.SpeechRecognitionEventArgs):
global language_detected
language_detected = True

"""performs continuous speech language detection with input from an audio file"""
# <SpeechContinuousLanguageDetectionWithFile>
# Creates an AutoDetectSourceLanguageConfig, which defines a number of possible spoken languages
auto_detect_source_language_config = \
speechsdk.languageconfig.AutoDetectSourceLanguageConfig(languages=["zh-CN", "en-US"])

# Creates a SpeechConfig from your speech key and region
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# Set continuous language detection (override the default of "AtStart")
speech_config.set_property(
property_id=speechsdk.PropertyId.SpeechServiceConnection_LanguageIdMode, value='Continuous')

audio_config = speechsdk.audio.AudioConfig(filename=multilingual_wav_file)

source_language_recognizer = speechsdk.SourceLanguageRecognizer(
speech_config=speech_config,
auto_detect_source_language_config=auto_detect_source_language_config,
audio_config=audio_config)

done = False

# Connect callbacks to the events fired by the speech recognizer
source_language_recognizer.recognized.connect(audio_recognized)
source_language_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
Expand Down
119 changes: 60 additions & 59 deletions samples/python/console/speech_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,6 @@ def stop_cb(evt: speechsdk.SessionEventArgs):

# <SpeechRecognitionUsingKeywordModel>
def speech_recognize_keyword_from_microphone():
"""performs keyword-triggered speech recognition with input microphone"""
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# Creates an instance of a keyword recognition model. Update this to
# point to the location of your keyword recognition model.
model = speechsdk.KeywordRecognitionModel("YourKeywordRecognitionModelFile.table")

# The phrase your keyword recognition model triggers on.
keyword = "YourKeyword"

speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

done = False

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
Expand All @@ -443,6 +429,20 @@ def recognized_cb(evt: speechsdk.SpeechRecognitionEventArgs):
elif evt.result.reason == speechsdk.ResultReason.NoMatch:
print('NOMATCH: {}'.format(evt))

"""performs keyword-triggered speech recognition with input microphone"""
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# Creates an instance of a keyword recognition model. Update this to
# point to the location of your keyword recognition model.
model = speechsdk.KeywordRecognitionModel("YourKeywordRecognitionModelFile.table")

# The phrase your keyword recognition model triggers on.
keyword = "YourKeyword"

speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

done = False

# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognizing.connect(recognizing_cb)
speech_recognizer.recognized.connect(recognized_cb)
Expand Down Expand Up @@ -493,6 +493,12 @@ def close(self):
"""close callback function"""
self._file_h.close()

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

# specify the audio format
Expand All @@ -509,12 +515,6 @@ def close(self):

done = False

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
Expand Down Expand Up @@ -553,6 +553,11 @@ def push_stream_writer(stream):


def speech_recognition_with_push_stream():
def session_stopped_cb(evt):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('SESSION STOPPED: {}'.format(evt))
recognition_done.set()

"""gives an example how to use a push audio stream to recognize speech from a custom audio
source"""
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
Expand All @@ -566,10 +571,6 @@ def speech_recognition_with_push_stream():
recognition_done = threading.Event()

# Connect callbacks to the events fired by the speech recognizer
def session_stopped_cb(evt):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('SESSION STOPPED: {}'.format(evt))
recognition_done.set()

speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
Expand Down Expand Up @@ -653,20 +654,6 @@ def speech_recognize_with_auto_language_detection_UsingCustomizedModel():


def speech_recognize_keyword_locally_from_microphone():
"""runs keyword spotting locally, with direct access to the result audio"""

# Creates an instance of a keyword recognition model. Update this to
# point to the location of your keyword recognition model.
model = speechsdk.KeywordRecognitionModel("YourKeywordRecognitionModelFile.table")

# The phrase your keyword recognition model triggers on.
keyword = "YourKeyword"

# Create a local keyword recognizer with the default microphone device for input.
keyword_recognizer = speechsdk.KeywordRecognizer()

done = False

def recognized_cb(evt: speechsdk.SpeechRecognitionEventArgs):
# Only a keyword phrase is recognized. The result cannot be 'NoMatch'
# and there is no timeout. The recognizer runs until a keyword phrase
Expand All @@ -685,6 +672,20 @@ def canceled_cb(evt: speechsdk.SpeechRecognitionCanceledEventArgs):
nonlocal done
done = True

"""runs keyword spotting locally, with direct access to the result audio"""

# Creates an instance of a keyword recognition model. Update this to
# point to the location of your keyword recognition model.
model = speechsdk.KeywordRecognitionModel("YourKeywordRecognitionModelFile.table")

# The phrase your keyword recognition model triggers on.
keyword = "YourKeyword"

# Create a local keyword recognizer with the default microphone device for input.
keyword_recognizer = speechsdk.KeywordRecognizer()

done = False

# Connect callbacks to the events fired by the keyword recognizer.
keyword_recognizer.recognized.connect(recognized_cb)
keyword_recognizer.canceled.connect(canceled_cb)
Expand Down Expand Up @@ -788,6 +789,27 @@ def pronunciation_assessment_continuous_from_file():
import difflib
import json

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

def recognized(evt: speechsdk.SpeechRecognitionEventArgs):
print('pronunciation assessment for: {}'.format(evt.result.text))
pronunciation_result = speechsdk.PronunciationAssessmentResult(evt.result)
print(' Accuracy score: {}, pronunciation score: {}, completeness score : {}, fluency score: {}'.format(
pronunciation_result.accuracy_score, pronunciation_result.pronunciation_score,
pronunciation_result.completeness_score, pronunciation_result.fluency_score
))
nonlocal recognized_words, fluency_scores, durations
recognized_words += pronunciation_result.words
fluency_scores.append(pronunciation_result.fluency_score)
json_result = evt.result.properties.get(speechsdk.PropertyId.SpeechServiceResponse_JsonResult)
jo = json.loads(json_result)
nb = jo['NBest'][0]
durations.append(sum([int(w['Duration']) for w in nb['Words']]))

# Creates an instance of a speech config with specified subscription key and service region.
# Replace with your own subscription key and service region (e.g., "westus").
# Note: The sample is for en-US language.
Expand All @@ -814,27 +836,6 @@ def pronunciation_assessment_continuous_from_file():
fluency_scores = []
durations = []

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
nonlocal done
done = True

def recognized(evt: speechsdk.SpeechRecognitionEventArgs):
print('pronunciation assessment for: {}'.format(evt.result.text))
pronunciation_result = speechsdk.PronunciationAssessmentResult(evt.result)
print(' Accuracy score: {}, pronunciation score: {}, completeness score : {}, fluency score: {}'.format(
pronunciation_result.accuracy_score, pronunciation_result.pronunciation_score,
pronunciation_result.completeness_score, pronunciation_result.fluency_score
))
nonlocal recognized_words, fluency_scores, durations
recognized_words += pronunciation_result.words
fluency_scores.append(pronunciation_result.fluency_score)
json_result = evt.result.properties.get(speechsdk.PropertyId.SpeechServiceResponse_JsonResult)
jo = json.loads(json_result)
nb = jo['NBest'][0]
durations.append(sum([int(w['Duration']) for w in nb['Words']]))

# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognized.connect(recognized)
speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
Expand Down
12 changes: 6 additions & 6 deletions samples/python/console/transcription_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
# Differentiation of speakers do not require voice signatures. In case more enhanced speaker identification is required,
# please use https://signature.centralus.cts.speech.microsoft.com/UI/index.html REST API to create your own voice signatures
def conversation_transcription_differentiate_speakers():
def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous transcription upon receiving an event `evt`"""
print('CLOSING {}'.format(evt))
nonlocal done
done = True

"""differentiates speakers using conversation transcription service"""
# Creates speech configuration with subscription information
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
Expand All @@ -61,12 +67,6 @@ def conversation_transcription_differentiate_speakers():

done = False

def stop_cb(evt: speechsdk.SessionEventArgs):
"""callback that signals to stop continuous transcription upon receiving an event `evt`"""
print('CLOSING {}'.format(evt))
nonlocal done
done = True

# Subscribe to the events fired by the conversation transcriber
transcriber.transcribed.connect(lambda evt: print('TRANSCRIBED: {}'.format(evt)))
transcriber.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
Expand Down
Loading