Intenties herkennen met aangepaste entiteitspatronen die overeenkomen

De Speech SDK van Azure AI-services heeft een ingebouwde functie voor het herkennen van intenties met eenvoudige taalpatronen. Een intentie is iets wat de gebruiker wil doen: een venster sluiten, een selectievakje markeren, tekst invoegen, enzovoort.

In deze handleiding gebruikt u de Speech SDK om een consoletoepassing te ontwikkelen die intenties afleidt van spraakuitingen die worden gesproken via de microfoon van uw apparaat. U leert het volgende:

  • Een Visual Studio-project maken dat verwijst naar het NuGet-pakket van de Speech SDK
  • Een spraakconfiguratie maken en een intentieherkenning ophalen
  • Intenties en patronen toevoegen via de Speech SDK-API
  • Aangepaste entiteiten toevoegen via de Speech SDK-API
  • Asynchrone, gebeurtenisgestuurde continue herkenning gebruiken

Wanneer gebruikt u patroonkoppeling

Gebruik patroonkoppeling als:

  • U bent alleen geïnteresseerd in het vergelijken van strikt wat de gebruiker zei. Deze patronen komen agressief overeen dan conversationele language understanding (CLU).
  • U hebt geen toegang tot een CLU-model, maar wilt toch intenties.

Zie het overzicht van patroonkoppelingen voor meer informatie.

Vereisten

Zorg ervoor dat u de volgende items hebt voordat u aan deze handleiding begint:

Een project maken

Maak een nieuw C#-consoletoepassingsproject in Visual Studio 2019 en installeer de Speech SDK.

Beginnen met standaardcode

We gaan code openen Program.cs en toevoegen die werkt als een skelet voor ons project.

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

Een Speech-configuratie maken

Voordat u een IntentRecognizer object kunt initialiseren, moet u een configuratie maken die gebruikmaakt van de sleutel en De Azure-regio voor uw Voorspellingsresource voor Azure AI-services.

  • Vervang door "YOUR_SUBSCRIPTION_KEY" de voorspellingssleutel van uw Azure AI-services.
  • Vervang deze door "YOUR_SUBSCRIPTION_REGION" de resourceregio van uw Azure AI-services.

In dit voorbeeld wordt de methode FromSubscription() gebruikt om de SpeechConfig te maken. Zie SpeechConfig Class voor een volledige lijst met beschikbare methoden.

Een IntentRecognizer initialiseren

Maak nu een IntentRecognizer. Voeg deze code toe onder uw Speech-configuratie.

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

Enkele intenties toevoegen

U moet enkele patronen koppelen aan een PatternMatchingModel en deze toepassen op de IntentRecognizer. We beginnen met het maken van een PatternMatchingModel en een paar intenties eraan toe te voegen.

Notitie

We kunnen meerdere patronen toevoegen aan een PatternMatchingIntent.

Voeg deze code in het using blok in:

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

Enkele aangepaste entiteiten toevoegen

Als u optimaal gebruik wilt maken van de patroonmatcher, kunt u uw entiteiten aanpassen. We maken 'floorName' een lijst met de beschikbare verdiepingen. We maken ook 'parkingLevel' een entiteit geheel getal.

Voeg deze code in onder uw intenties:

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

Ons model toepassen op de Recognizer

Nu is het nodig om het model toe te passen op het IntentRecognizer. Het is mogelijk om meerdere modellen tegelijk te gebruiken, zodat de API een verzameling modellen gebruikt.

Voeg deze code in onder uw entiteiten:

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

recognizer.ApplyLanguageModels(modelCollection);

Een intentie herkennen

Vanuit het IntentRecognizer-object roept u de methode RecognizeOnceAsync() aan. Met deze methode wordt de Speech-service gevraagd om spraak in één woordgroep te herkennen en te stoppen met het herkennen van spraak zodra de woordgroep is geïdentificeerd.

Voeg deze code in nadat u de taalmodellen hebt toegepast:

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

var result = await recognizer.RecognizeOnceAsync();

De herkenningsresultaten (of fouten) weergeven

Wanneer het herkenningsresultaat wordt geretourneerd door de Speech-service, wordt het resultaat afgedrukt.

Voeg deze code toe onder 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?");
    }
}

Uw code controleren

Op dit punt moet uw code er als volgt uitzien:

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

De app bouwen en uitvoeren

U bent nu klaar om uw app te bouwen en de spraakherkenning te testen met behulp van de Speech-service.

  1. De code compileren: kies in de menubalk van Visual Studio Build>Build Solution.
  2. Start uw app: kies in de menubalk Debug>Start Debugging of druk op F5.
  3. Herkenning starten - U wordt gevraagd iets te zeggen. De standaardtaal is Engels. Uw spraak wordt verzonden naar de Speech-service, getranscribeerd als tekst en weergegeven in de console.

Als u bijvoorbeeld 'Neem me mee naar verdieping 2', moet dit de uitvoer zijn:

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

Als een ander voorbeeld als u "Neem me naar verdieping 7", moet dit de uitvoer zijn:

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

Er is geen intentie herkend omdat 7 niet in onze lijst met geldige waarden voor floorName stond.

Een project maken

Maak een nieuw C++-consoletoepassingsproject in Visual Studio 2019 en installeer de Speech SDK.

Beginnen met standaardcode

We gaan code openen helloworld.cpp en toevoegen die werkt als een skelet voor ons project.

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

Een Speech-configuratie maken

Voordat u een IntentRecognizer object kunt initialiseren, moet u een configuratie maken die gebruikmaakt van de sleutel en De Azure-regio voor uw Voorspellingsresource voor Azure AI-services.

  • Vervang door "YOUR_SUBSCRIPTION_KEY" de voorspellingssleutel van uw Azure AI-services.
  • Vervang deze door "YOUR_SUBSCRIPTION_REGION" de resourceregio van uw Azure AI-services.

In dit voorbeeld wordt de methode FromSubscription() gebruikt om de SpeechConfig te maken. Zie SpeechConfig Class voor een volledige lijst met beschikbare methoden.

Een IntentRecognizer initialiseren

Maak nu een IntentRecognizer. Voeg deze code toe onder uw Speech-configuratie.

    auto intentRecognizer = IntentRecognizer::FromConfig(config);

Enkele intenties toevoegen

U moet enkele patronen koppelen aan een PatternMatchingModel en deze toepassen op de IntentRecognizer. We beginnen met het maken van een PatternMatchingModel en een paar intenties eraan toe te voegen. Een PatternMatchingIntent is een struct, dus we gebruiken alleen de in-line syntaxis.

Notitie

We kunnen meerdere patronen toevoegen aan een 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");

Enkele aangepaste entiteiten toevoegen

Als u optimaal gebruik wilt maken van de patroonmatcher, kunt u uw entiteiten aanpassen. We maken 'floorName' een lijst met de beschikbare verdiepingen.

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

Ons model toepassen op de Recognizer

Nu is het nodig om het model toe te passen op het IntentRecognizer. Het is mogelijk om meerdere modellen tegelijk te gebruiken, zodat de API een verzameling modellen gebruikt.

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

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

Een intentie herkennen

Vanuit het IntentRecognizer-object roept u de methode RecognizeOnceAsync() aan. Met deze methode wordt de Speech-service gevraagd om spraak in één woordgroep te herkennen en te stoppen met het herkennen van spraak zodra de woordgroep is geïdentificeerd. Voor de eenvoud zullen we wachten tot de toekomstige retournering voltooid is.

Voeg deze code in onder uw intenties:

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

De herkenningsresultaten (of fouten) weergeven

Wanneer het herkenningsresultaat wordt geretourneerd door de Speech-service, wordt het resultaat afgedrukt.

Voeg deze code toe onder 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;
}

Uw code controleren

Op dit punt moet uw code er als volgt uitzien:

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

De app bouwen en uitvoeren

U bent nu klaar om uw app te bouwen en de spraakherkenning te testen met behulp van de Speech-service.

  1. De code compileren: kies in de menubalk van Visual Studio Build>Build Solution.
  2. Start uw app: kies in de menubalk Debug>Start Debugging of druk op F5.
  3. Herkenning starten - U wordt gevraagd iets te zeggen. De standaardtaal is Engels. Uw spraak wordt verzonden naar de Speech-service, getranscribeerd als tekst en weergegeven in de console.

Als u bijvoorbeeld 'Neem me mee naar verdieping 2', moet dit de uitvoer zijn:

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

Een ander voorbeeld als u "Neem me naar verdieping 7", dit moet de uitvoer zijn:

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

De intentie-id is leeg omdat 7 niet in onze lijst staat.

Referentiedocumentatie | Aanvullende voorbeelden op GitHub

In deze quickstart installeert u de Speech SDK voor Java.

Platformvereisten

Kies uw doelomgeving:

De Speech SDK voor Java is compatibel met Windows, Linux en macOS.

In Windows moet u de 64-bits doelarchitectuur gebruiken. Windows 10 of hoger is vereist.

Installeer Microsoft Visual C++ Redistributable voor Visual Studio 2015, 2017, 2019 en 2022 voor uw platform. Als u dit pakket voor de eerste keer installeert, moet u mogelijk opnieuw opstarten.

De Speech SDK voor Java biedt geen ondersteuning voor Windows in ARM64.

Installeer een Java Development Kit zoals Azul Zulu OpenJDK. De Microsoft Build van OpenJDK of uw favoriete JDK moet ook werken.

De Speech SDK voor Java installeren

Sommige instructies gebruiken een specifieke SDK-versie, zoals 1.24.2. Als u de nieuwste versie wilt controleren, zoekt u in onze GitHub-opslagplaats.

Kies uw doelomgeving:

Deze handleiding laat zien hoe u de Speech SDK voor Java installeert in de Java Runtime.

Ondersteunde besturingssystemen

Het Speech SDK-pakket voor Java is beschikbaar voor deze besturingssystemen:

Volg deze stappen om de Speech SDK voor Java te installeren met behulp van Apache Maven:

  1. Installeer Apache Maven.

  2. Open een opdrachtprompt waarin u het nieuwe project wilt opnemen en maak een nieuw pom.xml bestand.

  3. Kopieer de volgende XML-inhoud naar 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. Voer de volgende Maven-opdracht uit om de Speech SDK en afhankelijkheden te installeren.

    mvn clean dependency:copy-dependencies
    

Beginnen met standaardcode

  1. Open Main.java vanuit de src dir.

  2. Vervang de inhoud van het bestand door het volgende:

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

Een Speech-configuratie maken

Voordat u een IntentRecognizer object kunt initialiseren, moet u een configuratie maken die gebruikmaakt van de sleutel en De Azure-regio voor uw Voorspellingsresource voor Azure AI-services.

  • Vervang door "YOUR_SUBSCRIPTION_KEY" de voorspellingssleutel van uw Azure AI-services.
  • Vervang deze door "YOUR_SUBSCRIPTION_REGION" de resourceregio van uw Azure AI-services.

In dit voorbeeld wordt de methode fromSubscription() gebruikt om de SpeechConfig te maken. Zie SpeechConfig Class voor een volledige lijst met beschikbare methoden.

Een IntentRecognizer initialiseren

Maak nu een IntentRecognizer. Voeg deze code toe onder uw Speech-configuratie. We doen dit in een poging, zodat we profiteren van de autosluitbare interface.

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

}

Enkele intenties toevoegen

U moet enkele patronen koppelen aan een PatternMatchingModel en deze toepassen op de IntentRecognizer. We beginnen met het maken van een PatternMatchingModel en een paar intenties eraan toe te voegen.

Notitie

We kunnen meerdere patronen toevoegen aan een PatternMatchingIntent.

Voeg deze code in het try blok in:

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

Enkele aangepaste entiteiten toevoegen

Als u optimaal gebruik wilt maken van de patroonmatcher, kunt u uw entiteiten aanpassen. We maken 'floorName' een lijst met de beschikbare verdiepingen. We maken ook 'parkingLevel' een entiteit geheel getal.

Voeg deze code in onder uw intenties:

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

Ons model toepassen op de Recognizer

Nu is het nodig om het model toe te passen op het IntentRecognizer. Het is mogelijk om meerdere modellen tegelijk te gebruiken, zodat de API een verzameling modellen gebruikt.

Voeg deze code in onder uw entiteiten:

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

recognizer.applyLanguageModels(modelCollection);

Een intentie herkennen

Vanuit het IntentRecognizer-object roept u de methode RecognizeOnceAsync() aan. Met deze methode wordt de Speech-service gevraagd om spraak in één woordgroep te herkennen en te stoppen met het herkennen van spraak zodra de woordgroep is geïdentificeerd.

Voeg deze code in nadat u de taalmodellen hebt toegepast:

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

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

De herkenningsresultaten (of fouten) weergeven

Wanneer het herkenningsresultaat wordt geretourneerd door de Speech-service, wordt het resultaat afgedrukt.

Voeg deze code toe onder 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?");
    }
}

Uw code controleren

Op dit punt moet uw code er als volgt uitzien:

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

De app bouwen en uitvoeren

U bent nu klaar om uw app te bouwen en onze intentieherkenning te testen met behulp van de spraakservice en de ingesloten patroonmatcher.

Selecteer de knop Uitvoeren in Eclipse of druk op Ctrl+F11 en bekijk vervolgens de uitvoer voor de tekst 'Iets zeggen...' Prompt. Zodra deze wordt weergegeven, spreekt u uw uiting en bekijkt u de uitvoer.

Als u bijvoorbeeld 'Neem me mee naar verdieping 2', moet dit de uitvoer zijn:

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

Als een ander voorbeeld als u "Neem me naar verdieping 7", moet dit de uitvoer zijn:

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

Er is geen intentie herkend omdat 7 niet in onze lijst met geldige waarden voor floorName stond.