Speech Recognition - Dialogue GUI goes away after first selection

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
kenlem
Posts: 26
Joined: Sun Aug 16, 2020 5:19 pm

Speech Recognition - Dialogue GUI goes away after first selection

Post by kenlem »

Hi,

I'm working on using speech recognition to select option. I followed the example from your site. The conversation I have works fine when triggered with a mouse. When an option is triggered with voice, OnClick(selectedResponse), the conversation ends. Any suggestions?

I'll send you link directly to a sample project in case you need that.

Thanks.


Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
using PixelCrushers;
using System.Linq;

public class SpeechDialogueUI : StandardDialogueUI
{
	public Response[] responses;
	
	SpeechController speechController;
	
	public override void ShowResponses(Subtitle subtitle, Response[] responses, float timeout)
	{
		base.ShowResponses(subtitle, responses, timeout);
		
		Debug.Log("Show Responses");
		if (speechController == null) {
			speechController = FindObjectOfType<SpeechController>();
		}
		
		speechController.StartRecognition();
		this.responses = responses;
		Debug.Log("Found " + responses.Length + " responses");
	}
	
	public override void HideResponses()
	{
		base.HideResponses();
		Debug.Log("HideResponses");
		speechController.EndRecognition();
	}
	
	public void PartialResultReceived(string result)
	{
		Debug.Log("UI: partial: " + result);
	}
	
	public void ResultReceived(string result)
	{
		Debug.Log("UI: result: " + result);
		
		// Loop through responses and see which one scores highest.		
		string[] words = result.Split(' ');
		
		int selectedResponse = -1;
		float maxScore = 0.0f;
		for (int i=0; i<responses.Length; i++) {
			float score = scoreResponse(words, responses[i].destinationEntry.DialogueText);
			if (score > maxScore) {
				selectedResponse = i;
				maxScore = i;
			}
			
			Debug.Log(score + " " + responses[i].destinationEntry.DialogueText);
		}
		
		if (selectedResponse > -1) {
			Debug.Log("Selected " + responses[selectedResponse].destinationEntry.DialogueText);
			OnClick(selectedResponse);
		}
	}

	private static float scoreResponse(string[] src, string test)
	{
		float score = 0.0f;
		float matchCnt = 0.0f;
		string[] dst = test.Split(' ');
		
		int len = Mathf.Min(src.Length, dst.Length);
		
		for (int i=0; i<len; i++) {
			if (FuzzyMatch.LevenshteinDistance(src[i], dst[i]) <= 1) {
				matchCnt += 1.0f;
			}
		}
		
		if (matchCnt > 0.0f) {
			score = matchCnt / (float)Mathf.Max(src.Length, dst.Length);
		}

		return score;	
	}
}

User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Speech Recognition - Dialogue GUI goes away after first selection

Post by Tony Li »

Hi,

Pass the entire Response object to OnClick(), not the index in the responses array. Change this line:

Code: Select all

OnClick(selectedResponse);
to this:

Code: Select all

OnClick(responses[selectedResponse]);
Post Reply