간단한 언어 패턴 일치를 사용하여 의도를 인식하는 방법

Azure AI 서비스 음성 SDK에서는 간단한 언어 패턴 일치를 통해 의도 인식을 제공하는 기능이 기본 제공됩니다. 의도는 창 닫기, 확인란 표시, 텍스트 삽입 등을 사용자가 수행하려는 작업입니다.

이 가이드에서는 Speech SDK를 사용하여 디바이스의 마이크를 통해 사용자의 말에서 의도를 추론하는 C++ 콘솔 애플리케이션을 개발합니다. 다음 방법에 대해 설명합니다.

  • Speech SDK NuGet 패키지를 참조하는 Visual Studio 프로젝트 만들기
  • 음성 구성을 만들고 의도 인식기 가져오기
  • Speech SDK API를 통해 의도 및 패턴 추가
  • 마이크에서 음성 인식
  • 비동기, 이벤트 중심 연속 인식 사용

패턴 일치를 사용하는 경우

다음과 같은 경우 패턴 일치를 사용합니다.

  • 사용자가 말한 것과 정확히 일치시키는 데만 관심이 있습니다. 이러한 패턴은 CLU(대화 언어 이해)보다 더 적극적으로 일치합니다.
  • CLU 모델에 대한 액세스 권한이 없지만 여전히 의도가 필요합니다.

자세한 내용은 패턴 일치 개요를 참조하세요.

필수 조건

이 가이드를 시작하기 전에, 다음 항목을 갖추고 있는지 확인합니다.

Speech와 간단한 패턴

단순 패턴은 음성 SDK의 기능이며 Azure AI 서비스 리소스 또는 통합 음성 리소스가 필요합니다.

패턴은 그 안의 어딘가에 엔터티를 포함하는 구문입니다. 엔터티는 단어를 중괄호로 묶음으로써 정의됩니다. 이 예에서는 대/소문자를 구분하는 "floorName" ID를 사용하여 엔터티를 정의합니다.

    Take me to the {floorName}

다른 모든 특수 문자와 구두점은 무시됩니다.

의도는 IntentRecognizer->AddIntent() API에 대한 호출을 사용하여 추가됩니다.

프로젝트 만들기

Visual Studio 2019에서 새 C# 콘솔 애플리케이션 프로젝트를 만들고 Speech SDK를 설치합니다.

몇 가지 상용구 코드로 시작

Program.cs를 열고 프로젝트의 골격으로 작동하는 코드를 추가해 보겠습니다.

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Intent;

namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            IntentPatternMatchingWithMicrophoneAsync().Wait();
        }

        private static async Task IntentPatternMatchingWithMicrophoneAsync()
        {
            var config = SpeechConfig.FromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");
        }
    }
}

음성 구성 만들기

IntentRecognizer 개체를 초기화하려면 먼저 Azure AI 서비스 예측 리소스에 대한 키와 위치를 사용하는 구성을 만들어야 합니다.

  • "YOUR_SUBSCRIPTION_KEY"을 Azure AI 서비스 예측 키로 바꿉니다.
  • "YOUR_SUBSCRIPTION_REGION"을 Azure AI 서비스 리소스 지역으로 바꿉니다.

이 샘플에서는 FromSubscription() 메서드를 사용하여 SpeechConfig를 빌드합니다. 사용 가능한 메서드의 전체 목록은 SpeechConfig 클래스를 참조하세요.

IntentRecognizer 초기화

이제 IntentRecognizer를 만듭니다. 음성 구성 바로 아래에 이 코드를 삽입합니다.

using (var intentRecognizer = new IntentRecognizer(config))
{
    
}

의도 추가

AddIntent()를 호출하여 일부 패턴을 IntentRecognizer에 연결해야 합니다. 층을 변경하기 위해 동일한 ID를 가진 2개의 의도를 추가하고 문을 열고 닫는 별도의 ID를 가진 다른 의도를 추가합니다. 이 코드를 using 블록 내에 삽입합니다.

intentRecognizer.AddIntent("Take me to floor {floorName}.", "ChangeFloors");
intentRecognizer.AddIntent("Go to floor {floorName}.", "ChangeFloors");
intentRecognizer.AddIntent("{action} the door.", "OpenCloseDoor");

참고 항목

선언할 수 있는 엔터티의 수에는 제한이 없지만 느슨하게 일치합니다. "{action} door"와 같은 구문을 추가하면 "door"라는 단어 앞에 텍스트가 있을 때마다 일치합니다. 의도는 엔터티 수를 기준으로 평가됩니다. 두 패턴이 일치하면 더 많은 엔터티가 정의된 패턴이 반환됩니다.

의도 인식

IntentRecognizer 개체에서 RecognizeOnceAsync() 메서드를 호출합니다. 이 방법은 음성 서비스에 단일 구문의 음성을 인식하도록 요청하고 구문이 식별되면 음성 인식을 중지합니다. 번거로움을 피하기 위해 완료될 때까지 기다립니다.

이 코드를 사용자 의도 아래에 삽입합니다.

Console.WriteLine("Say something...");

var result = await intentRecognizer.RecognizeOnceAsync();

인식 결과(또는 오류) 표시

Speech Service에서 인식 결과가 반환되면 결과를 출력합니다.

이 코드를 var result = await recognizer.RecognizeOnceAsync(); 아래에 삽입합니다.

string floorName;
switch (result.Reason)
{
    case ResultReason.RecognizedSpeech:
        Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
        Console.WriteLine($"    Intent not recognized.");
        break;
    case ResultReason.RecognizedIntent:
        Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
        Console.WriteLine($"       Intent Id= {result.IntentId}.");
        var entities = result.Entities;
        if (entities.TryGetValue("floorName", out floorName))
        {
            Console.WriteLine($"       FloorName= {floorName}");
        }
    
        if (entities.TryGetValue("action", out floorName))
        {
            Console.WriteLine($"       Action= {floorName}");
        }
    
        break;
    case ResultReason.NoMatch:
    {
        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
        var noMatch = NoMatchDetails.FromResult(result);
        switch (noMatch.Reason)
        {
            case NoMatchReason.NotRecognized:
                Console.WriteLine($"NOMATCH: Speech was detected, but not recognized.");
                break;
            case NoMatchReason.InitialSilenceTimeout:
                Console.WriteLine($"NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech.");
                break;
            case NoMatchReason.InitialBabbleTimeout:
                Console.WriteLine($"NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech.");
                break;
            case NoMatchReason.KeywordNotRecognized:
                Console.WriteLine($"NOMATCH: Keyword not recognized");
                break;
        }
        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 set the speech resource key and region values?");
        }
        break;
    }
    default:
        break;
}

코드 확인

이 시점에서 코드는 다음과 같습니다.

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Intent;

namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            IntentPatternMatchingWithMicrophoneAsync().Wait();
        }

        private static async Task IntentPatternMatchingWithMicrophoneAsync()
        {
            var config = SpeechConfig.FromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");
            using (var intentRecognizer = new IntentRecognizer(config))
            {
                intentRecognizer.AddIntent("Take me to floor {floorName}.", "ChangeFloors");
                intentRecognizer.AddIntent("Go to floor {floorName}.", "ChangeFloors");
                intentRecognizer.AddIntent("{action} the door.", "OpenCloseDoor");

                Console.WriteLine("Say something...");

                var result = await intentRecognizer.RecognizeOnceAsync();

                string floorName;
                switch (result.Reason)
                {
                    case ResultReason.RecognizedSpeech:
                        Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
                        Console.WriteLine($"    Intent not recognized.");
                        break;
                    case ResultReason.RecognizedIntent:
                        Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
                        Console.WriteLine($"       Intent Id= {result.IntentId}.");
                        var entities = result.Entities;
                        if (entities.TryGetValue("floorName", out floorName))
                        {
                            Console.WriteLine($"       FloorName= {floorName}");
                        }

                        if (entities.TryGetValue("action", out floorName))
                        {
                            Console.WriteLine($"       Action= {floorName}");
                        }

                        break;
                    case ResultReason.NoMatch:
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                        var noMatch = NoMatchDetails.FromResult(result);
                        switch (noMatch.Reason)
                        {
                            case NoMatchReason.NotRecognized:
                                Console.WriteLine($"NOMATCH: Speech was detected, but not recognized.");
                                break;
                            case NoMatchReason.InitialSilenceTimeout:
                                Console.WriteLine($"NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech.");
                                break;
                            case NoMatchReason.InitialBabbleTimeout:
                                Console.WriteLine($"NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech.");
                                break;
                            case NoMatchReason.KeywordNotRecognized:
                                Console.WriteLine($"NOMATCH: Keyword not recognized");
                                break;
                        }
                        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 set the speech resource key and region values?");
                        }
                        break;
                    }
                    default:
                        break;
                }
            }
        }
    }
}

앱 빌드 및 실행

이제 앱을 빌드하고 음성 서비스를 사용하여 음성 인식을 테스트할 준비가 되었습니다.

  1. 코드 컴파일 - Visual Studio의 메뉴 모음에서 빌드>빌드 솔루션을 선택합니다.
  2. 앱 시작 - 메뉴 모음에서 디버그>디버깅 시작을 선택하거나 F5 키를 누릅니다.
  3. 인식 시작 - 무언가를 말하라는 메시지가 표시됩니다. 기본 언어는 한국어(Korean)입니다. 음성은 Speech Service로 전송되어 텍스트로 변환되고 콘솔에 렌더링됩니다.

예를 들어 "Take me to floor 7"이라고 말하면 다음과 같이 출력됩니다.

Say something ...
RECOGNIZED: Text= Take me to floor 7.
  Intent Id= ChangeFloors
  FloorName= 7

프로젝트 만들기

Visual Studio 2019에서 새 C++ 콘솔 애플리케이션 프로젝트를 만들고 Speech SDK를 설치합니다.

몇 가지 상용구 코드로 시작

helloworld.cpp를 열고 프로젝트의 골격으로 작동하는 코드를 추가해 보겠습니다.

    #include <iostream>
    #include <speechapi_cxx.h>

    using namespace Microsoft::CognitiveServices::Speech;
    using namespace Microsoft::CognitiveServices::Speech::Intent;

    int main()
    {
        std::cout << "Hello World!\n";

        auto config = SpeechConfig::FromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");
    }

음성 구성 만들기

IntentRecognizer 개체를 초기화하려면 먼저 Azure AI 서비스 예측 리소스에 대한 키와 위치를 사용하는 구성을 만들어야 합니다.

  • "YOUR_SUBSCRIPTION_KEY"을 Azure AI 서비스 예측 키로 바꿉니다.
  • "YOUR_SUBSCRIPTION_REGION"을 Azure AI 서비스 리소스 지역으로 바꿉니다.

이 샘플에서는 FromSubscription() 메서드를 사용하여 SpeechConfig를 빌드합니다. 사용 가능한 메서드의 전체 목록은 SpeechConfig 클래스를 참조하세요.

IntentRecognizer 초기화

이제 IntentRecognizer를 만듭니다. 음성 구성 바로 아래에 이 코드를 삽입합니다.

    auto intentRecognizer = IntentRecognizer::FromConfig(config);

의도 추가

AddIntent()를 호출하여 일부 패턴을 IntentRecognizer에 연결해야 합니다. 층을 변경하기 위해 동일한 ID를 가진 2개의 의도를 추가하고 문을 열고 닫는 별도의 ID를 가진 다른 의도를 추가합니다.

    intentRecognizer->AddIntent("Take me to floor {floorName}.", "ChangeFloors");
    intentRecognizer->AddIntent("Go to floor {floorName}.", "ChangeFloors");
    intentRecognizer->AddIntent("{action} the door.", "OpenCloseDoor");

참고 항목

선언할 수 있는 엔터티의 수에는 제한이 없지만 느슨하게 일치합니다. "{action} door"와 같은 구문을 추가하면 "door"라는 단어 앞에 텍스트가 있을 때마다 일치합니다. 의도는 엔터티 수를 기준으로 평가됩니다. 두 패턴이 일치하면 더 많은 엔터티가 정의된 패턴이 반환됩니다.

의도 인식

IntentRecognizer 개체에서 RecognizeOnceAsync() 메서드를 호출합니다. 이 방법은 음성 서비스에 단일 구문의 음성을 인식하도록 요청하고 구문이 식별되면 음성 인식을 중지합니다. 번거로움을 피하기 위해 완료될 때까지 기다립니다.

이 코드를 사용자 의도 아래에 삽입합니다.

    std::cout << "Say something ..." << std::endl;
    auto result = intentRecognizer->RecognizeOnceAsync().get();

인식 결과(또는 오류) 표시

Speech Service에서 인식 결과가 반환되면 결과를 출력합니다.

이 코드를 auto result = intentRecognizer->RecognizeOnceAsync().get(); 아래에 삽입합니다.

switch (result->Reason)
{
case ResultReason::RecognizedSpeech:
        std::cout << "RECOGNIZED: Text = " << result->Text.c_str() << std::endl;
        std::cout << "NO INTENT RECOGNIZED!" << std::endl;
        break;
case ResultReason::RecognizedIntent:
    std::cout << "RECOGNIZED: Text = " << result->Text.c_str() << std::endl;
    std::cout << "  Intent Id = " << result->IntentId.c_str() << std::endl;
    auto entities = result->GetEntities();
    if (entities.find("floorName") != entities.end())
    {
        std::cout << "  Floor name: = " << entities["floorName"].c_str() << std::endl;
    }

    if (entities.find("action") != entities.end())
    {
        std::cout << "  Action: = " << entities["action"].c_str() << std::endl;
    }

    break;
case ResultReason::NoMatch:
{
    auto noMatch = NoMatchDetails::FromResult(result);
    switch (noMatch->Reason)
    {
    case NoMatchReason::NotRecognized:
        std::cout << "NOMATCH: Speech was detected, but not recognized." << std::endl;
        break;
    case NoMatchReason::InitialSilenceTimeout:
        std::cout << "NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." << std::endl;
        break;
    case NoMatchReason::InitialBabbleTimeout:
        std::cout << "NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." << std::endl;
        break;
    case NoMatchReason::KeywordNotRecognized:
        std::cout << "NOMATCH: Keyword not recognized" << std::endl;
        break;
    }
    break;
}
case ResultReason::Canceled:
{
    auto cancellation = CancellationDetails::FromResult(result);

    if (!cancellation->ErrorDetails.empty())
    {
        std::cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails.c_str() << std::endl;
        std::cout << "CANCELED: Did you set the speech resource key and region values?" << std::endl;
    }
}
default:
    break;
}

코드 확인

이 시점에서 코드는 다음과 같습니다.

#include <iostream>
#include <speechapi_cxx.h>

using namespace Microsoft::CognitiveServices::Speech;
using namespace Microsoft::CognitiveServices::Speech::Intent;

int main()
{
    auto config = SpeechConfig::FromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");
    auto intentRecognizer = IntentRecognizer::FromConfig(config);

    intentRecognizer->AddIntent("Take me to floor {floorName}.", "ChangeFloors");
    intentRecognizer->AddIntent("Go to floor {floorName}.", "ChangeFloors");
    intentRecognizer->AddIntent("{action} the door.", "OpenCloseDoor");

    std::cout << "Say something ..." << std::endl;

    auto result = intentRecognizer->RecognizeOnceAsync().get();

    switch (result->Reason)
    {
    case ResultReason::RecognizedSpeech:
        std::cout << "RECOGNIZED: Text = " << result->Text.c_str() << std::endl;
        std::cout << "NO INTENT RECOGNIZED!" << std::endl;
        break;
    case ResultReason::RecognizedIntent:
        std::cout << "RECOGNIZED: Text = " << result->Text.c_str() << std::endl;
        std::cout << "  Intent Id = " << result->IntentId.c_str() << std::endl;
        auto entities = result->GetEntities();
        if (entities.find("floorName") != entities.end())
        {
            std::cout << "  Floor name: = " << entities["floorName"].c_str() << std::endl;
        }

        if (entities.find("action") != entities.end())
        {
            std::cout << "  Action: = " << entities["action"].c_str() << std::endl;
        }

        break;
    case ResultReason::NoMatch:
    {
        auto noMatch = NoMatchDetails::FromResult(result);
        switch (noMatch->Reason)
        {
        case NoMatchReason::NotRecognized:
            std::cout << "NOMATCH: Speech was detected, but not recognized." << std::endl;
            break;
        case NoMatchReason::InitialSilenceTimeout:
            std::cout << "NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." << std::endl;
            break;
        case NoMatchReason::InitialBabbleTimeout:
            std::cout << "NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." << std::endl;
            break;
        case NoMatchReason::KeywordNotRecognized:
            std::cout << "NOMATCH: Keyword not recognized." << std::endl;
            break;
        }
        break;
    }
    case ResultReason::Canceled:
    {
        auto cancellation = CancellationDetails::FromResult(result);

        if (!cancellation->ErrorDetails.empty())
        {
            std::cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails.c_str() << std::endl;
            std::cout << "CANCELED: Did you set the speech resource key and region values?" << std::endl;
        }
    }
    default:
        break;
    }
}

앱 빌드 및 실행

이제 앱을 빌드하고 음성 서비스를 사용하여 음성 인식을 테스트할 준비가 되었습니다.

  1. 코드 컴파일 - Visual Studio의 메뉴 모음에서 빌드>빌드 솔루션을 선택합니다.
  2. 앱 시작 - 메뉴 모음에서 디버그>디버깅 시작을 선택하거나 F5 키를 누릅니다.
  3. 인식 시작 - 무언가를 말하라는 메시지가 표시됩니다. 기본 언어는 한국어(Korean)입니다. 음성은 Speech Service로 전송되어 텍스트로 변환되고 콘솔에 렌더링됩니다.

예를 들어 "Take me to floor 7"이라고 말하면 다음과 같이 출력됩니다.

Say something ...
RECOGNIZED: Text = Take me to floor 7.
  Intent Id = ChangeFloors
  Floor name: = 7

참조 설명서 | GitHub의 추가 샘플

이 빠른 시작에서는 Java용 Speech SDK를 설치합니다.

플랫폼 요구 사항

대상 환경 선택:

Java용 Speech SDK는 Windows, Linux 및 macOS와 호환됩니다.

Windows에서는 64비트 대상 아키텍처를 사용해야 합니다. Windows 10 이상이 필요합니다.

플랫폼에 적합한 Visual Studio 2015, 2017, 2019, 2022용 Microsoft Visual C++ 재배포 가능 패키지를 설치합니다. 이 패키지를 처음 설치하려면 다시 시작해야 할 수 있습니다.

Java용 Speech SDK는 ARM64의 Windows를 지원하지 않습니다.

Azul Zulu OpenJDK와 같은 Java 개발 키트를 설치해야 합니다. OpenJDK의 Microsoft 빌드 또는 선호하는 JDK도 작동해야 합니다.

Java용 Speech SDK 설치

일부 지침은 1.24.2과 같은 특정 SDK 버전을 사용합니다. 최신 버전을 확인하려면 GitHub 리포지토리를 검색합니다.

대상 환경 선택:

이 가이드는 Java 런타임에서 Java용 Speech SDK를 설치하는 방법을 보여 줍니다.

지원되는 운영 체제

Java용 Speech SDK 패키지는 다음 운영 체제에서 사용할 수 있습니다.

Apache Maven을 사용하여 Java용 Speech SDK를 설치하려면 다음 단계를 따릅니다.

  1. Apache Maven을 설치합니다.

  2. 새 프로젝트를 원하는 명령 프롬프트를 열고 새 pom.xml 파일을 만듭니다.

  3. 다음 XML 콘텐츠를 pom.xml에 복사합니다.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.microsoft.cognitiveservices.speech.samples</groupId>
        <artifactId>quickstart-eclipse</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                <source>1.8</source>
                <target>1.8</target>
                </configuration>
            </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
            <groupId>com.microsoft.cognitiveservices.speech</groupId>
            <artifactId>client-sdk</artifactId>
            <version>1.37.0</version>
            </dependency>
        </dependencies>
    </project>
    
  4. 다음 Maven 명령을 실행하여 Speech SDK 및 종속성을 설치합니다.

    mvn clean dependency:copy-dependencies
    

몇 가지 상용구 코드로 시작

  1. src dir에서 Main.java를 엽니다.

  2. 파일의 내용을 다음으로 바꿉니다.

package quickstart;
import java.util.Dictionary;
import java.util.concurrent.ExecutionException;

import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.intent.*;

public class Program {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        IntentPatternMatchingWithMicrophone();
    }

    public static void IntentPatternMatchingWithMicrophone() throws InterruptedException, ExecutionException {
        SpeechConfig config = SpeechConfig.fromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");
    }
}

음성 구성 만들기

IntentRecognizer 개체를 초기화하려면 먼저 Azure AI 서비스 예측 리소스에 대한 키와 위치를 사용하는 구성을 만들어야 합니다.

  • "YOUR_SUBSCRIPTION_KEY"을 Azure AI 서비스 예측 키로 바꿉니다.
  • "YOUR_SUBSCRIPTION_REGION"을 Azure AI 서비스 리소스 지역으로 바꿉니다.

이 샘플에서는 FromSubscription() 메서드를 사용하여 SpeechConfig를 빌드합니다. 사용 가능한 메서드의 전체 목록은 SpeechConfig 클래스를 참조하세요.

IntentRecognizer 초기화

이제 IntentRecognizer를 만듭니다. 음성 구성 바로 아래에 이 코드를 삽입합니다.

try (IntentRecognizer intentRecognizer = new IntentRecognizer(config)) {
    
}

의도 추가

addIntent()를 호출하여 일부 패턴을 IntentRecognizer에 연결해야 합니다. 층을 변경하기 위해 동일한 ID를 가진 2개의 의도를 추가하고 문을 열고 닫는 별도의 ID를 가진 다른 의도를 추가합니다. 이 코드를 try 블록 내에 삽입합니다.

intentRecognizer.addIntent("Take me to floor {floorName}.", "ChangeFloors");
intentRecognizer.addIntent("Go to floor {floorName}.", "ChangeFloors");
intentRecognizer.addIntent("{action} the door.", "OpenCloseDoor");

참고 항목

선언할 수 있는 엔터티의 수에는 제한이 없지만 느슨하게 일치합니다. "{action} door"와 같은 구문을 추가하면 "door"라는 단어 앞에 텍스트가 있을 때마다 일치합니다. 의도는 엔터티 수를 기준으로 평가됩니다. 두 패턴이 일치하면 더 많은 엔터티가 정의된 패턴이 반환됩니다.

의도 인식

IntentRecognizer 개체에서 recognizeOnceAsync() 메서드를 호출합니다. 이 방법은 음성 서비스에 단일 구문의 음성을 인식하도록 요청하고 구문이 식별되면 음성 인식을 중지합니다. 번거로움을 피하기 위해 완료될 때까지 기다립니다.

이 코드를 사용자 의도 아래에 삽입합니다.

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

IntentRecognitionResult result = intentRecognizer.recognizeOnceAsync().get();

인식 결과(또는 오류) 표시

Speech Service에서 인식 결과가 반환되면 결과를 출력합니다.

이 코드를 IntentRecognitionResult result = recognizer.recognizeOnceAsync().get(); 아래에 삽입합니다.

if (result.getReason() == ResultReason.RecognizedSpeech) {
    System.out.println("RECOGNIZED: Text= " + result.getText());
    System.out.println(String.format("%17s", "Intent not recognized."));
}
else if (result.getReason() == ResultReason.RecognizedIntent) {
    System.out.println("RECOGNIZED: Text= " + result.getText());
    System.out.println(String.format("%17s %s", "Intent Id=", result.getIntentId() + "."));
    Dictionary<String, String> entities = result.getEntities();

    if (entities.get("floorName") != null) {
        System.out.println(String.format("%17s %s", "FloorName=", entities.get("floorName")));
    }
    if (entities.get("action") != null) {
        System.out.println(String.format("%17s %s", "Action=", entities.get("action")));
    }
}
else if (result.getReason() == ResultReason.NoMatch) {
    System.out.println("NOMATCH: Speech could not be recognized.");
}
else if (result.getReason() == ResultReason.Canceled) {
    CancellationDetails cancellation = CancellationDetails.fromResult(result);
    System.out.println("CANCELED: Reason=" + cancellation.getReason());

    if (cancellation.getReason() == CancellationReason.Error)
    {
        System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
        System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
        System.out.println("CANCELED: Did you update the subscription info?");
    }
}

코드 확인

이 시점에서 코드는 다음과 같습니다.

package quickstart;
import java.util.Dictionary;
import java.util.concurrent.ExecutionException;

import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.intent.*;

public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        IntentPatternMatchingWithMicrophone();
    }

    public static void IntentPatternMatchingWithMicrophone() throws InterruptedException, ExecutionException {
        SpeechConfig config = SpeechConfig.fromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SUBSCRIPTION_REGION");

        try (IntentRecognizer intentRecognizer = new IntentRecognizer(config)) {
            intentRecognizer.addIntent("Take me to floor {floorName}.", "ChangeFloors");
            intentRecognizer.addIntent("Go to floor {floorName}.", "ChangeFloors");
            intentRecognizer.addIntent("{action} the door.", "OpenCloseDoor");

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

            IntentRecognitionResult result = intentRecognizer.recognizeOnceAsync().get();
            if (result.getReason() == ResultReason.RecognizedSpeech) {
            System.out.println("RECOGNIZED: Text= " + result.getText());
            System.out.println(String.format("%17s", "Intent not recognized."));
            }
            else if (result.getReason() == ResultReason.RecognizedIntent) {
                System.out.println("RECOGNIZED: Text= " + result.getText());
                System.out.println(String.format("%17s %s", "Intent Id=", result.getIntentId() + "."));
                Dictionary<String, String> entities = result.getEntities();

                if (entities.get("floorName") != null) {
                    System.out.println(String.format("%17s %s", "FloorName=", entities.get("floorName")));
                }
                if (entities.get("action") != null) {
                    System.out.println(String.format("%17s %s", "Action=", entities.get("action")));
                }
            }
            else if (result.getReason() == ResultReason.NoMatch) {
                System.out.println("NOMATCH: Speech could not be recognized.");
            }
            else if (result.getReason() == ResultReason.Canceled) {
                CancellationDetails cancellation = CancellationDetails.fromResult(result);
                System.out.println("CANCELED: Reason=" + cancellation.getReason());

                if (cancellation.getReason() == CancellationReason.Error)
                {
                    System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
                    System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
                    System.out.println("CANCELED: Did you update the subscription info?");
                }
            }
        }
    }
}

앱 빌드 및 실행

이제 앱을 빌드하고 Speech Services와 포함된 패턴 검사기를 사용하여 의도 인식을 테스트할 준비가 되었습니다.

Eclipse에서 실행 버튼을 선택하거나 ctrl+F11을 누른 다음 "Say Something..." 프롬프트의 출력을 살펴보세요. 해당 출력이 나타나면 발화를 말하고 출력을 시청하세요.

예를 들어 "Take me to floor 7"이라고 말하면 다음과 같이 출력됩니다.

Say something ...
RECOGNIZED: Text= Take me to floor 7.
  Intent Id= ChangeFloors
  FloorName= 7

다음 단계

사용자 지정 엔터티를 사용하여 패턴 일치를 개선합니다.