annyang icon indicating copy to clipboard operation
annyang copied to clipboard

Allow defining a command prefix

Open TalAter opened this issue 11 years ago • 11 comments

For example:

annyang.setPrefix('OK, glass');

Now only sentences beggining with "OK, glass" will be parsed.

TalAter avatar Aug 30 '13 17:08 TalAter

Hey there, I think this idea would really help the script to gain popularity. Do you think including a prefix option would be difficult to implement based on the current state of the code?

spacerobot5 avatar Dec 13 '13 19:12 spacerobot5

This would be a great feature to have. I'm currently using Aanyang to test an AI script and have circumvented not having this feature by using a command like 'OK computer (please) *term'. Unfortunately, it's missing that nice audio cue that it's actually listening like Google uses when you say "OK Google" with Google Now. Having this feature would allow a more robust use of Aanyang for situations like this. Great work overall by the way on this!!!

michaelrod77 avatar Jul 09 '14 17:07 michaelrod77

+1

revett avatar Apr 16 '16 19:04 revett

+1

dpmontero avatar Sep 14 '16 11:09 dpmontero

Any news on the implementation of this? This is exactly what I require since I am running Annyang continuously on a machine I keep on, currently its picking up everything but with this prefix it would stop random speech recognition playing through my speakers every time I get too close to the microphone.

brendonexus avatar Nov 09 '16 13:11 brendonexus

It's possible to do this without needing a PR or any modification to the library using a key rename function. Simply add your commands in object format - e.g.;

var commands = {
	"Where am I?"		:	myLocationFunction,
	"What's the time?"	:	myTimeFunction,
};

Then, add the following rename function;

var rename = function(obj, prefix){
	if(typeof obj !== 'object' || !obj){
		return false;
	}
	var keys = Object.keys(obj),
		keysLen = keys.length,
		prefix = prefix || '';
	for(var i=0; i<keysLen ;i++){
		obj[prefix+keys[i]] = obj[keys[i]];
		if(typeof obj[keys[i]]=== 'object'){
			rename(obj[prefix+keys[i]],prefix);
		}
		delete obj[keys[i]];
	}
	return obj;
};

Usage is as follows; rename(commands, "Hello Annyang ");

I'd recommend adding a space on the end of your prefix (in this case, "Hello Annyang ". After that, simply call;

annyang.addCommands(commands);
annyang.start();

If you console.log(commands) after calling rename, you'll see it's prefixed all your commands with your specified prefix.

ChronSyn avatar Feb 10 '17 22:02 ChronSyn

If no one is taken this up can I take it up? I am a beginner and would like to do it.

codevenkat avatar Jan 18 '18 04:01 codevenkat

I did it a while back but author was not responding

quentin-sommer avatar Jan 18 '18 14:01 quentin-sommer

@ChronSyn this worked for me but only when I removed the question mark from after the "I?" but thanks as it's a nice workaround for the hotword detection.

LukeMcLachlan avatar Jan 27 '18 19:01 LukeMcLachlan

A work around I have implemented on my application for this is define a command to turn on a state (lets say the command was "Ok Tom" and the state is named "isListening") for 5 seconds. Then have other commands to check if "isListening" active, before actually executing the logic assigned to the given command.

phouthasak avatar Jul 18 '19 18:07 phouthasak

@ChronSyn thank you so much it worked for me :)

Siyamfahad avatar Nov 17 '21 03:11 Siyamfahad