View on GitHub

jsBotVoice

Create voice bots for browser

Español # English

Load

<script src="js/voice/voice.js"></script>
<script src="js/voice/voiceBrowser.js"></script>

Create new object Voice

First you need to create a Voice object. That object can receive as parameter the language. If you don’t pass it uses the browser language.

var voice = new Voice();//uses language from browser
var voice = new Voice("es");//spanish
var voice = new Voice("en");//english

Voice Recognition

VoiceCommand

For voice recognition you need use VoiceComand object.

function VoiceCommand() {
	this.name; //string
	this.expressions; //array of string
	this.execute; //function
}

Each VoiceCommand includes three fields:

Example:

var vc1 = new VoiceCommand();
vc1.name="hi";
vc1.expressions[0] = "bot# hello";
vc1.execute = function(exp, m, voice) {
	voice.talk("hi person");
};

After create voice command you need add this with addVoiceCommand

voice.addVoiceCommand(vcmd, topic);

vcmd: VoiceCommand to be added
topic: No obligatory. Conversation topic, voiceCommand will be used only with this topic. Default value is “default”

voice.addVoiceCommand(vc1);

Expressions

“Expressions” are regular expressions. Agent compare any thing you say with all exoresions in a VoiceCommand if any match execute the function in the VoiceCommand field.

Expressions doesn’t uses regular expressions directly, you need create tokens. Tokens are regular expressions that match with a part of text.

var useful_tokens = {
	"bot#": "chispa|chispas",
	"word#": "[a-bA-B]*",
	"number#": "[0-9]*",
	"any#": ".*"
}


var english_tokens = {
	"hello": "hi|hello",
	"sayhelloto": "say hello to",
	"sayhello": "say hello",
	"sing": "sing",
	"salsa": "salsa|salsita",
	"bachata": "bachata",
	"mynameis": "my name is|i am",
	"search": "search",
	"repeat": "say|repeat"
}

After you can use tokens to create a expression:

var vc1 = new VoiceCommand();
vc1.name="hi";
vc1.expressions[0] = "bot# hello";
vc1.execute = function(exp, m, voice) {
	voice.talk("hi person");
};


var vc2 = new VoiceCommand();
vc2.name="say hello to";
vc2.expressions[0] = "bot# sayhelloto any#?";
vc2.expressions[1] = "bot# sayhello";
vc2.execute = function(exp, m, voice) {
	if(exp == 0 && m.length > 3)
		voice.talk("hi "+"#"+m[3]);//m[3] - any#
	else
		voice.talk("hi");
};

You can add modifiers to tokens:

Maybe you prefer create you own regular expresion, no problem, you only need put * as first character in expression.

Speech Synthesis

To use speech text you must use talk command

voice.talk(text, topics); 

text: test to read. Each word is a token from dictionary. You can use these specail characters.

topics: No obligatory. Array of one or more topics. Default value is voice.topics value.

voice.talk("hi person"); 

voice.data["name"] = "Cubiwan";
voice.talk("hi $name"); //hello cubiwan

voice.talk("*hello my friend");

Dictionaries

You need create a dictionary of tokens. you could use few options for token. When you generates an expresion token will be remplace randomly by one of options.

var english_dictionary =  {
	"hi": ["hi", "hello"],
	"person": ["person","human"],
	"yes": ["yes","afirmative"],
	"no": ["no","negative"],
	"ok": ["ok", "right"]
};

Now you can add dictionaries to generate expresions

voice.addDictionary(english_dictionary);

addDictionary: Have two params

voice.data

It is a hashmap use to storage datas to be used in Speech Synthesis with command talk remplacing $token tokens. $token -> voice.data[‘token’]

voice.data["name"] = "Cubiwan";
voice.talk("hi $name"); //hello cubiwan

voice.topics

Array of topics use as default value of param topics . By default value is [“default”]

voice.topics.push("sing");

voice.analyze

If you don’t need use voice recognition but you can use expresions to analyze texts you can use function analyze.

voice.analyze(text, topics);

text: Text to analyze
topics: No obligatory. Array of one or more topics. Default value is voice.topics value.

voice.generateExpression

If you want generate a expresion but no transform that in voice you can use function generateExpression

voice.generateExpression = function(text, data, topics)

text: Tokens to transform in expression data: No obligatory. Map with data. Default value is voice.data. topics: No obligatory. Array of one or more topics. Default value is voice.topics value.

API Summary

Example

Demo