كيفية التعرف على الأهداف مـع مطابقة نمط الكيان المخصص

تحتوي خدمات Azure الذكاء الاصطناعي Speech SDK على ميزة مضمنة لتوفير التعرف على الهدف مع مطابقة نمط اللغة البسيط. الهدف هو شيء يريد المستخدم القيام به: إغلاق نافذة، ووضع علامة على خانة اختيار، وإدراج بعض النص، وما إلى ذلك.

في هذا الدليل، يُمكنك استخدام Speech SDK لتطوير تطبيق وحدة تحكم يستمد الأهداف من تعبيرات الكلام المنطوقة من خلال ميكروفون جهازك. ‏‫ستتعلم كيفية:

  • أنشئ مشروع Visual Studio بالرجوع إلى حزمة Speech SDK NuGet
  • إنشاء تكوين الكلام والحصول علـى أداة التعرف على الهدف
  • إضافة الأهداف والأنماط عبر Speech SDK API
  • إضافة كيانات مُخصصة عبر Speech SDK API
  • استخدم التعرف المستمر غير المتزامن المستند إلى الحدث

متى تستخدم مُطابقة النمط

استخدم مطابقة النمط إذا:

  • أنت مهتم فقط بمطابقة ما قاله المستخدم بدقة. تتطابق هذه الأنماط بقوة أكبر من فهم لغة المحادثة (CLU).
  • ليس لديك حق الوصول إلى نموذج CLU، ولكنك لا تزال تريد الأهداف.

لمزيد مـن المعلومات، راجع نظرة عامة على مطابقة النمط.

المتطلبات الأساسية

تأكد من أن لديك العناصر التالية قبل بدء هذا الدليل:

إنشاء مشروع

إنشاء مَشروع تطبيق وحدة تحكم C# جديد في Visual Studio 2019 وتثبيت 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 لمورد التنبؤ بخدمات Azure الذكاء الاصطناعي.

  • استبدل "YOUR_SUBSCRIPTION_KEY" بمفتاح توقع خدمات Azure الذكاء الاصطناعي.
  • استبدل "YOUR_SUBSCRIPTION_REGION" بمنطقة مورد خدمات Azure الذكاء الاصطناعي.

يستخدم هذا النموذج أسلوبFromSubscription()لإنشاء SpeechConfig. للحصول على قائمة كاملة بالطرق المتاحة، راجعفئة SpeechConfig.

تهيئة المتعرف على الأهداف

الآن أنشئ IntentRecognizer. إدراج هذا الكود مباشرةً أسفل تكوين الكلام.

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

إضافة بعض الأهداف

تحتاج إلى إقران بعض الأنماط بـ PatternMatchingModel وتطبيقها على IntentRecognizer. سوف نبدأ بإنشاء PatternMatchingModel وإضافة بعض الأهداف إليه.

إشعار

يمكننا إضافة أنماط متعددة إلى PatternMatchingIntent.

إدخال هذا الرمز داخل الكتلة using:

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

إضافة بعض الكيانات المُخصصة

للاستفادة الكاملة من مطابق النمط، يُمكنك تخصيص الكيانات الخاصة بك. سوف نجعل "floorName" قائمة بالطوابق المُتوفرة. سوف نجعل أيضًا "parkingLevel" كيانًا صحيحًا.

إدراج هـذه التعليمة البرمجية أسفل أهدافك:

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

تطبيق نموذجنا علـى Recognizer

الآن مـن الضروري تطبيق النموذج على IntentRecognizer. مـن الممكن استخدام نماذج متعددة في وقت واحد حتى تأخذ واجهة برمجة التطبيقات مجموعة من النماذج.

أدخل هذه التعليمة أسفل الكيانات الخاصة بك:

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

recognizer.ApplyLanguageModels(modelCollection);

التعرف على الهدف

من الكائنIntentRecognizer، ستطلب طريقةRecognizeOnceAsync(). تطلب هذه الطريقة من خدمة الكلام التعرف على الكلام في عبارة واحدة، والتوقف عن التعرف على الكلام بمجرد تحديد العبارة.

قم بإدراج هذه التعليمات البرمجية بعد تطبيق نماذج اللغة:

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

var result = await recognizer.RecognizeOnceAsync();

عرض نتائج التعرف (أو الأخطاء)

عند إرجاع نتيجة التعرف بواسطة خدمة الكلام، سوف نقوم بطباعة النتيجة.

أدخِل هذا الرمز أسفل 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?");
    }
}

تحقق من الكود الخاص بك.

في هذه النقطة، يجب أن تظهر التعليمات البرمجية كذلك:

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

إنشاء تطبيقك وتشغيله

الآن أنت مستعد لإنشاء التطبيق واختبار التعرف على الكلام باستخدام خدمة التعرف على الكلام.

  1. ترجمة التعليمات البرمجية - من شريط قوائم Visual Studio، اختر إنشاء>إنشاء حل.
  2. ابدأ تشغيل تطبيقك - من شريط القوائم، اخترتصحيح الأخطاء>بدء التصحيح أو اضغط F5.
  3. بدء التعرف - سوف يطالبك بقول شيء ما. اللغة الافتراضية هـي اللغة الإنجليزية. يُرسل الكلام إلى خدمة التعرف على الكلام ونسخه كنص وتقديمه في وحدة التحكم.

على سبيل المثال، إذا قلت "خذني إلى الطابق 2"، يجب أن يكون هذا الناتج:

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

كمثال آخر إذا قلت "خذني إلى الطابق 7"، يجب أن يكون هـذا الناتج:

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

لم يتم التعرف على أي هدف لأن 7 لم يكن في قائمة القيم الصالحة لـ floorName.

إنشاء مشروع

إنشاء مشروع تطبيق وحدة تحكم C++‎ جديد فـي Visual Studio 2019 وتثبيت 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 لمورد التنبؤ بخدمات Azure الذكاء الاصطناعي.

  • استبدل "YOUR_SUBSCRIPTION_KEY" بمفتاح توقع خدمات Azure الذكاء الاصطناعي.
  • استبدل "YOUR_SUBSCRIPTION_REGION" بمنطقة مورد خدمات Azure الذكاء الاصطناعي.

يستخدم هذا النموذج أسلوبFromSubscription()لإنشاء SpeechConfig. للحصول على قائمة كاملة بالطرق المتاحة، راجعفئة SpeechConfig.

تهيئة المتعرف على الأهداف

الآن أنشئ IntentRecognizer. إدراج هذا الكود مباشرةً أسفل تكوين الكلام.

    auto intentRecognizer = IntentRecognizer::FromConfig(config);

إضافة بعض الأهداف

تحتاج إلى إقران بعض الأنماط بـ PatternMatchingModel وتطبيقها على IntentRecognizer. سوف نبدأ بإنشاء PatternMatchingModel وإضافة بعض الأهداف إليه. إن PatternMatchingIntent عبارة عن بنية لذلك سنستخدم فقط بناء الجملة المضمّن.

إشعار

يمكننا إضافة أنماط متعددة إلى PatternMatchingIntent.

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

إضافة بعض الكيانات المُخصصة

للاستفادة الكاملة من مطابق النمط، يُمكنك تخصيص الكيانات الخاصة بك. سوف نجعل "floorName" قائمة بالطوابق المُتوفرة.

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

تطبيق نموذجنا علـى Recognizer

الآن مـن الضروري تطبيق النموذج على IntentRecognizer. مـن الممكن استخدام نماذج متعددة في وقت واحد حتى تأخذ واجهة برمجة التطبيقات مجموعة من النماذج.

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

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

التعرف على الهدف

من الكائنIntentRecognizer، ستطلب طريقةRecognizeOnceAsync(). تطلب هذه الطريقة من خدمة الكلام التعرف على الكلام في عبارة واحدة، والتوقف عن التعرف على الكلام بمجرد تحديد العبارة. من أجل التبسيط، سننتظر حتى يكتمل.

إدراج هـذه التعليمة البرمجية أسفل أهدافك:

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

عرض نتائج التعرف (أو الأخطاء)

عند إرجاع نتيجة التعرف بواسطة خدمة الكلام، سوف نقوم بطباعة النتيجة.

أدخِل هذا الرمز أسفل 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);

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

إنشاء تطبيقك وتشغيله

الآن أنت مستعد لإنشاء التطبيق واختبار التعرف على الكلام باستخدام خدمة التعرف على الكلام.

  1. ترجمة التعليمات البرمجية - من شريط قوائم Visual Studio، اختر إنشاء>إنشاء حل.
  2. ابدأ تشغيل تطبيقك - من شريط القوائم، اخترتصحيح الأخطاء>بدء التصحيح أو اضغط F5.
  3. بدء التعرف - سوف يطالبك بقول شيء ما. اللغة الافتراضية هـي اللغة الإنجليزية. يُرسل الكلام إلى خدمة التعرف على الكلام ونسخه كنص وتقديمه في وحدة التحكم.

على سبيل المثال، إذا قلت "خذني إلى الطابق 2"، يجب أن يكون هذا الناتج:

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

مثال آخر إذا قلت "خذني إلى الطابق 7"، يجب أن يكون هذا هـو الإخراج:

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

مُعرف الهدف فارغ لأن 7 لم يكن في قائمتنا.

وثائق مرجعية | نماذج إضافية على GitHub

في هذا التشغيل السريع، يمكنك تثبيت Speech SDK ل Java.

متطلبات النظام الأساسي

اختر البيئة المستهدفة:

يتوافق Speech SDK لـ Java مع أنظمة التشغيل Windows وLinux وmacOS.

في Windows، يجب استخدام بنية الهدف 64 بت. مطلوب Windows 10 أو إصدار أحدث.

قم بتثبيت Microsoft Visual C++ Redistributable ل Visual Studio 2015 و2017 و2019 و2022 للنظام الأساسي الخاص بك. يتطلب التثبيت لأول مرة إعادة تشغيل.

لا يدعم Speech SDK ل Java Windows على ARM64.

تثبيت Java Development Kit مثل Azul Zulu OpenJDK. يجب أن يعمل Microsoft Build of OpenJDK أو JDK المفضل لديك أيضًا.

تثبيت SDK الكلام لـ Java

بعض الإرشادات تستخدم إصدار SDK معينًا مثل 1.24.2. للتحقق من أحدث إصدار، ابحث في مستودع GitHub.

اختر البيئة المستهدفة:

هذا الدليل يوضح كيفية تثبيت Speech SDK لـ Java على Java Runtime.

أنظمة التشغيل المدعومة

تُتاح Speech SDK لحزمة Java لأنظمة التشغيل هذه:

اتبع هذه الخطوات لتثبيت Speech SDK لـ Java باستخدام Apache Maven:

  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. افتح Main.java من src dir.

  2. استبدال محتويات الملف بالتعليمات البرمجية التالية:

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

إنشاء تكوين الكلام.

قبل أن تتمكن من تهيئة عنصر IntentRecognizer ، تحتاج إلى إنشاء تكوين يستخدم المفتاح ومنطقة Azure لمورد التنبؤ بخدمات Azure الذكاء الاصطناعي.

  • استبدل "YOUR_SUBSCRIPTION_KEY" بمفتاح توقع خدمات Azure الذكاء الاصطناعي.
  • استبدل "YOUR_SUBSCRIPTION_REGION" بمنطقة مورد خدمات Azure الذكاء الاصطناعي.

يستخدم هذا النموذج أسلوبfromSubscription()لإنشاء SpeechConfig. للحصول على قائمة كاملة بالطرق المتاحة، راجعفئة SpeechConfig.

تهيئة المتعرف على الأهداف

الآن أنشئ IntentRecognizer. إدراج هذا الكود مباشرةً أسفل تكوين الكلام. نقوم بذلك في محاولة حتى نستفيد من الواجهة القابلة للاخفاء التلقائي.

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

}

إضافة بعض الأهداف

تحتاج إلى إقران بعض الأنماط بـ PatternMatchingModel وتطبيقها على IntentRecognizer. سوف نبدأ بإنشاء PatternMatchingModel وإضافة بعض الأهداف إليه.

إشعار

يمكننا إضافة أنماط متعددة إلى PatternMatchingIntent.

إدخال هذا الرمز داخل الكتلة try:

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

إضافة بعض الكيانات المُخصصة

للاستفادة الكاملة من مطابق النمط، يُمكنك تخصيص الكيانات الخاصة بك. سوف نجعل "floorName" قائمة بالطوابق المُتوفرة. سوف نجعل أيضًا "parkingLevel" كيانًا صحيحًا.

إدراج هـذه التعليمة البرمجية أسفل أهدافك:

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

تطبيق نموذجنا علـى Recognizer

الآن مـن الضروري تطبيق النموذج على IntentRecognizer. مـن الممكن استخدام نماذج متعددة في وقت واحد حتى تأخذ واجهة برمجة التطبيقات مجموعة من النماذج.

أدخل هذه التعليمة أسفل الكيانات الخاصة بك:

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

recognizer.applyLanguageModels(modelCollection);

التعرف على الهدف

من الكائنIntentRecognizer، ستطلب طريقةRecognizeOnceAsync(). تطلب هذه الطريقة من خدمة الكلام التعرف على الكلام في عبارة واحدة، والتوقف عن التعرف على الكلام بمجرد تحديد العبارة.

قم بإدراج هذه التعليمات البرمجية بعد تطبيق نماذج اللغة:

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

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

عرض نتائج التعرف (أو الأخطاء)

عند إرجاع نتيجة التعرف بواسطة خدمة الكلام، سوف نقوم بطباعة النتيجة.

أدخِل هذا الرمز أسفل 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?");
    }
}

تحقق من الكود الخاص بك.

في هذه النقطة، يجب أن تظهر التعليمات البرمجية كذلك:

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

إنشاء تطبيقك وتشغيله

أنت الآن جاهز لإنشاء تطبيقك واختبار التعرف على الهدف باستخدام خدمة الكلام ومطابق النمط المضمن.

حدد زر التشغيل في Eclipse أو اضغط على ctrl+F11، ثم شاهد الإخراج ل "Say something..." موجه. بمجرد ظهوره، تحدث كلامك وشاهد الإخراج.

على سبيل المثال، إذا قلت "خذني إلى الطابق 2"، يجب أن يكون هذا الناتج:

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

كمثال آخر إذا قلت "خذني إلى الطابق 7"، يجب أن يكون هـذا الناتج:

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

لم يتم التعرف على أي هدف لأن 7 لم يكن في قائمة القيم الصالحة لـ floorName.