灵云语义理解


转载请说明出处!
作者:kqw攻城狮
出处:个人站 | CSDN


效果图

效果图

源码

GitHub

SDK下载

灵云SDK下载

SDK集成

下载SDK以后,将jar和so导入工程

导入SDK

权限

1
2
3
4
5
6
7
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

封装

灵云配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package kong.qingwei.kqwhcidemo;
/**
* Created by kqw on 2016/8/12.
* 灵云配置信息
*/
public final class ConfigUtil {
/**
* 灵云APP_KEY
*/
public static final String APP_KEY = "3d5d5466";
/**
* 开发者密钥
*/
public static final String DEVELOPER_KEY = "eca643ff7b3c758745d7cf516e808d34";
/**
* 灵云云服务的接口地址
*/
public static final String CLOUD_URL = "test.api.hcicloud.com:8888";
/**
* 需要运行的灵云能力
*/
public static final String CAP_KEY = "tts.local.synth";
// public static final String CAP_KEY = "tts.cloud.wangjing";
public static final String CAP_KEY_NUL = "nlu.cloud";
}

初始化灵云语音能力的工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package kong.qingwei.kqwhcidemo;
import android.app.Activity;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.sinovoice.hcicloudsdk.api.HciCloudSys;
import com.sinovoice.hcicloudsdk.common.AuthExpireTime;
import com.sinovoice.hcicloudsdk.common.HciErrorCode;
import com.sinovoice.hcicloudsdk.common.InitParam;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by kqw on 2016/8/12.
* 初始化灵云语音
*/
public class HciUtil {
private static final String TAG = "HciUtil";
private Activity mActivity;
private final String mConfigStr;
public HciUtil(Activity activity) {
mActivity = activity;
// 加载信息,返回InitParam, 获得配置参数的字符串
InitParam initParam = getInitParam();
mConfigStr = initParam.getStringConfig();
}
public boolean initHci() {
// 初始化
int errCode = HciCloudSys.hciInit(mConfigStr, mActivity);
if (errCode != HciErrorCode.HCI_ERR_NONE && errCode != HciErrorCode.HCI_ERR_SYS_ALREADY_INIT) {
Toast.makeText(mActivity, "hciInit error: " + HciCloudSys.hciGetErrorInfo(errCode), Toast.LENGTH_SHORT).show();
return false;
}
// 获取授权/更新授权文件 :
errCode = checkAuthAndUpdateAuth();
if (errCode != HciErrorCode.HCI_ERR_NONE) {
// 由于系统已经初始化成功,在结束前需要调用方法hciRelease()进行系统的反初始化
Toast.makeText(mActivity, "CheckAuthAndUpdateAuth error: " + HciCloudSys.hciGetErrorInfo(errCode), Toast.LENGTH_SHORT).show();
HciCloudSys.hciRelease();
return false;
}
return true;
}
/**
* 释放
*/
public void hciRelease(){
HciCloudSys.hciRelease();
}
/**
* 加载初始化信息
*
* @return 系统初始化参数
*/
private InitParam getInitParam() {
String authDirPath = mActivity.getFilesDir().getAbsolutePath();
// 前置条件:无
InitParam initparam = new InitParam();
// 授权文件所在路径,此项必填
initparam.addParam(InitParam.AuthParam.PARAM_KEY_AUTH_PATH, authDirPath);
// 是否自动访问云授权,详见 获取授权/更新授权文件处注释
initparam.addParam(InitParam.AuthParam.PARAM_KEY_AUTO_CLOUD_AUTH, "no");
// 灵云云服务的接口地址,此项必填
initparam.addParam(InitParam.AuthParam.PARAM_KEY_CLOUD_URL, ConfigUtil.CLOUD_URL);
// 开发者Key,此项必填,由捷通华声提供
initparam.addParam(InitParam.AuthParam.PARAM_KEY_DEVELOPER_KEY, ConfigUtil.DEVELOPER_KEY);
// 应用Key,此项必填,由捷通华声提供
initparam.addParam(InitParam.AuthParam.PARAM_KEY_APP_KEY, ConfigUtil.APP_KEY);
// 配置日志参数
String sdcardState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String packageName = mActivity.getPackageName();
String logPath = sdPath + File.separator + "sinovoice" + File.separator + packageName + File.separator + "log" + File.separator;
// 日志文件地址
File fileDir = new File(logPath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
// 日志的路径,可选,如果不传或者为空则不生成日志
initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_FILE_PATH, logPath);
// 日志数目,默认保留多少个日志文件,超过则覆盖最旧的日志
initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_FILE_COUNT, "5");
// 日志大小,默认一个日志文件写多大,单位为K
initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_FILE_SIZE, "1024");
// 日志等级,0=无,1=错误,2=警告,3=信息,4=细节,5=调试,SDK将输出小于等于logLevel的日志信息
initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_LEVEL, "5");
}
return initparam;
}
/**
* 获取授权
*
* @return 授权结果
*/
private int checkAuthAndUpdateAuth() {
// 获取系统授权到期时间
int initResult;
AuthExpireTime objExpireTime = new AuthExpireTime();
initResult = HciCloudSys.hciGetAuthExpireTime(objExpireTime);
if (initResult == HciErrorCode.HCI_ERR_NONE) {
// 显示授权日期,如用户不需要关注该值,此处代码可忽略
Date date = new Date(objExpireTime.getExpireTime() * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
Log.i(TAG, "expire time: " + sdf.format(date));
if (objExpireTime.getExpireTime() * 1000 > System.currentTimeMillis()) {
// 已经成功获取了授权,并且距离授权到期有充足的时间(>7天)
Log.i(TAG, "checkAuth success");
return initResult;
}
}
// 获取过期时间失败或者已经过期
initResult = HciCloudSys.hciCheckAuth();
if (initResult == HciErrorCode.HCI_ERR_NONE) {
Log.i(TAG, "checkAuth success");
return initResult;
} else {
Log.e(TAG, "checkAuth failed: " + initResult);
return initResult;
}
}
}

语义理解的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package kong.qingwei.kqwhcidemo;
import android.app.Activity;
import android.util.Log;
import com.sinovoice.hcicloudsdk.api.nlu.HciCloudNlu;
import com.sinovoice.hcicloudsdk.common.HciErrorCode;
import com.sinovoice.hcicloudsdk.common.Session;
import com.sinovoice.hcicloudsdk.common.nlu.NluConfig;
import com.sinovoice.hcicloudsdk.common.nlu.NluInitParam;
import com.sinovoice.hcicloudsdk.common.nlu.NluRecogResult;
/**
* Created by kqw on 2016/8/12.
* 语义理解类
*/
public class NluUtil {
private static final String TAG = "NluUtil";
private Activity mActivity;
public NluUtil(Activity activity) {
mActivity = activity;
}
public boolean initNul() {
//构造Asr初始化的帮助类的实例
NluInitParam initParam = new NluInitParam();
// 获取App应用中的lib的路径,放置能力所需资源文件。如果使用/data/data/packagename/lib目录,需要添加android_so的标记
String dataPath = mActivity.getFilesDir().getAbsolutePath().replace("files", "lib");
initParam.addParam(NluInitParam.PARAM_KEY_DATA_PATH, dataPath);
initParam.addParam(NluInitParam.PARAM_KEY_FILE_FLAG, NluInitParam.VALUE_OF_PARAM_FILE_FLAG_ANDROID_SO);
initParam.addParam(NluInitParam.PARAM_KEY_INIT_CAP_KEYS, ConfigUtil.CAP_KEY_NUL);
int errCode = HciCloudNlu.hciNluInit(initParam.getStringConfig());
Log.i(TAG, "initNul: errCode = " + errCode);
return errCode == HciErrorCode.HCI_ERR_NONE || errCode == HciErrorCode.HCI_ERR_NLU_ALREADY_INIT;
}
public void recog(String text, OnNluRecogListener onNluRecogListener) {
// 初始化配置参数
NluConfig nluConfig = initConfig();
// 创建会话
Session session = new Session();
// 开始会话
int errCode = HciCloudNlu.hciNluSessionStart(nluConfig.getStringConfig(), session);
if (errCode == HciErrorCode.HCI_ERR_NONE) {
// 开始翻译
// 调用HciCloudMt.hciMtTrans() 方法进行合成
NluRecogResult nluResult = new NluRecogResult();
errCode = HciCloudNlu.hciNluRecog(session, text, nluConfig.getStringConfig(), nluResult);
if (errCode == HciErrorCode.HCI_ERR_NONE) {
onNluRecogListener.onNluResult(nluResult);
} else {
onNluRecogListener.onError(errCode);
}
// 结束会话
errCode = HciCloudNlu.hciNluSessionStop(session);
if (errCode != HciErrorCode.HCI_ERR_NONE) {
onNluRecogListener.onError(errCode);
}
} else {
onNluRecogListener.onError(errCode);
}
}
private NluConfig initConfig() {
NluConfig nluConfig = new NluConfig();
nluConfig.addParam(NluConfig.SessionConfig.PARAM_KEY_CAP_KEY, ConfigUtil.CAP_KEY_NUL);
nluConfig.addParam("intention", "weather");
return nluConfig;
}
public interface OnNluRecogListener {
void onNluResult(NluRecogResult nluRecogResult);
void onError(int errorCode);
}
}

使用

初始化灵云的语音能力和语义理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 灵云语音工具类
mInitTts = new HciUtil(this);
// 初始化灵云语音
boolean isInitHci = mInitTts.initHci();
if (isInitHci) { // 初始化成功
……
// 语义理解
mNluUtil = new NluUtil(this);
boolean isInitNul = mNluUtil.initNul();
if (isInitNul) {
Toast.makeText(this, "语义理解 初始化成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "语义理解 初始化失败", Toast.LENGTH_SHORT).show();
}
}

语义理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 语义理解
*
* @param view
*/
public void recog(View view) {
final String text = mEditText2.getText().toString();
if (TextUtils.isEmpty(text)) {
Toast.makeText(this, "理解句子内容为空", Toast.LENGTH_SHORT).show();
return;
}
mNluUtil.recog(text, new NluUtil.OnNluRecogListener() {
@Override
public void onNluResult(NluRecogResult nluRecogResult) {
StringBuilder stringBuffer = new StringBuilder();
ArrayList<NluRecogResultItem> nluRecogResultItems = nluRecogResult.getRecogResultItemList();
for (NluRecogResultItem nluRecogResultItem : nluRecogResultItems) {
String result = nluRecogResultItem.getResult();
stringBuffer.append(result).append("\n");
Log.i(TAG, "onNluResult: " + result);
}
showDialog(text, stringBuffer.toString());
}
@Override
public void onError(int errorCode) {
Log.i(TAG, "onError: errorCode = " + errorCode);
}
});
}

Dialog

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 显示Dialog
*
* @param title title
* @param message message
*/
private void showDialog(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("确认", null);
builder.create().show();
}

注意

灵云的语义,想要识别哪个场景,必须要先开通对应场景,然后做配置以后才可以使用,类似这样

1
nluConfig.addParam("intention", "weather");

这里的天气,对应的参数是weather,了解更多场景和对应场景参数可以查看捷通华声灵云公有云能力平台NLU结果开发手册.pdf

坚持原创技术分享,您的支持将鼓励我继续创作!