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) reprinted 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 server in the world, and Iris on android usesHttp://start.csail.mit.edu/(Natural voice Q & A System) the interface provided by this website and a smart chat platform called cleverbotHttp://www.cleverbot.com/This chat website supports Chinese. However, it only supports PinYin Input-Khan.

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/I intelligent chatbot.

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);if (m.find()) {   msg = m.group(1);}

Through the aforementioned small I Chat Robot interface, you can implement a simple Siri that can chat freely. I robot is still very intelligent, chat conversation is also very interesting, but only chat, this and iphone
There is a big gap between 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.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {String strResult = EntityUtils.toString(httpResponse.getEntity());Pattern p = Pattern.compile("sessionId = .(\\d+)"); //get sessionIdMatcher 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.execute(httpRequest1);String strRevAsk = Recv_Path + "SID=" + mSessionId+ "&USR=" + mSessionId + "&r=";HttpGet httpRequest2 = new HttpGet(strRevAsk);httpResponse = httpClient.execute(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.execute(httpRequest);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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.execute(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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return msg;}}

Usage: XiaoI xiaoi = new XiaoI ();
Xiaoi. initialize ();
Xiaoi. sendMsg (mQuestion );
Results = xiaoi. revMsg ();

Because 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.