Build a Chinese Siri voice assistant for Android (1)-interfaces for mini I robots

Source: Internet
Author: User
Tags sendmsg

 

 

By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source

The Siri of the iPhone 4 S is eye-catching and there are countless videos on the Internet. It makes android users feel itchy. Fortunately, later experts in the android camp quickly attacked and launched Iris. The tragedy is that Iris only supports English, which makes us very bad at English. It's really depressing ~ _~

So I plan to use android resources to build a Chinese version of Siri, so that we can use Chinese to tease it at will. (I made a simple, haha, put in the youyi market, interested in children's shoes can go to experience the http://www.eoemarket.com/apps/61634)

 

 

 

First, we will analyze the structure of Siri, which can be roughly divided into three parts: Speech Recognition, natural language processing, and speech output. For speech recognition, we can use google's Speech Recognition API for speech recognition and convert speech into text. Voice output is actually played by using TTS and texts for speech synthesis. This android platform also has an interface. The real core is natural language recognition and processing. A large part of Siri's functionality depends on this. This requires a large database to maintain its operation, it cannot be implemented locally. Even if the iphone's Siri is also a command for speech recognition, the voice is uploaded to the Apple Server for resolution and returned. Because apple interfaces are not open, we cannot use their interfaces. Fortunately, there are more than one apple in the world with such servers, iris on android is the use of http://start.csail.mit.edu/(natural voice Q & A System) the interface provided by this website and a smart chat platform called cleverbot http://www.cleverbot.com/this web site is to support Chinese, just support the input of audio and audio.

 

Therefore, our core task is to find a Q & A system that supports Chinese characters. After a long search on the Internet, I found that, unfortunately, I did not find it (PS: if anyone finds a better website, please share it with me ), however, I found a better alternative to our demand for Siri-chatbots. http://www.xiaoi.com/widget/1007/xiaomi Smart Robot robot.

After a short period of exploration, I implemented a class to initialize the interface connecting the small I robot, send data and receive feedback. The interface address used is as follows:

 

 

Private String Webbot_Path = "http://webbot.xiaoi.com/engine/widget1007/webbot.js? Encoding = UTF-8 ";

Private String Send_Path = "http: // 122.226.240.164/engine/widget1007/send. js? Encoding = UTF-8 &";

Private String Recv_Path = "http: // 122.226.240.164/engine/widget1007/recv. js? Encoding = UTF-8 &";

 

The Webbot_Path above the http connection will return some data:

 

 

Var L_IDS_SEND_MESSAGE_URL = "http: // 122.226.240.164/engine/widget1007/send. js? Encoding = UTF-8 &";

Var L_IDS_RECV_MESSAGE_URL = "http: // 122.226.240.164/engine/widget1007/recv. js? Encoding = UTF-8 &";

Var L_IDS_GET_RESOURCE_URL = "http: // 122.226.240.164/engine/widget1007/getres. do ";

Var _ sessionId = "86491993134658194 ";

Document. write ("<script src = 'HTTP: // 122.226.240.164/engine/widget1007/_ core. js? Encoding = UTF-8 & '> <\/script> ");

The returned data packet includes the sending and receiving address and a sessionId. This sessionId is very important, similar to a key, used in the subsequent session. Because the sending and receiving addresses are fixed and can be written to death directly, but the sessionId is changed, you need to extract it from the feedback data first, I am using a simple regular expression:

 

String strResult = EntityUtils. toString (httpResponse. getEntity ());

Pattern p = Pattern. compile ("sessionId =. (\ d +)"); // get sessionId

Matcher m = p. matcher (strResult );

If (m. find () mSessionId = m. group (1 );

After we get the sessionId, We can initialize it. the initialization process is very simple. Enter the sessionId in the following format and send it to the server.

 

 

String strSendJoin = Send_Path + "SID =" + mSessionId + "& USR =" + mSessionId + "& CMD = JOIN & r = ";

 

 

After initialization, you can use the URL below to send questions and receive answers:

String strSend = Send_Path + "SID =" + mSessionId + "& USR =" + mSessionId + "& CMD = CHAT & SIG = You & MSG =" + msg + "& FTN = & FTS = & FTC = & r = ";

String strRec = Recv_Path + "SID =" + mSessionId + "& USR =" + mSessionId + "& r ="; xiaoi. sendMsg (mQuestion );

Results = xiaoi. revMsg ();

The received content also needs to be extracted, using a regular expression:

 

 

String msgTmp = EntityUtils. toString (httpResponse. getEntity ());

Pattern p = Pattern. compile ("\" MSG \":\"(.*?) \"");

Matcher m = p. matcher (msgTmp );

(M. find ()){

Msg = m. group (1 );

 

 

Through the aforementioned small I Chat Robot interface, you can implement a simple Siri that can chat freely. The I Robot is still very intelligent, and the chat conversation is also very interesting, but only chatting is supported. This is a big gap with iphone Siri, so we will add another intelligent brain to it later.

 

The complete code of this article is as follows:

 

Public class XiaoI {

 

Private String Webbot_Path = "http://webbot.xiaoi.com/engine/widget1007/webbot.js? Encoding = UTF-8 ";

Private String Send_Path = "http: // 122.226.240.164/engine/widget1007/send. js? Encoding = UTF-8 &";

Private String Recv_Path = "http: // 122.226.240.164/engine/widget1007/recv. js? Encoding = UTF-8 &";

 

Private String mSessionId = null;

Private HttpClient httpClient = null;

 

Public boolean initialize (){

Boolean success = false;

HttpParams httpParams = new BasicHttpParams ();

HttpConnectionParams. setConnectionTimeout (httpParams, 30000 );

HttpConnectionParams. setSoTimeout (httpParams, 30000 );

HttpClient = new DefaultHttpClient (httpParams );

Try {

String strGetId = Webbot_Path;

HttpGet httpRequest = new HttpGet (strGetId );

HttpResponse httpResponse = httpClient.exe cute (httpRequest );

If (httpResponse. getStatusLine (). getStatusCode () = HttpURLConnection. HTTP_ OK ){

String strResult = EntityUtils. toString (httpResponse

. GetEntity ());

Pattern p = Pattern. compile ("sessionId =. (\ d +)"); // get sessionId

Matcher m = p. matcher (strResult );

If (m. find ()){

MSessionId = m. group (1 );

String strSendJoin = Send_Path + "SID =" + mSessionId

+ "& USR =" + mSessionId + "& CMD = JOIN & r = ";

HttpGet httpRequest1 = new HttpGet (strSendJoin );

HttpResponse = httpClient.exe cute (httpRequest1 );

 

String strRevAsk = Recv_Path + "SID =" + mSessionId

+ "& USR =" + mSessionId + "& r = ";

HttpGet httpRequest2 = new HttpGet (strRevAsk );

HttpResponse = httpClient.exe cute (httpRequest2 );

Success = true;

}

}

} Catch (ClientProtocolException e ){

E. printStackTrace ();

} Catch (IOException e ){

E. printStackTrace ();

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

Return success;

}

}

 

Public void sendMsg (String msg ){

String strTalksend = Send_Path + "SID =" + mSessionId + "& USR ="

+ MSessionId + "& CMD = CHAT & SIG = You & MSG =" + msg

+ "& FTN = & FTS = & FTC = & r = ";

HttpGet httpRequest = new HttpGet (strTalksend );

Try {

HttpClient.exe cute (httpRequest );

} Catch (ClientProtocolException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

 

Public String revMsg (){

String strTalkRec = Recv_Path + "SID =" + mSessionId + "& USR ="

+ MSessionId + "& r = ";

HttpGet httpRequest = new HttpGet (strTalkRec );

String msg = null;

Try {

HttpResponse httpResponse = httpClient.exe cute (httpRequest );

If (httpResponse. getStatusLine (). getStatusCode () = 200 ){

String msgTmp = EntityUtils. toString (httpResponse. getEntity ());

Pattern p = Pattern. compile ("\" MSG \":\"(.*?) \"");

Matcher m = p. matcher (msgTmp );

If (m. find ()){

Msg = m. group (1 );

}

}

} Catch (ClientProtocolException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

Return msg;

 

}

}

 

Usage: XiaoI xiaoi = new XiaoI ();

Xiaoi. initialize ();

Xiaoi. sendMsg (mQuestion );

Results = xiaoi. revMsg ();

 

 

Since sending and receiving takes a lot of time, it is best to put it in the background for processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.