[[Tutorial]] ** The first application [#a2cd5868] *** Create Recognizer: [#rdb14941] We will use the static boost::python library (libboost_python.a) and the dynamic version of the Python library (libpython.so). We will change the path in the Makefile to point to your boost installation directory. Let’s get some code running. You’ll need to include the correct headers, as you might imagine. #highlight(C++){{ #include <boost/python.hpp> #include <Python.h> #include <dlfcn.h> }} These header files are added in the beginning of the program as usual. Then move to the member function onInit()and add the following lines. #highlight(python){{ Py_Initialize(); py::object main_module = py::import("__main__"); py::object main_namespace = main_module.attr("__dict__"); }} Note that you must initialize the Python interpreter directly (line 1). After initializing, the __main__ module is imported and the namespace is extracted. This results in a blank canvas upon which we can then call Python code, adding modules and variables. Note: The initialization of interpreter, import of __main__ module and namespace is independent of the member functions that are first used in. Thus you can do this in some other member function like onAction() which is a loop function in Sigverse. The only requirement is that they should be used before using any python functionality. Also, you dont have to use loop/loop functions to initialize the interpreter. Thus, its recommended to initialize the interpreter inside onInit() member function. #highlight(python){{ Py_Initialize(); }} #highlight(python){{ boost::python::exec("print 'Hello, world'", main_namespace); boost::python::exec("print 'Hello, world'[3:5]", main_namespace); boost::python::exec("print '.'.join(['1','2','3'])", main_namespace); }} *** Add event handler: [#vf81deb6] #highlight(c#){{ ... // The result will be processed in SpeechEngine_SpeechRecognized function _speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs> (SpeechEngine_SpeechRecognized); ... }} *** Create Grammar: [#fd5fd131] #highlight(c#){{ ... GrammarBuilder CMD = new GrammarBuilder(); CMD.Append(new Choices("Please take a cup of", "I need some", "take a cup of")); //Providence T. F. Green Internatinoal Airport GrammarBuilder OBJ = new GrammarBuilder(); Choices OBJchoices = new Choices("coffee", "tea", "milk", "hot water"); OBJ.Append(OBJchoices); OBJ.Append(new SemanticResultValue("Object")); CMD.Append(OBJ); //Boston Logan Internatinoal Airport GrammarBuilder CTRL = new GrammarBuilder(); Choices CTRLchoices = new Choices("go back", "go ahead", "turn left", "turn right", "stop"); CTRL.Append(CTRLchoices); CTRL.Append(new SemanticResultValue("CTRL")); // Final choices Choices finalChoices = new Choices("Hi", "Hello", "What is your name", "How are you", "How are you today", "How'r you", "Are you ready"); finalChoices.Add(CMD); finalChoices.Add(CTRL); //Final Grammar GrammarBuilder dialogGrammar = new GrammarBuilder(); dialogGrammar.Culture = culture; dialogGrammar.Append(new SemanticResultKey("Dialog",finalChoices)); // Load grammar _speechRecognitionEngine.LoadGrammar(dialogGrammar); ... }} *** Set input source for Recognizer: [#eb3310db] #highlight(c#){{ ... // Set input to Default Audio device _speechRecognitionEngine.SetInputToDefaultAudioDevice(); ... }} *** Start recognize: [#b48271a3] #highlight(c#){{ ... // start asynchronous recognizer try { _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple); } catch (InvalidOperationException e) { _isRecognizing = false; LogLine("Failed"); LogLine(e.Message); return false; } // start synchronous recognizer try { _speechRecognitionEngine.Recognize(); } catch (InvalidOperationException e) { LogLine(e.Message); return false; } }} ** Send result text to an Agent in SIGverse [#b49c4788] *** Import sigverse.dll [#afb97c89] Follow this tutorial [[import DLL:http://www.sociointelligenesis.org/SIGVerse/index.php?%E3%83%A1%E3%83%83%E3%82%BB%E3%83%BC%E3%82%B8%E9%80%81%E4%BF%A1%E3%83%84%E3%83%BC%E3%83%AB%E3%81%AE%E4%BD%9C%E6%88%90]] *** Send result text to SIGverse [#of8c83cf] #highlight(c#){{ sbyte[] String2Sbytes(string text) { sbyte[] osb = new sbyte[text.Length]; byte[] b = Encoding.ASCII.GetBytes(text); for (int i = 0; i < b.Length; i++) { osb[i] = (sbyte)b[i]; } return osb; } void SendMessage2Agent(string hostname, string port, string agentname, string message) { MessageSender messageSender; sbyte[] rs = String2Sbytes(message + "\0"); unsafe { sbyte[] _msgname = String2Sbytes("Microsoft Speech API"); fixed (sbyte* _mn = _msgname) { messageSender = new MessageSender(_mn); fixed (sbyte* _hn = String2Sbytes(hostname)) { if (messageSender.connect(_hn, Convert.ToInt32(port))) { LogLine("Connected to [" + hostname + ":" + port + "]"); sbyte* a = stackalloc sbyte[rs.Length]; for (int i = 0; i < rs.Length; i++) { a[i] = rs[i]; } sbyte** _rs = &a; sbyte[] agent = String2Sbytes(agentname); fixed (sbyte* _agent = agent) { if (messageSender.sendto(_agent, 1, _rs)) { LogLine("message [" + message + "] has been sent to [" + hostname + ":" + port + "]"); } else { LogLine("error to send message to [" + hostname + ":" + port + "]"); } } } else { LogLine("Can not connect to [" + hostname + ":" + port + "]. Please check ssh connection from SIGviewer"); } } } } } // handle recognized event void SpeechEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { ... // Send message to SIGverse's Agent here SendMessage2Agent(_hostNameSIGverse.Text, _portSIGverse.Text, _agentSIGverse.Text, e.Result.Text.ToLower()); ... } }}