Snippets

Furhat Robotics Language switching in flow

Created by Ludvig Linse
  <!-- In your flow, you need to change between the contexts -->
  
  <state id="start" extends="Dialog">
        <var name="lang" type="String" value="en"/>
        
		<onentry>
			<agent:say>German or English?</agent:say>
		</onentry>
		
        <onevent name="sense.user.speak" cond="event?:sem:german">
			<exec>lang = "de"</exec>
	        <send event="action.context.default" p:context="'german'" />
			<send event="action.voice" p:name="'SOME_GERMAN_VOICE'"/>
			<goto state="next"/>
		</onevent>
        
		<onevent name="sense.user.speak" cond="event?:sem:english">
			<exec>lang = "en"</exec>
			<send event="action.context.default" p:context="'english'" />
			<send event="action.voice" p:name="'SOME_ENGLISH_VOICE'"/>
			<goto state="next"/>
		</onevent>
	</state> 
	
	<state id="next">
		<onentry>
			<if cond="eq(lang, 'de')">
				<agent:say>Schönen Tag</agent:say>
			<else/>
				<agent:say>Hello there</agent:say>
			</if>
		</onentry>
	</state>
// In YourSkill.java you have to create the contexts


@Override
public void init() throws Exception {

    // ...
    // Create english context and set an open vocabulary context
    handler.loadContext("english", new OpenVocabularyContext(language));
	handler.loadContext("english", new SemanticGrammarContext(new SRGSGrammar(getPackageFile("YourGrammar_en.xml"))));
	
    // Create german context, this time with a list of german phrases that the regozniser should give prominance to
    handler.loadContext("german", new OpenVocabularyContext(language, german_phrases));
	handler.loadContext("german", new SemanticGrammarContext(new SRGSGrammar(getPackageFile("YourGrammar_de.xml"))));
    
    // Set english as default
	handler.setDefaultContext("default");
    
    // ...
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.