Reprint Please specify source: Zhou Mushi's CSDN Blog
Http://blog.csdn.net/zhoumushui
The semantic analysis of Iflytek Voice SDK is very powerful, which makes our application more powerful.
The previous blog post introduces some of the simple features of the CyberLink SDK:
Android using the CyberLink Voice SDK
Today, let's look at the parsing and processing of semantic analysis results json:
Implementing voice dialing
First, let's look at the phrase "call Zhang San" after the server analysis, what is the JSON that is passed to us:
{"semantic{"slots": {"name": "张三"}},"rc0,"operation"CALL","service"telephone","text"打电话给张三。"}
So, our idea is to get to name, and then match the contact DisplayName in the contact ContentProvider one by one. But to take into account the problem of the same word (for example: "Call Angelzhang", the server returned the name is "Zhang quiet", this does not match.) In the absence of a match, we turn the name into pinyin and then compare it with the contact pinyin.
Take a look at the core code:
if ("Telephone". Equals(Strservice)) {//"Operation":"Call"String Peoplename = Jsonobject. Getjsonobject("Semantic"). Getjsonobject("Slots"). getString("Name");String operationstr = Jsonobject. getString("Operation");if ("Call". Equals(OPERATIONSTR)) {String Phonenum = getcontactnumberbyname (peoplename);String Phonecode ="";try {phonecode = Jsonobject. Getjsonobject("Semantic"). Getjsonobject("Slots"). getString("Code");} catch (Exception e) {} if (Phonenum! = null & Phonenum. Trim(). Length() >0) {String stranswer ="is calling:"+ Peoplename;Tvanswer. SetText(stranswer);Startspeak (Stranswer);Uri uri = Uri. Parse("Tel:"+ phonenum);Intent Intent = new Intent (Intent. ACTION_call, URI);StartActivity (Intent);} else if (phonecode! = null& Phonecode. Trim(). Length() >0) {String stranswer ="is calling:"+ Peoplename;Tvanswer. SetText(stranswer);Startspeak (Stranswer);Uri uri = Uri. Parse("Tel:"+ Phonecode);Intent Intent = new Intent (Intent. ACTION_call, URI);StartActivity (Intent);} else {String Phonenumfrompinyin = Getcontactnumberbypinyin (pinyinutil. ConvertAll(Peoplename));if (phonenumfrompinyin! = null& Phonenumfrompinyin. Trim(). Length() >0) {String stranswer ="is calling:"+ Peoplename;Tvanswer. SetText(stranswer);Startspeak (Stranswer);Uri uri = Uri. Parse("Tel:"+ Phonenumfrompinyin);Intent Intent = new Intent (Intent. ACTION_call, URI);StartActivity (Intent);} else {String stranswer ="not found in Address Book:"+ Peoplename;Tvanswer. SetText(stranswer);Startspeak (Stranswer);} } }}
Voice navigation
This is not considered in the case of how to walk from A to B (A, A, a). The start point is not the same as the current position, but is not commonly used. So here's an example where the starting point defaults to the current positioning position.
When we say "Navigate to Shenzhen North Station", the JSON returned by the server is as follows:
{"Semantic":{"Slots":{"Endloc":{"type": " loc_poi", "POI": " Shenzhen South railway Station","City": "Shenzhen", " cityaddr": " Shenzhen"},"Startloc":{"type": "Loc_poi", "City": "current_city", "POI": "Current_ POI "}}},"RC":0,"Operation":"ROUTE","Service":"Map","text":"Navigate to Shenzhen South railway station. "}
First we parse the JSON:
if("Map".equals(Strservice)) {//Operation ":" ROUTE " StringEndpoistr=Jsonobject.Getjsonobject ("Semantic").Getjsonobject ("Slots").Getjsonobject ("Endloc").GetString ("POI");StringEndcitystr=Jsonobject.Getjsonobject ("Semantic").Getjsonobject ("Slots").Getjsonobject ("Endloc").GetString ("City");StringEndaddressstr= "";if("Current_city".equals(ENDCITYSTR)) Endcitystr=Msharedpreferences.GetString ("CityName","Unknown");}
Call Baidu Map navigation interface, need to pass in the latitude and longitude of the starting point. The latitude and longitude of the current position is known, so the destination string is converted to latitude and longitude. This involves the use of the Baidu lbs SDK:
{ mEndSearch = GeoCoder.newInstance(); mEndSearch.setOnGetGeoCodeResultListener(new MyOnGetGeoCoderResultListener()); mEndSearch.geocode(new GeoCodeOption().city(endCityStr).address(endPoiStr)); }
class Myongetgeocoderresultlistener implements Ongetgeocoderresultlistener {@Override Public voidOngetgeocoderesult (geocoderesult result) {//TODO auto-generated method stubMENDLATLNG = Result.getlocation ();if(MENDLATLNG! =NULL) {//Starting point: Current positionStartlat = double.parsedouble (msharedpreferences.getstring ("Latitude","0.0")); STARTLNG = double.parsedouble (msharedpreferences.getstring ("Longitude","0.0"));//DestinationEndlat = Mendlatlng.latitude; ENDLNG = Mendlatlng.longitude; LATLNG STARTLATLNG =NewLATLNG (Startlat, STARTLNG); LATLNG ENDLATLNG =NewLATLNG (Endlat, ENDLNG);//Build navigation parametersNavipara para =NewNavipara (); Para.startpoint = STARTLATLNG; Para.startname ="Start from here."; Para.endpoint = ENDLATLNG; Para.endname ="End here.";Try{Baidumapnavigation.openbaidumapnavi (para, Getapplicationcontext ()); }Catch(Baidumapappnotsupportnaviexception e) {E.printstacktrace (); }}} @Override Public voidOngetreversegeocoderesult (Reversegeocoderesult result) {}}
Voice-activated Apps
When we say "open Baidu Map", the JSON returned by the server is:
{"semantic{"slots": {"name": "百度地图"}},"rc0,"operation"LAUNCH","service"app","text"打开百度地图。"}
Similar to voice dialing, we can get the name of the app and then match it to the name of the app in Resolveinfo, and if it matches, get the package name and start.
Note: Consider the case where the app name is non-Chinese. (For example, we say "start QQ", but the message is "QQ" recognition, if the simple rough string of the equals comparison, it will match the failure. You need to turn the app name and label to uppercase or lowercase. )
if ("App". Equals(Strservice)) { //"Operation":"LAUNCH", String appName = Jsonobject. Getjsonobject("Semantic"). Getjsonobject("Slots"). getString("Name");String operationstr = Jsonobject. getString("Operation");if ("LAUNCH". Equals(OPERATIONSTR)) {String PackageName = getapppackagebyname (appName);Toast. Maketext(Getapplicationcontext (), PackageName, Toast. LENGTH_short). Show();if (!"Com.tchip.carlauncher". Equals(PackageName)) {String stranswer ="Starting:"+ AppName;Tvanswer. SetText(stranswer);Startspeak (Stranswer);Startappbypackage (PackageName);} else {String stranswer ="app not found:"+ AppName;Tvanswer. SetText(stranswer);Startspeak (Stranswer);} }}
Reprint Please specify source: Zhou Mushi's CSDN Blog
Http://blog.csdn.net/zhoumushui
Android integrated Flight SDK for voice dialing, voice navigation, voice start applications