DefaultVoiceMsgParser.java
7.55 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.ys.yslibrary.paraser;
import android.text.TextUtils;
import android.util.Log;
import com.iflytek.aiui.AIUIEvent;
import com.ys.yslibrary.callback.OnVoiceIdentifyResultListener;
import com.ys.yslibrary.bean.RawMessage;
import org.json.JSONArray;
import org.json.JSONObject;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;
import static com.ys.yslibrary.bean.RawMessage.FromType.AIUI;
import static com.ys.yslibrary.bean.RawMessage.FromType.USER;
import static com.ys.yslibrary.bean.RawMessage.MsgType.TEXT;
import static com.ys.yslibrary.bean.RawMessage.MsgType.Voice;
public class DefaultVoiceMsgParser implements AbsVoiceMsgParser {
private final String TAG = getClass().getName();
private OnVoiceIdentifyResultListener onVoiceIdentifyResultListener;
//处理PGS听写(流式听写)的数组
private String[] mIATPGSStack = new String[256];
private List<String> mInterResultStack = new LinkedList<>();
private DefaultVoiceMsgParser voiceMsgParser;
//当前未结束的语音交互消息,更新语音消息的听写内容时使用
private RawMessage mAppendVoiceMsg = null;
/**
* 重置流式识别的信息
*/
@Override
public void resetAppendVoiceMsg() {
mAppendVoiceMsg = new RawMessage(USER, Voice, null);
mAppendVoiceMsg.cacheContent = "";
//语音消息msgData为录音时长
mAppendVoiceMsg.msgData = ByteBuffer.allocate(4).putFloat(0).array();
mInterResultStack.clear();
}
@Override
public boolean isHasUserCache() {
return (mAppendVoiceMsg != null && !TextUtils.isEmpty(mAppendVoiceMsg.cacheContent));
}
/**
* 处理AIUI结果事件(听写结果和语义结果)
*
* @param event 结果事件
*/
@Override
public RawMessage processResult(AIUIEvent event) {
RawMessage rawMessage = null;
try {
JSONObject bizParamJson = new JSONObject(event.info);
JSONObject data = bizParamJson.getJSONArray("data").getJSONObject(0);
JSONObject params = data.getJSONObject("params");
JSONObject content = data.getJSONArray("content").getJSONObject(0);
long rspTime = event.data.getLong("eos_rslt", -1); //响应时间
String sub = params.optString("sub");
if (content.has("cnt_id") && !sub.equals("tts")) {
String cnt_id = content.getString("cnt_id");
JSONObject cntJson = new JSONObject(new String(event.data.getByteArray(cnt_id), "utf-8"));
//听写结束语义结果
if ("nlp".equals(sub)) {
JSONObject semanticResult = cntJson.optJSONObject("intent");
if (semanticResult != null && semanticResult.length() != 0) {
//解析得到语义结果,将语义结果作为消息插入到消息列表中
rawMessage = new RawMessage(AIUI, TEXT,
semanticResult.toString().getBytes(), null, rspTime);
// addChatMessage(rawMessage);
Log.e(TAG, "processResult: " + rawMessage);
if (onVoiceIdentifyResultListener != null) {
onVoiceIdentifyResultListener.onResult(false, rawMessage);
}
}
return rawMessage;
}
//流式响应数据
else if ("iat".equals(sub)) {
return processIATResult(cntJson);
}
}
} catch (Exception e) {
}
return rawMessage;
}
/**
* 解析听写结果更新当前语音消息的听写内容
*/
@Override
public RawMessage processIATResult(JSONObject cntJson) {
RawMessage rawMessage = null;
if (mAppendVoiceMsg == null) return rawMessage;
try {
JSONObject text = cntJson.optJSONObject("text");
// 解析拼接此次听写结果
StringBuilder iatText = new StringBuilder();
JSONArray words = text.optJSONArray("ws");
boolean lastResult = text.optBoolean("ls");
for (int index = 0; index < words.length(); index++) {
JSONArray charWord = words.optJSONObject(index).optJSONArray("cw");
for (int cIndex = 0; cIndex < charWord.length(); cIndex++) {
iatText.append(charWord.optJSONObject(cIndex).opt("w"));
}
}
String voiceIAT = "";
String pgsMode = text.optString("pgs");
//非PGS模式结果
if (TextUtils.isEmpty(pgsMode)) {
if (TextUtils.isEmpty(iatText)) return rawMessage;
//和上一次结果进行拼接
if (!TextUtils.isEmpty(mAppendVoiceMsg.cacheContent)) {
voiceIAT = mAppendVoiceMsg.cacheContent;//+ "\n";
}
voiceIAT += iatText;
} else {
int serialNumber = text.optInt("sn");
mIATPGSStack[serialNumber] = iatText.toString();
//pgs结果两种模式rpl和apd模式(替换和追加模式)
if ("rpl".equals(pgsMode)) {
//根据replace指定的range,清空stack中对应位置值
JSONArray replaceRange = text.optJSONArray("rg");
int start = replaceRange.getInt(0);
int end = replaceRange.getInt(1);
for (int index = start; index <= end; index++) {
mIATPGSStack[index] = null;
}
}
StringBuilder PGSResult = new StringBuilder();
//汇总stack经过操作后的剩余的有效结果信息
for (int index = 0; index < mIATPGSStack.length; index++) {
if (TextUtils.isEmpty(mIATPGSStack[index])) continue;
// if(!TextUtils.isEmpty(PGSResult.toString())) PGSResult.append("\n");
PGSResult.append(mIATPGSStack[index]);
//如果是最后一条听写结果,则清空stack便于下次使用
if (lastResult) {
mIATPGSStack[index] = null;
}
}
voiceIAT = join(mInterResultStack) + PGSResult.toString();
if (lastResult) {
mInterResultStack.add(PGSResult.toString());
}
}
if (!TextUtils.isEmpty(voiceIAT)) {
mAppendVoiceMsg.cacheContent = voiceIAT;
if (onVoiceIdentifyResultListener != null) {
onVoiceIdentifyResultListener.onResult(true, mAppendVoiceMsg);
}
Log.e(TAG, "processIATResult: 流式结果" + mAppendVoiceMsg);
return mAppendVoiceMsg;
}
} catch (Exception e) {
}
return rawMessage;
}
@Override
public String getUserSpeakContent() {
if (mAppendVoiceMsg == null) {
return "";
}
return mAppendVoiceMsg.cacheContent;
}
private String join(List<String> data) {
StringBuilder builder = new StringBuilder();
for (int index = 0; index < data.size(); index++) {
builder.append(data.get(index));
}
return builder.toString();
}
public void setOnVoiceIdentifyResultListener(OnVoiceIdentifyResultListener onVoiceIdentifyResultListener) {
this.onVoiceIdentifyResultListener = onVoiceIdentifyResultListener;
}
}