Tutorial 2 - Adding Voice Experiences to Your App with Custom NLP
Built-in NLP is a great way to grasp the potential of voice in VR. For your users to get most of what voice can do in your own VR experience, you’ll want to train your dedicated Wit app. Custom intents, entities, and traits can be combined with built-ins to design your app in exactly the way you want.
In this tutorial, we’ll build a simple app that lets users use their voice to change the color of 3D shapes. It will go over the basics of integrating the Voice SDK, training a Wit app, and activating voice commands.
Before proceeding with this tutorial, complete the setup steps outlined in
Voice SDK tutorials to create a project with the necessary dependencies. This tutorial builds
upon that project.
Adding Voice Experiences to Your App with Custom Intents, Entities, and Traits
Step 1: Setting Up the Scene
- In a blank scene in your new Unity project, right-click the Hierarchy window and then select GameObject > 3D Objects > Cube.
- Select the added cube and go to the Inspector window and set Position X to
-2.5. This will move the cube over to make room for other shapes. - Repeat the steps above to add a sphere, a capsule and a cylinder. Set their Position X to –
0.75, 0.75 and 2.5 respectively. - Right click on the Hierarchy window and select Create Empty and name it Shapes to group the shapes together.
Select the four shapes and drag them into the Shapes GameObject.
- In Unity, create a new GameObject and call it Canvas.
- Right-click on the GameObject and then select Button - TextMeshPro. Name the button element Button. Open its Hierarchy window and change the name of the text item to Button Label. Place it at the bottom of the screen.
Step 3: Enabling App Voice Experience in the Scene
To programmatically utilize the Wit response, you’ll need to add an App Voice Experience GameObject.
Click Assets > Create > Voice SDK > Add App Voice Experience to Scene, and select the App Voice Experience GameObject.
Drag the wit configuration file you created before into its slot in the AppVoiceExperience component.
Step 4: Training Your Wit App
- Open the Wit app (on the wit.ai website) that you linked to your unity app.
- On the Understanding tab, enter make the cube green in the Utterance field.
Under Intent, enter change_color to create a new intent.
Under Utterance, highlight “cube” and then enter shape in the Entity for “cube” field. Click + Create Entity.
- Highlight green and create a new entity and call it color.
- Click Train and Validate to train your app.
Repeat steps 4 through 6 with other possible utterances a user might say, such as Set the sphere to red, Make the cylinder green, and so on.
TIP: After training, the app will start to identify entities on its own. However, it can sometimes make mistakes, especially initially. If this is an issue, try training several phrases and then tweaking the NLU`s mistakes along the way. Highlight the word that should be matched and set the correct entity. You can then click the X next to the incorrect entities to remove them.
On the Entities tab, verify that the following entities are present:
Step 5: Improve Matches and Provide Synonyms
Matches will improve over time, but it’s best to improve the accuracy of your app by including synonyms for your shapes. By default, Wit.ai attempts to match entities with both free text and keywords. However, in cases such as this where you have specific names of the shapes to work with, you can improve the precision a little more by switching to a Keyword lookup strategy.
- Open the Entities tab and choose a shape entity to open the entity configuration page.
- Under Lookup Strategies, select Keywords, and then add the names and likely synonyms of each shape. For instance, you can include the synonym
tube along with the shape name cylinder. This makes the app return the text cylinder from the intent callback if a user says tube.
Note: In the Keyword field, make sure you match the case of the GameObject you created in Unity, so it can find that GameObject when the intent callback is triggered.
Step 6: Testing Utterances with the Understanding Viewer
With a trained Wit app for recognizing utterances to change a shape’s color, you can test some commands with the Understanding Viewer.
- Select Meta > Voice SDK > Understanding Viewer.
- Enter the cube should be green for the utterance, and then click Submit.
- Browse the response payload returned from the utterance.
The response payload will contain the extracted values for the intent (
change_color) and entities (shape and color) from your utterance.
Step 7: Add a Response Handler for Voice Commands
When a user speaks a command, the Voice SDK will send the utterance to the Wit API to do NLU processing. After the processing is complete, it will send back a response containing the extracted intent, entities, and other relevant fields.
To handle this response, set up a response handler as follows:
- In Unity, select the App Voice Experience GameObject in the Hierarchy window.
- Right-click Create Empty and make it a child GameObject to App Voice Experience.
- Name the new GameObject Response Handler.
Step 8: Consuming the Wit Entity Values
To consume the entity values, a value handler needs to be added and linked to a script. This script will contain the logic for applying the shape and color value to the 3D shape GameObjects in the scene.
- Click Meta > Voice SDK > Understanding Viewer, and then select the foldout for entities to view the shape and color value.
- Go to the entities > color:color >** 0** and click value.
- Select Add response matcher to Color Handler.
For the value of the shape entity, click entities > shape:shape > 0 > value = cube. In the popup window, select Add response matcher to Response Handler.
- In the Hierarchy window, select the Shapes GameObject.
- In the Inspector window, click Add Component.
- Select New Script and name the new script ColorChanger.
Add the following code to the script:
using System;
using UnityEngine;
public class ColorChanger : MonoBehaviour
{
/// <summary>
/// Sets the color of the specified transform.
/// </summary>
/// <param name="trans"></param>
/// <param name="color"></param>
private void SetColor(Transform trans, Color color)
{
trans.GetComponent<Renderer>().material.color = color;
}
/// <summary>
/// Updates the color of GameObject with the names specified in the input values.
/// </summary>
/// <param name="values"></param>
public void UpdateColor(string[] values)
{
var colorString = values[0];
var shapeString = values[1];
if (!ColorUtility.TryParseHtmlString(colorString, out var color)) return;
if (string.IsNullOrEmpty(shapeString)) return;
foreach (Transform child in transform) // iterate through all children of the gameObject.
{
if (child.name.IndexOf(shapeString, StringComparison.OrdinalIgnoreCase) != -1) // if the name exists
{
SetColor(child, color);
return;
}
}
}
}
- In the Hierarchy window, under App Voice Experience, select the Response Handler GameObject. Open the Wit Response Matcher (Script) window. Click + under On Multi Value Event(String []), and then drag the Shapes GameObject into its slot. From the function dropdown, select ColorChanger.UpdateColor().
Step 9: Test the Integration
- Run your app by pressing Play.
- Click Meta > Voice SDK > Understanding Viewer to open the viewer.
Choose Activate and say Make the capsule red.