<jupyter_start><jupyter_text>本プログラムについて概要：Qiita記事「英語の発音練習に活用：Azure Pronunciation assessmentの使用方法を解説」https://qiita.com/sugulu_Ogawa_ISID/items/1d96546b74a739b3d44fの実装プログラムです。 ---[1] まずは音声認識からはじめてみましょう===================<jupyter_code># [1] AzureのCognitive Servicesのspeechのインストール
!pip install azure-cognitiveservices-speech

# [2] 使用するパッケージなどのimport
import json
import azure.cognitiveservices.speech as speech_sdk

# [3] keyとregionの設定
# 今回はセキュリテイを無視していますが、本番環境では重要情報なのでご注意を

COG_SERVICE_KEY="your_cognitive_services_key"
COG_SERVICE_REGION="your_cognitive_services_location"

# [4] SpeechConfigの設定
# 今回はセキュリテイを無視していますが、本番環境では重要情報なのでご注意を
speech_config = speech_sdk.SpeechConfig(COG_SERVICE_KEY, COG_SERVICE_REGION)
print('speech serviceのregionはこちらに設定しました:', speech_config.region)

# [5] 使用するファイルの設定
audioFile = '/content/time.wav'

# [6] AudioConfigの設定
audio_config = speech_sdk.AudioConfig(filename=audioFile)

# [7] SpeechRecognizerの設定（SpeechConfigとAudioConfigを設定時に使用します）
speech_recognizer = speech_sdk.SpeechRecognizer(speech_config, audio_config)

# [8] SpeechRecognizerで音声処理（音声認識）
speech_result = speech_recognizer.recognize_once_async().get()
print("次の音声と認識しました：", speech_result.text)<jupyter_output>次の音声と認識しました： What time is it?<jupyter_text>---[2] 発音評価（Pronunciation assessment）===================<jupyter_code># [1] 発音している単語のスクリプトを用意します
script = 'What time is it now in Japan ?'<jupyter_output><empty_output><jupyter_text>[2] AudioConfigの設定・参考記事https://docs.microsoft.com/ja-jp/azure/cognitive-services/speech-service/how-to-pronunciation-assessment?pivots=programming-language-python・SDK解説：azure-cognitiveservices-speech Packagehttps://docs.microsoft.com/ja-jp/python/api/azure-cognitiveservices-speech/?view=azure-pythonhttps://docs.microsoft.com/ja-jp/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.pronunciationassessmentconfig?view=azure-python・プログラム参考https://github.com/Azure-Samples/cognitive-services-speech-sdk/blob/4f9ee79c2287a5a00dcd1a50112cd43694aa7286/samples/python/console/speech_sample.pyL707<jupyter_code># [2] AudioConfigの設定
pronunciation_config = speech_sdk.PronunciationAssessmentConfig(reference_text=script,
                                                                  grading_system=speech_sdk.PronunciationAssessmentGradingSystem.HundredMark,
                                                                  granularity=speech_sdk.PronunciationAssessmentGranularity.Word)

# [3] SpeechRecognizerの設定（SpeechConfigとAudioConfigを設定時に使用します）
speech_recognizer = speech_sdk.SpeechRecognizer(speech_config, audio_config)
# 先ほど[7]で設定したものと同じ内容です

# [4] 発音評価：Pronunciation Assessmentの実施
pronunciation_config.apply_to(speech_recognizer)
result = speech_recognizer.recognize_once()

# [5] 発音評価：Pronunciation Assessmentの結果をまとめたオブジェクトを作成
pronunciation_result = speech_sdk.PronunciationAssessmentResult(result)

# [6] 発音評価の結果を表示（全文での）
print('Accuracy score: {}, fluency score: {}, completeness score : {}, pronunciation score: {}'.format(
            pronunciation_result.accuracy_score, pronunciation_result.fluency_score,
            pronunciation_result.completeness_score, pronunciation_result.pronunciation_score
        ))
# [7] 発音評価の結果を表示（単語ごとに）
for word_result in pronunciation_result.words:
    print('単語：{}, Accuracy score：{}'.format(word_result.word, word_result.accuracy_score))
# [8] まとめてJsonで取得・表示
json_result = result.properties.get(speech_sdk.PropertyId.SpeechServiceResponse_JsonResult)
jo = json.loads(json_result)
print(json.dumps(jo, indent=2))<jupyter_output>{
  "Id": "b19370d415794e5bb53919f179ffa1df",
  "RecognitionStatus": "Success",
  "Offset": 5000000,
  "Duration": 23500000,
  "DisplayText": "What time is it now in Japan?",
  "SNR": 27.69381,
  "NBest": [
    {
      "Confidence": 0.9015003,
      "Lexical": "What time is it now in Japan",
      "ITN": "What time is it now in Japan",
      "MaskedITN": "what time is it now in japan",
      "Display": "What time is it now in Japan?",
      "PronunciationAssessment": {
        "AccuracyScore": 69.0,
        "FluencyScore": 53.0,
        "CompletenessScore": 86.0,
        "PronScore": 62.8
      },
      "Words": [
        {
          "Word": "What",
          "Offset": 5000000,
          "Duration": 3500000,
          "PronunciationAssessment": {
            "AccuracyScore": 100.0,
            "ErrorType": "None"
          }
        },
        {
          "Word": "time",
          "Offset": 8600000,
          "Duration": 2900000,
          "PronunciationAssessment": {
            "Accu[...]
{'fraction_non_alphanumeric': 0.11920529801324503, 'fraction_numerical': 0.035099337748344374, 'mean_word_length': 4.628571428571429, 'pattern_counts': {'":': 28, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '623983', 'license': 'MIT', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1770, 'n_words': 209, 'path': 'example_of_azure_pronunciation_assessment.ipynb', 'repo_name': 'YutaroOgawa/example_of_azure_pronunciation_assessment', 'size': '4530'}