parent
8737c0479e
commit
2846fcf27e
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 7.7 KiB |
@ -0,0 +1,38 @@
|
||||
package net.micode.notes.speech.setting;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.view.Window;
|
||||
|
||||
import com.iflytek.speech.util.SettingTextWatcher;
|
||||
|
||||
/**
|
||||
* 听写设置界面
|
||||
*/
|
||||
public class IatSettings extends PreferenceActivity implements OnPreferenceChangeListener {
|
||||
|
||||
public static final String PREFER_NAME = "com.iflytek.setting";
|
||||
private EditTextPreference mVadbosPreference;
|
||||
private EditTextPreference mVadeosPreference;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
super.onCreate(savedInstanceState);
|
||||
getPreferenceManager().setSharedPreferencesName(PREFER_NAME);
|
||||
|
||||
mVadbosPreference = (EditTextPreference) findPreference("iat_vadbos_preference");
|
||||
mVadbosPreference.getEditText().addTextChangedListener(new SettingTextWatcher(IatSettings.this, mVadbosPreference, 0, 10000));
|
||||
|
||||
mVadeosPreference = (EditTextPreference) findPreference("iat_vadeos_preference");
|
||||
mVadeosPreference.getEditText().addTextChangedListener(new SettingTextWatcher(IatSettings.this, mVadeosPreference, 0, 10000));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
package net.micode.notes.speech.util;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
/**
|
||||
* Json结果解析类
|
||||
*/
|
||||
public class JsonParser {
|
||||
|
||||
public static String parseIatResult(String json) {
|
||||
StringBuffer ret = new StringBuffer();
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(json);
|
||||
JSONObject joResult = new JSONObject(tokener);
|
||||
|
||||
JSONArray words = joResult.getJSONArray("ws");
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
// 转写结果词,默认使用第一个结果
|
||||
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
|
||||
JSONObject obj = items.getJSONObject(0);
|
||||
ret.append(obj.getString("w"));
|
||||
// 如果需要多候选结果,解析数组其他字段
|
||||
// for(int j = 0; j < items.length(); j++)
|
||||
// {
|
||||
// JSONObject obj = items.getJSONObject(j);
|
||||
// ret.append(obj.getString("w"));
|
||||
// }
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static String parseGrammarResult(String json, String engType) {
|
||||
StringBuffer ret = new StringBuffer();
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(json);
|
||||
JSONObject joResult = new JSONObject(tokener);
|
||||
|
||||
JSONArray words = joResult.getJSONArray("ws");
|
||||
// 云端和本地结果分情况解析
|
||||
if ("cloud".equals(engType)) {
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
|
||||
for (int j = 0; j < items.length(); j++) {
|
||||
JSONObject obj = items.getJSONObject(j);
|
||||
if (obj.getString("w").contains("nomatch")) {
|
||||
ret.append("没有匹配结果.");
|
||||
return ret.toString();
|
||||
}
|
||||
ret.append("【结果】" + obj.getString("w"));
|
||||
ret.append("【置信度】" + obj.getInt("sc"));
|
||||
ret.append("\n");
|
||||
}
|
||||
}
|
||||
} else if ("local".equals(engType)) {
|
||||
ret.append("【结果】");
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
JSONObject wsItem = words.getJSONObject(i);
|
||||
JSONArray items = wsItem.getJSONArray("cw");
|
||||
if ("<contact>".equals(wsItem.getString("slot"))) {
|
||||
// 可能会有多个联系人供选择,用中括号括起来,这些候选项具有相同的置信度
|
||||
ret.append("【");
|
||||
for (int j = 0; j < items.length(); j++) {
|
||||
JSONObject obj = items.getJSONObject(j);
|
||||
if (obj.getString("w").contains("nomatch")) {
|
||||
ret.append("没有匹配结果.");
|
||||
return ret.toString();
|
||||
}
|
||||
ret.append(obj.getString("w")).append("|");
|
||||
}
|
||||
ret.setCharAt(ret.length() - 1, '】');
|
||||
} else {
|
||||
//本地多候选按照置信度高低排序,一般选取第一个结果即可
|
||||
JSONObject obj = items.getJSONObject(0);
|
||||
if (obj.getString("w").contains("nomatch")) {
|
||||
ret.append("没有匹配结果.");
|
||||
return ret.toString();
|
||||
}
|
||||
ret.append(obj.getString("w"));
|
||||
}
|
||||
}
|
||||
ret.append("【置信度】" + joResult.getInt("sc"));
|
||||
ret.append("\n");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ret.append("没有匹配结果.");
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static String parseGrammarResult(String json) {
|
||||
StringBuffer ret = new StringBuffer();
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(json);
|
||||
JSONObject joResult = new JSONObject(tokener);
|
||||
|
||||
JSONArray words = joResult.getJSONArray("ws");
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
|
||||
for (int j = 0; j < items.length(); j++) {
|
||||
JSONObject obj = items.getJSONObject(j);
|
||||
if (obj.getString("w").contains("nomatch")) {
|
||||
ret.append("没有匹配结果.");
|
||||
return ret.toString();
|
||||
}
|
||||
ret.append("【结果】" + obj.getString("w"));
|
||||
ret.append("【置信度】" + obj.getInt("sc"));
|
||||
ret.append("\n");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ret.append("没有匹配结果.");
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static String parseLocalGrammarResult(String json) {
|
||||
StringBuffer ret = new StringBuffer();
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(json);
|
||||
JSONObject joResult = new JSONObject(tokener);
|
||||
|
||||
JSONArray words = joResult.getJSONArray("ws");
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
|
||||
for (int j = 0; j < items.length(); j++) {
|
||||
JSONObject obj = items.getJSONObject(j);
|
||||
if (obj.getString("w").contains("nomatch")) {
|
||||
ret.append("没有匹配结果.");
|
||||
return ret.toString();
|
||||
}
|
||||
ret.append("【结果】" + obj.getString("w"));
|
||||
ret.append("\n");
|
||||
}
|
||||
}
|
||||
ret.append("【置信度】" + joResult.optInt("sc"));
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ret.append("没有匹配结果.");
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static String parseTransResult(String json, String key) {
|
||||
StringBuffer ret = new StringBuffer();
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(json);
|
||||
JSONObject joResult = new JSONObject(tokener);
|
||||
String errorCode = joResult.optString("ret");
|
||||
if (!errorCode.equals("0")) {
|
||||
return joResult.optString("errmsg");
|
||||
}
|
||||
JSONObject transResult = joResult.optJSONObject("trans_result");
|
||||
ret.append(transResult.optString(key));
|
||||
/*JSONArray words = joResult.getJSONArray("results");
|
||||
for (int i = 0; i < words.length(); i++) {
|
||||
JSONObject obj = words.getJSONObject(i);
|
||||
ret.append(obj.getString(key));
|
||||
}*/
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.iflytek.speech.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 输入框输入范围控制
|
||||
*/
|
||||
public class SettingTextWatcher implements TextWatcher {
|
||||
private int editStart;
|
||||
private int editCount;
|
||||
private EditTextPreference mEditTextPreference;
|
||||
int minValue;//最小值
|
||||
int maxValue;//最大值
|
||||
private Context mContext;
|
||||
|
||||
public SettingTextWatcher(Context context, EditTextPreference e, int min, int max) {
|
||||
mContext = context;
|
||||
mEditTextPreference = e;
|
||||
minValue = min;
|
||||
maxValue = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
// Log.e("demo", "onTextChanged start:"+start+" count:"+count+" before:"+before);
|
||||
editStart = start;
|
||||
editCount = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
// Log.e("demo", "beforeTextChanged start:"+start+" count:"+count+" after:"+after);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (TextUtils.isEmpty(s)) {
|
||||
return;
|
||||
}
|
||||
String content = s.toString();
|
||||
// Log.e("demo", "content:"+content);
|
||||
if (isNumeric(content)) {
|
||||
int num = Integer.parseInt(content);
|
||||
if (num > maxValue || num < minValue) {
|
||||
s.delete(editStart, editStart + editCount);
|
||||
mEditTextPreference.getEditText().setText(s);
|
||||
Toast.makeText(mContext, "超出有效值范围", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
s.delete(editStart, editStart + editCount);
|
||||
mEditTextPreference.getEditText().setText(s);
|
||||
Toast.makeText(mContext, "只能输入数字哦", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则表达式-判断是否为数字
|
||||
*/
|
||||
public static boolean isNumeric(String str) {
|
||||
Pattern pattern = Pattern.compile("[0-9]*");
|
||||
return pattern.matcher(str).matches();
|
||||
}
|
||||
|
||||
};
|
@ -0,0 +1,55 @@
|
||||
package com.iflytek.speech.util;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
/**
|
||||
* Xml结果解析类
|
||||
*/
|
||||
public class XmlParser {
|
||||
|
||||
public static String parseNluResult(String xml) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
try {
|
||||
// DOM builder
|
||||
DocumentBuilder domBuilder = null;
|
||||
// DOM doc
|
||||
Document domDoc = null;
|
||||
|
||||
// init DOM
|
||||
DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
|
||||
domBuilder = domFact.newDocumentBuilder();
|
||||
InputStream is = new ByteArrayInputStream(xml.getBytes());
|
||||
domDoc = domBuilder.parse(is);
|
||||
|
||||
// 获取根节点
|
||||
Element root = (Element) domDoc.getDocumentElement();
|
||||
|
||||
Element raw = (Element) root.getElementsByTagName("rawtext").item(0);
|
||||
buffer.append("【识别结果】" + raw.getFirstChild().getNodeValue());
|
||||
buffer.append("\n");
|
||||
|
||||
Element e = (Element) root.getElementsByTagName("result").item(0);
|
||||
|
||||
Element focus = (Element) e.getElementsByTagName("focus").item(0);
|
||||
buffer.append("【FOCUS】" + focus.getFirstChild().getNodeValue());
|
||||
buffer.append("\n");
|
||||
|
||||
Element action = (Element) e.getElementsByTagName("action").item(0);
|
||||
Element operation = (Element) action.getElementsByTagName("operation").item(0);
|
||||
buffer.append("【ACTION】" + operation.getFirstChild().getNodeValue());
|
||||
buffer.append("\n");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
buffer.append("【ALL】" + xml);
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package net.micode.notes.ui;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.iflytek.cloud.SpeechUtility;
|
||||
|
||||
public class SpeechApplication extends Application {
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
SpeechUtility.createUtility(SpeechApplication.this, "appid=802516ec");
|
||||
super.onCreate();
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 5.8 KiB |
Loading…
Reference in new issue