Özel varlık deseni eşleştirme ile amaçları tanıma

Azure AI hizmetleri Konuşma SDK'sı, basit dil desen eşleştirmesi ile amaç tanıma sağlayan yerleşik bir özelliğe sahiptir. Amaç, kullanıcının yapmak istediği bir şeydir: pencereyi kapatma, onay kutusunu işaretleme, metin ekleme vb.

Bu kılavuzda Konuşma SDK'sını kullanarak cihazınızın mikrofonu aracılığıyla konuşulan konuşma konuşmalarından amaçları türeten bir konsol uygulaması geliştireceksiniz. Şunları yapmayı öğreneceksiniz:

  • Konuşma SDK'sı NuGet paketine başvuran bir Visual Studio projesi oluşturma
  • Konuşma yapılandırması oluşturma ve amaç tanıyıcısı alma
  • Konuşma SDK'sı API'sini kullanarak amaçlar ve desenler ekleme
  • Konuşma SDK'sı API'sini kullanarak özel varlıklar ekleme
  • Zaman uyumsuz, olay odaklı sürekli tanıma kullanma

Desen eşleştirme ne zaman kullanılır?

Şu durumlar için desen eşleştirmeyi kullanın:

  • Yalnızca kullanıcının söylediklerini tam olarak eşleştirmek istiyorsunuz. Bu desenler, konuşma dili anlama (CLU) ile daha agresif bir şekilde eşleşmektedir.
  • CLU modeline erişiminiz yok, ancak yine de amaçları istiyorsunuz.

Daha fazla bilgi için bkz. desen eşleştirmeye genel bakış.

Önkoşullar

Bu kılavuza başlamadan önce aşağıdaki öğelere sahip olduğunuzdan emin olun:

Bir proje oluştur

Visual Studio 2019'da yeni bir C# konsol uygulaması projesi oluşturun ve Konuşma SDK'sını yükleyin.

Bazı ortak kodlarla başlayın

Şimdi projemiz için iskelet olarak çalışan bazı kodlar açalım Program.cs ve ekleyelim.

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");
        }
    }
}

Konuşma yapılandırması oluşturma

Bir IntentRecognizer nesneyi başlatabilmeniz için önce Azure AI hizmetleri tahmin kaynağınız için anahtarı ve Azure bölgesini kullanan bir yapılandırma oluşturmanız gerekir.

  • değerini Azure AI hizmetleri tahmin anahtarınız ile değiştirin "YOUR_SUBSCRIPTION_KEY" .
  • değerini Azure AI hizmetleri kaynak bölgenizle değiştirin "YOUR_SUBSCRIPTION_REGION" .

Bu örnek, FromSubscription() derlemek SpeechConfigiçin yöntemini kullanır. Kullanılabilir yöntemlerin tam listesi için bkz . SpeechConfig Sınıfı.

IntentRecognizer başlatma

Şimdi bir IntentRecognizeroluşturun. Bu kodu Konuşma yapılandırmanızın hemen altına ekleyin.

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

Bazı amaçlar ekleme

Bazı desenleri ile PatternMatchingModel ilişkilendirmeniz ve uygulamasına IntentRecognizeruygulamanız gerekir. İlk olarak bir PatternMatchingModel oluşturup birkaç amaç ekleyeceğiz.

Not

öğesine PatternMatchingIntentbirden çok desen ekleyebiliriz.

Bu kodu bloğun using içine ekleyin:

// Creates a Pattern Matching model and adds specific intents from your model. The
// Id is used to identify this model from others in the collection.
var model = new PatternMatchingModel("YourPatternMatchingModelId");

// Creates a pattern that uses groups of optional words. "[Go | Take me]" will match either "Go", "Take me", or "".
var patternWithOptionalWords = "[Go | Take me] to [floor|level] {floorName}";

// Creates a pattern that uses an optional entity and group that could be used to tie commands together.
var patternWithOptionalEntity = "Go to parking [{parkingLevel}]";

// You can also have multiple entities of the same name in a single pattern by adding appending a unique identifier
// to distinguish between the instances. For example:
var patternWithTwoOfTheSameEntity = "Go to floor {floorName:1} [and then go to floor {floorName:2}]";
// NOTE: Both floorName:1 and floorName:2 are tied to the same list of entries. The identifier can be a string
//       and is separated from the entity name by a ':'

// Creates the pattern matching intents and adds them to the model
model.Intents.Add(new PatternMatchingIntent("ChangeFloors", patternWithOptionalWords, patternWithOptionalEntity, patternWithTwoOfTheSameEntity));
model.Intents.Add(new PatternMatchingIntent("DoorControl", "{action} the doors", "{action} doors", "{action} the door", "{action} door"));

Bazı özel varlıklar ekleme

Desen eşleştiriciden tam olarak yararlanmak için varlıklarınızı özelleştirebilirsiniz. "floorName"i uygun zeminlerin listesi yapacağız. Ayrıca "parkingLevel" öğesini tamsayı varlığı yapacağız.

Bu kodu amaçlarınızın altına ekleyin:

// Creates the "floorName" entity and set it to type list.
// Adds acceptable values. NOTE the default entity type is Any and so we do not need
// to declare the "action" entity.
model.Entities.Add(PatternMatchingEntity.CreateListEntity("floorName", EntityMatchMode.Strict, "ground floor", "lobby", "1st", "first", "one", "1", "2nd", "second", "two", "2"));

// Creates the "parkingLevel" entity as a pre-built integer
model.Entities.Add(PatternMatchingEntity.CreateIntegerEntity("parkingLevel"));

Modelimizi Tanıyıcı'ya uygulama

Şimdi modeli IntentRecognizeruygulaması gerekir. API'nin bir model koleksiyonu alması için aynı anda birden çok model kullanmak mümkündür.

Bu kodu varlıklarınızın altına ekleyin:

var modelCollection = new LanguageUnderstandingModelCollection();
modelCollection.Add(model);

recognizer.ApplyLanguageModels(modelCollection);

Amacı tanıma

nesnesinden IntentRecognizer yöntemini çağıracaksınız RecognizeOnceAsync() . Bu yöntem Konuşma tanıma hizmetinden tek bir tümcecikteki konuşmayı tanımasını ve tümcecik tanımlandıktan sonra konuşmayı tanımayı durdurmasını ister.

Dil modellerini uyguladıktan sonra bu kodu ekleyin:

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

var result = await recognizer.RecognizeOnceAsync();

Tanıma sonuçlarını (veya hatalarını) görüntüleme

Tanıma sonucu Konuşma hizmeti tarafından döndürülürse, sonucu yazdıracağız.

Bu kodu altına var result = await recognizer.RecognizeOnceAsync();ekleyin:

if (result.Reason == ResultReason.RecognizedIntent)
{
    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
    Console.WriteLine($"       Intent Id={result.IntentId}.");

    var entities = result.Entities;
    switch (result.IntentId)
    {
        case "ChangeFloors":
            if (entities.TryGetValue("floorName", out string floorName))
            {
                Console.WriteLine($"       FloorName={floorName}");
            }

            if (entities.TryGetValue("floorName:1", out floorName))
            {
                Console.WriteLine($"     FloorName:1={floorName}");
            }

            if (entities.TryGetValue("floorName:2", out floorName))
            {
                Console.WriteLine($"     FloorName:2={floorName}");
            }

            if (entities.TryGetValue("parkingLevel", out string parkingLevel))
            {
                Console.WriteLine($"    ParkingLevel={parkingLevel}");
            }

            break;

        case "DoorControl":
            if (entities.TryGetValue("action", out string action))
            {
                Console.WriteLine($"          Action={action}");
            }
            break;
    }
}
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)
{
    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?");
    }
}

Kodunuzu denetleyin

Bu noktada kodunuz şu şekilde görünmelidir:

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 recognizer = new IntentRecognizer(config))
            {
                // Creates a Pattern Matching model and adds specific intents from your model. The
                // Id is used to identify this model from others in the collection.
                var model = new PatternMatchingModel("YourPatternMatchingModelId");

                // Creates a pattern that uses groups of optional words. "[Go | Take me]" will match either "Go", "Take me", or "".
                var patternWithOptionalWords = "[Go | Take me] to [floor|level] {floorName}";

                // Creates a pattern that uses an optional entity and group that could be used to tie commands together.
                var patternWithOptionalEntity = "Go to parking [{parkingLevel}]";

                // You can also have multiple entities of the same name in a single pattern by adding appending a unique identifier
                // to distinguish between the instances. For example:
                var patternWithTwoOfTheSameEntity = "Go to floor {floorName:1} [and then go to floor {floorName:2}]";
                // NOTE: Both floorName:1 and floorName:2 are tied to the same list of entries. The identifier can be a string
                //       and is separated from the entity name by a ':'

                // Adds some intents to look for specific patterns.
                model.Intents.Add(new PatternMatchingIntent("ChangeFloors", patternWithOptionalWords, patternWithOptionalEntity, patternWithTwoOfTheSameEntity));
                model.Intents.Add(new PatternMatchingIntent("DoorControl", "{action} the doors", "{action} doors", "{action} the door", "{action} door"));

                // Creates the "floorName" entity and set it to type list.
                // Adds acceptable values. NOTE the default entity type is Any and so we do not need
                // to declare the "action" entity.
                model.Entities.Add(PatternMatchingEntity.CreateListEntity("floorName", EntityMatchMode.Strict, "ground floor", "lobby", "1st", "first", "one", "1", "2nd", "second", "two", "2"));

                // Creates the "parkingLevel" entity as a pre-built integer
                model.Entities.Add(PatternMatchingEntity.CreateIntegerEntity("parkingLevel"));

                var modelCollection = new LanguageUnderstandingModelCollection();
                modelCollection.Add(model);

                recognizer.ApplyLanguageModels(modelCollection);

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

                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"       Intent Id={result.IntentId}.");

                    var entities = result.Entities;
                    switch (result.IntentId)
                    {
                        case "ChangeFloors":
                            if (entities.TryGetValue("floorName", out string floorName))
                            {
                                Console.WriteLine($"       FloorName={floorName}");
                            }

                            if (entities.TryGetValue("floorName:1", out floorName))
                            {
                                Console.WriteLine($"     FloorName:1={floorName}");
                            }

                            if (entities.TryGetValue("floorName:2", out floorName))
                            {
                                Console.WriteLine($"     FloorName:2={floorName}");
                            }

                            if (entities.TryGetValue("parkingLevel", out string parkingLevel))
                            {
                                Console.WriteLine($"    ParkingLevel={parkingLevel}");
                            }

                            break;

                        case "DoorControl":
                            if (entities.TryGetValue("action", out string action))
                            {
                                Console.WriteLine($"          Action={action}");
                            }
                            break;
                    }
                }
                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)
                {
                    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?");
                    }
                }
            }
        }
    }
}

Uygulamanızı derleme ve çalıştırma

Artık uygulamanızı oluşturmaya ve Konuşma tanıma hizmetini kullanarak konuşma tanımamızı test etmeye hazırsınız.

  1. Kodu derleme - Visual Studio'nun menü çubuğunda Derleme Çözümü'ne> tıklayın.
  2. Uygulamanızı başlatın - Menü çubuğunda Hata AyıklamaYı>Başlat Hata Ayıklama'yı seçin veya F5 tuşuna basın.
  3. Tanımayı başlat - Bir şey söylemenizi ister. Varsayılan dil İngilizce'dir. Konuşmanız Konuşma hizmetine gönderilir, metin olarak yazılır ve konsolda işlenir.

Örneğin, "Beni 2. kata götür" diyorsanız, çıkış bu olmalıdır:

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

Başka bir örnek olarak "Beni 7. kata götür" diyorsanız, çıkış bu olmalıdır:

Say something...
RECOGNIZED: Text=Take me to floor 7.
    Intent not recognized.

FloorName için geçerli değerler listemizde 7 olmadığından hiçbir amaç tanınmadı.

Bir proje oluştur

Visual Studio 2019'da yeni bir C++ konsol uygulaması projesi oluşturun ve Konuşma SDK'sını yükleyin.

Bazı ortak kodlarla başlayın

Şimdi projemiz için iskelet olarak çalışan bazı kodlar açalım helloworld.cpp ve ekleyelim.

#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");
}

Konuşma yapılandırması oluşturma

Bir IntentRecognizer nesneyi başlatabilmeniz için önce Azure AI hizmetleri tahmin kaynağınız için anahtarı ve Azure bölgesini kullanan bir yapılandırma oluşturmanız gerekir.

  • değerini Azure AI hizmetleri tahmin anahtarınız ile değiştirin "YOUR_SUBSCRIPTION_KEY" .
  • değerini Azure AI hizmetleri kaynak bölgenizle değiştirin "YOUR_SUBSCRIPTION_REGION" .

Bu örnek, FromSubscription() derlemek SpeechConfigiçin yöntemini kullanır. Kullanılabilir yöntemlerin tam listesi için bkz . SpeechConfig Sınıfı.

IntentRecognizer başlatma

Şimdi bir IntentRecognizeroluşturun. Bu kodu Konuşma yapılandırmanızın hemen altına ekleyin.

    auto intentRecognizer = IntentRecognizer::FromConfig(config);

Bazı amaçlar ekleme

Bazı desenleri ile PatternMatchingModel ilişkilendirmeniz ve uygulamasına IntentRecognizeruygulamanız gerekir. İlk olarak bir PatternMatchingModel oluşturup birkaç amaç ekleyeceğiz. PatternMatchingIntent bir yapı olduğundan yalnızca satır içi söz dizimini kullanacağız.

Not

öğesine PatternMatchingIntentbirden çok desen ekleyebiliriz.

auto model = PatternMatchingModel::FromId("myNewModel");

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

Bazı özel varlıklar ekleme

Desen eşleştiriciden tam olarak yararlanmak için varlıklarınızı özelleştirebilirsiniz. "floorName"i uygun zeminlerin listesi yapacağız.

model->Entities.push_back({ "floorName" , Intent::EntityType::List, Intent::EntityMatchMode::Strict, {"one", "1", "two", "2", "lobby", "ground floor"} });

Modelimizi Tanıyıcı'ya uygulama

Şimdi modeli IntentRecognizeruygulaması gerekir. API'nin bir model koleksiyonu alması için aynı anda birden çok model kullanmak mümkündür.

std::vector<std::shared_ptr<LanguageUnderstandingModel>> collection;

collection.push_back(model);
intentRecognizer->ApplyLanguageModels(collection);

Amacı tanıma

nesnesinden IntentRecognizer yöntemini çağıracaksınız RecognizeOnceAsync() . Bu yöntem Konuşma tanıma hizmetinden tek bir tümcecikteki konuşmayı tanımasını ve tümcecik tanımlandıktan sonra konuşmayı tanımayı durdurmasını ister. Kolaylık olması için, geleceğin tamamlanmasını bekleyeceğiz.

Bu kodu amaçlarınızın altına ekleyin:

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

Tanıma sonuçlarını (veya hatalarını) görüntüleme

Tanıma sonucu Konuşma hizmeti tarafından döndürülürse, sonucu yazdıracağız.

Bu kodu altına auto result = intentRecognizer->RecognizeOnceAsync().get();ekleyin:

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;
}

Kodunuzu denetleyin

Bu noktada kodunuz şu şekilde görünmelidir:

#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);

    auto model = PatternMatchingModel::FromId("myNewModel");

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

    model->Entities.push_back({ "floorName" , Intent::EntityType::List, Intent::EntityMatchMode::Strict, {"one", "1", "two", "2", "lobby", "ground floor"} });

    std::vector<std::shared_ptr<LanguageUnderstandingModel>> collection;

    collection.push_back(model);
    intentRecognizer->ApplyLanguageModels(collection);

    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;
    }
}

Uygulamanızı derleme ve çalıştırma

Artık uygulamanızı oluşturmaya ve Konuşma tanıma hizmetini kullanarak konuşma tanımamızı test etmeye hazırsınız.

  1. Kodu derleme - Visual Studio'nun menü çubuğunda Derleme Çözümü'ne> tıklayın.
  2. Uygulamanızı başlatın - Menü çubuğunda Hata AyıklamaYı>Başlat Hata Ayıklama'yı seçin veya F5 tuşuna basın.
  3. Tanımayı başlat - Bir şey söylemenizi ister. Varsayılan dil İngilizce'dir. Konuşmanız Konuşma hizmetine gönderilir, metin olarak yazılır ve konsolda işlenir.

Örneğin, "Beni 2. kata götür" diyorsanız, çıkış bu olmalıdır:

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

"Beni 7. kata götür" diyorsanız başka bir örnek de çıktı olmalıdır:

Say something ...
RECOGNIZED: Text = Take me to floor 7.
NO INTENT RECOGNIZED!

7 listemizde olmadığından Amaç Kimliği boş.

Başvuru belgeleri | GitHub'da Ek Örnekler

Bu hızlı başlangıçta Java için Konuşma SDK'sını yükleyebilirsiniz.

Platform gereksinimleri

Hedef ortamınızı seçin:

Java için Konuşma SDK'sı Windows, Linux ve macOS ile uyumludur.

Windows'ta 64 bit hedef mimariyi kullanmanız gerekir. Windows 10 veya üzeri gereklidir.

Platformunuz için Microsoft Visual Studio için Visual C++ Yeniden Dağıtılabilir 2015, 2017, 2019 ve 2022'yi yükleyin. Bu paketi ilk kez yüklemek için yeniden başlatma gerekebilir.

Java için Konuşma SDK'sı ARM64'te Windows'u desteklemez.

Azul Zulu OpenJDK gibi bir Java Geliştirme Seti yükleyin. OpenJDK'nin Microsoft Derlemesi veya tercih ettiğiniz JDK de çalışmalıdır.

Java için Konuşma SDK'sını yükleme

Yönergelerden bazıları gibi 1.24.2belirli bir SDK sürümünü kullanır. En son sürümü denetlemek için GitHub depomuzda arama yapın.

Hedef ortamınızı seçin:

Bu kılavuzda Java Çalışma Zamanı'na Java için Konuşma SDK'sının nasıl yükleneceği gösterilmektedir.

Desteklenen işletim sistemleri

Java için Konuşma SDK'sı paketi şu işletim sistemleri için kullanılabilir:

  • Windows: Yalnızca 64 bit.
  • Mac: macOS X sürüm 10.14 veya üzeri.
  • Linux: Desteklenen Linux dağıtımlarına ve hedef mimarilerine bakın.

Apache Maven kullanarak Java için Konuşma SDK'sını yüklemek için şu adımları izleyin:

  1. Apache Maven'ı yükleyin.

  2. Yeni projeyi istediğiniz bir komut istemi açın ve yeni bir pom.xml dosyası oluşturun.

  3. Aşağıdaki XML içeriğini pom.xml kopyalayın:

    <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. Konuşma SDK'sını ve bağımlılıkları yüklemek için aşağıdaki Maven komutunu çalıştırın.

    mvn clean dependency:copy-dependencies
    

Bazı ortak kodlarla başlayın

  1. src dir'den açın Main.java .

  2. Dosyanın içeriğini aşağıdakilerle değiştirin:

import java.util.ArrayList;
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");
    }
}

Konuşma yapılandırması oluşturma

Bir IntentRecognizer nesneyi başlatabilmeniz için önce Azure AI hizmetleri tahmin kaynağınız için anahtarı ve Azure bölgesini kullanan bir yapılandırma oluşturmanız gerekir.

  • değerini Azure AI hizmetleri tahmin anahtarınız ile değiştirin "YOUR_SUBSCRIPTION_KEY" .
  • değerini Azure AI hizmetleri kaynak bölgenizle değiştirin "YOUR_SUBSCRIPTION_REGION" .

Bu örnek, fromSubscription() derlemek SpeechConfigiçin yöntemini kullanır. Kullanılabilir yöntemlerin tam listesi için bkz . SpeechConfig Sınıfı.

IntentRecognizer başlatma

Şimdi bir IntentRecognizeroluşturun. Bu kodu Konuşma yapılandırmanızın hemen altına ekleyin. Bunu, otomatik kapatılabilir arabirimden yararlanabilmemiz için bir denemede yaparız.

try (IntentRecognizer recognizer = new IntentRecognizer(config)) {

}

Bazı amaçlar ekleme

Bazı desenleri ile PatternMatchingModel ilişkilendirmeniz ve uygulamasına IntentRecognizeruygulamanız gerekir. İlk olarak bir PatternMatchingModel oluşturup birkaç amaç ekleyeceğiz.

Not

öğesine PatternMatchingIntentbirden çok desen ekleyebiliriz.

Bu kodu bloğun try içine ekleyin:

// Creates a Pattern Matching model and adds specific intents from your model. The
// Id is used to identify this model from others in the collection.
PatternMatchingModel model = new PatternMatchingModel("YourPatternMatchingModelId");

// Creates a pattern that uses groups of optional words. "[Go | Take me]" will match either "Go", "Take me", or "".
String patternWithOptionalWords = "[Go | Take me] to [floor|level] {floorName}";

// Creates a pattern that uses an optional entity and group that could be used to tie commands together.
String patternWithOptionalEntity = "Go to parking [{parkingLevel}]";

// You can also have multiple entities of the same name in a single pattern by adding appending a unique identifier
// to distinguish between the instances. For example:
String patternWithTwoOfTheSameEntity = "Go to floor {floorName:1} [and then go to floor {floorName:2}]";
// NOTE: Both floorName:1 and floorName:2 are tied to the same list of entries. The identifier can be a string
//       and is separated from the entity name by a ':'

// Creates the pattern matching intents and adds them to the model
model.getIntents().put(new PatternMatchingIntent("ChangeFloors", patternWithOptionalWords, patternWithOptionalEntity, patternWithTwoOfTheSameEntity));
model.getIntents().put(new PatternMatchingIntent("DoorControl", "{action} the doors", "{action} doors", "{action} the door", "{action} door"));

Bazı özel varlıklar ekleme

Desen eşleştiriciden tam olarak yararlanmak için varlıklarınızı özelleştirebilirsiniz. "floorName"i uygun zeminlerin listesi yapacağız. Ayrıca "parkingLevel" öğesini tamsayı varlığı yapacağız.

Bu kodu amaçlarınızın altına ekleyin:

// Creates the "floorName" entity and set it to type list.
// Adds acceptable values. NOTE the default entity type is Any and so we do not need
// to declare the "action" entity.
model.getEntities().put(PatternMatchingEntity.CreateListEntity("floorName", PatternMatchingEntity.EntityMatchMode.Strict, "ground floor", "lobby", "1st", "first", "one", "1", "2nd", "second", "two", "2"));

// Creates the "parkingLevel" entity as a pre-built integer
model.getEntities().put(PatternMatchingEntity.CreateIntegerEntity("parkingLevel"));

Modelimizi Tanıyıcı'ya uygulama

Şimdi modeli IntentRecognizeruygulaması gerekir. API'nin bir model koleksiyonu alması için aynı anda birden çok model kullanmak mümkündür.

Bu kodu varlıklarınızın altına ekleyin:

ArrayList<LanguageUnderstandingModel> modelCollection = new ArrayList<LanguageUnderstandingModel>();
modelCollection.add(model);

recognizer.applyLanguageModels(modelCollection);

Amacı tanıma

nesnesinden IntentRecognizer yöntemini çağıracaksınız RecognizeOnceAsync() . Bu yöntem Konuşma tanıma hizmetinden tek bir tümcecikteki konuşmayı tanımasını ve tümcecik tanımlandıktan sonra konuşmayı tanımayı durdurmasını ister.

Dil modellerini uyguladıktan sonra bu kodu ekleyin:

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

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

Tanıma sonuçlarını (veya hatalarını) görüntüleme

Tanıma sonucu Konuşma hizmeti tarafından döndürülürse, sonucu yazdıracağız.

Bu kodu altına IntentRecognitionResult result = recognizer.recognizeOnceAsync.get();ekleyin:

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();

    switch (result.getIntentId())
    {
        case "ChangeFloors":
            if (entities.get("floorName") != null) {
                System.out.println(String.format("%17s %s", "FloorName=", entities.get("floorName")));
            }
            if (entities.get("floorName:1") != null) {
                System.out.println(String.format("%17s %s", "FloorName:1=", entities.get("floorName:1")));
            }
            if (entities.get("floorName:2") != null) {
                System.out.println(String.format("%17s %s", "FloorName:2=", entities.get("floorName:2")));
            }
            if (entities.get("parkingLevel") != null) {
                System.out.println(String.format("%17s %s", "ParkingLevel=", entities.get("parkingLevel")));
            }
            break;
        case "DoorControl":
            if (entities.get("action") != null) {
                System.out.println(String.format("%17s %s", "Action=", entities.get("action")));
            }
            break;
    }
}
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?");
    }
}

Kodunuzu denetleyin

Bu noktada kodunuz şu şekilde görünmelidir:

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

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 recognizer = new IntentRecognizer(config)) {
            // Creates a Pattern Matching model and adds specific intents from your model. The
            // Id is used to identify this model from others in the collection.
            PatternMatchingModel model = new PatternMatchingModel("YourPatternMatchingModelId");

            // Creates a pattern that uses groups of optional words. "[Go | Take me]" will match either "Go", "Take me", or "".
            String patternWithOptionalWords = "[Go | Take me] to [floor|level] {floorName}";

            // Creates a pattern that uses an optional entity and group that could be used to tie commands together.
            String patternWithOptionalEntity = "Go to parking [{parkingLevel}]";

            // You can also have multiple entities of the same name in a single pattern by adding appending a unique identifier
            // to distinguish between the instances. For example:
            String patternWithTwoOfTheSameEntity = "Go to floor {floorName:1} [and then go to floor {floorName:2}]";
            // NOTE: Both floorName:1 and floorName:2 are tied to the same list of entries. The identifier can be a string
            // and is separated from the entity name by a ':'

            // Creates the pattern matching intents and adds them to the model
            model.getIntents().put(new PatternMatchingIntent("ChangeFloors", patternWithOptionalWords, patternWithOptionalEntity, patternWithTwoOfTheSameEntity));
            model.getIntents().put(new PatternMatchingIntent("DoorControl", "{action} the doors", "{action} doors", "{action} the door", "{action} door"));

            // Creates the "floorName" entity and set it to type list.
            // Adds acceptable values. NOTE the default entity type is Any and so we do not need
            // to declare the "action" entity.
            model.getEntities().put(PatternMatchingEntity.CreateListEntity("floorName", PatternMatchingEntity.EntityMatchMode.Strict, "ground floor", "lobby", "1st", "first", "one", "1", "2nd", "second", "two", "2"));

            // Creates the "parkingLevel" entity as a pre-built integer
            model.getEntities().put(PatternMatchingEntity.CreateIntegerEntity("parkingLevel"));

            ArrayList<LanguageUnderstandingModel> modelCollection = new ArrayList<LanguageUnderstandingModel>();
            modelCollection.add(model);

            recognizer.applyLanguageModels(modelCollection);

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

            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();

                switch (result.getIntentId())
                {
                    case "ChangeFloors":
                        if (entities.get("floorName") != null) {
                            System.out.println(String.format("%17s %s", "FloorName=", entities.get("floorName")));
                        }
                        if (entities.get("floorName:1") != null) {
                            System.out.println(String.format("%17s %s", "FloorName:1=", entities.get("floorName:1")));
                        }
                        if (entities.get("floorName:2") != null) {
                            System.out.println(String.format("%17s %s", "FloorName:2=", entities.get("floorName:2")));
                        }
                        if (entities.get("parkingLevel") != null) {
                            System.out.println(String.format("%17s %s", "ParkingLevel=", entities.get("parkingLevel")));
                        }
                        break;

                    case "DoorControl":
                        if (entities.get("action") != null) {
                            System.out.println(String.format("%17s %s", "Action=", entities.get("action")));
                        }
                        break;
                }
            }
            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?");
                }
            }
        }
    }
}

Uygulamanızı derleme ve çalıştırma

Artık konuşma hizmetini ve ekli desen eşleştiricisini kullanarak uygulamanızı oluşturmaya ve amaç tanımamızı test etmeye hazırsınız.

Eclipse'te çalıştır düğmesini seçin veya ctrl+F11 tuşlarına basın, ardından "Bir şey söyle..." çıkışını izleyin Istemi. Göründüğünde konuşmanızı söyleyin ve çıkışı izleyin.

Örneğin, "Beni 2. kata götür" diyorsanız, çıkış bu olmalıdır:

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

Başka bir örnek olarak "Beni 7. kata götür" diyorsanız, çıkış bu olmalıdır:

Say something...
RECOGNIZED: Text=Take me to floor 7.
    Intent not recognized.

FloorName için geçerli değerler listemizde 7 olmadığından hiçbir amaç tanınmadı.