java - 如何将 AIResponse gson 转换为可用于 Text to Speech 的文本?
问题描述
嗨,我正在使用Dialog Flow Example中的代码,并且正在尝试自定义 AIButtonSampleActivity 类。
当我说话并且应用程序调用 Dialog 的 Flow API 时,AI 响应显示为弹出窗口和 JSON。我正在尝试将该 JSON 或 GSON 转换为文本,然后创建一个函数文本到语音。
我怎么能在这堂课上做到这一点?
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ai.api.sample;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.util.HashMap;
import java.util.Map;
import ai.api.android.AIConfiguration;
import ai.api.android.GsonFactory;
import ai.api.model.AIError;
import ai.api.model.AIResponse;
import ai.api.model.Metadata;
import ai.api.model.Result;
import ai.api.model.Status;
import ai.api.ui.AIButton;
public class AIButtonSampleActivity extends BaseActivity implements AIButton.AIButtonListener {
public static final String TAG = AIButtonSampleActivity.class.getName();
private AIButton aiButton;
private TextView resultTextView;
private Gson gson = GsonFactory.getGson();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aibutton_sample);
resultTextView = (TextView) findViewById(R.id.resultTextView);
aiButton = (AIButton) findViewById(R.id.micButton);
final AIConfiguration config = new AIConfiguration(Config.ACCESS_TOKEN,
AIConfiguration.SupportedLanguages.PortugueseBrazil,
AIConfiguration.RecognitionEngine.System);
config.setRecognizerStartSound(getResources().openRawResourceFd(R.raw.test_start));
config.setRecognizerStopSound(getResources().openRawResourceFd(R.raw.test_stop));
config.setRecognizerCancelSound(getResources().openRawResourceFd(R.raw.test_cancel));
aiButton.initialize(config);
aiButton.setResultsListener(this);
}
@Override
protected void onPause() {
super.onPause();
// use this method to disconnect from speech recognition service
// Not destroying the SpeechRecognition object in onPause method would block other apps from using SpeechRecognition service
aiButton.pause();
}
@Override
protected void onResume() {
super.onResume();
// use this method to reinit connection to recognition service
aiButton.resume();
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_aibutton_sample, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(AISettingsActivity.class);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onResult(final AIResponse response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "onResult");
resultTextView.setText(gson.toJson(response));
Log.i(TAG, "Received success response");
// this is example how to get different parts of result object
final Status status = response.getStatus();
Log.i(TAG, "Status code: " + status.getCode());
Log.i(TAG, "Status type: " + status.getErrorType());
final Result result = response.getResult();
Log.i(TAG, "Resolved query: " + result.getResolvedQuery());
Log.i(TAG, "Action: " + result.getAction());
final String speech = result.getFulfillment().getSpeech();
Log.i(TAG, "Speech: " + speech);
TTS.speak(speech);
final Metadata metadata = result.getMetadata();
if (metadata != null) {
Log.i(TAG, "Intent id: " + metadata.getIntentId());
Log.i(TAG, "Intent name: " + metadata.getIntentName());
}
final HashMap<String, JsonElement> params = result.getParameters();
if (params != null && !params.isEmpty()) {
Log.i(TAG, "Parameters: ");
for (final Map.Entry<String, JsonElement> entry : params.entrySet()) {
Log.i(TAG, String.format("%s: %s", entry.getKey(), entry.getValue().toString()));
}
}
}
});
}
@Override
public void onError(final AIError error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "onError");
resultTextView.setText(error.toString());
}
});
}
@Override
public void onCancelled() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "onCancelled");
resultTextView.setText("");
}
});
}
private void startActivity(Class<?> cls) {
final Intent intent = new Intent(this, cls);
startActivity(intent);
}
}
这是我收到的响应,我想获取参数{"messages":[{"speech":["Oi!"]}],"speech":"Oi!"},并将其转换为文本讲话
解决方案
你需要使用JSONObject
-简单使用教程
在这里替换resultTextView.setText(gson.toJson(response));
为onResult
JSONObject base = new JSONObject(gson.toJson(response));
String speechText = base.getJSONObject("results").getJSONObject("fulfillment").getString("speech");
resultTextView.setText(speechText);
推荐阅读
- angular - 创建组件时,将 [ngClass]="class" 和 [ngStyle]="style" 对元素执行什么操作
- c# - 截取面板截图
- google-apps-script - 谷歌电子表格中的逻辑异常/差异范围
- sql - 试图在同一用户的两个日期之间显示超过一天的数据?
- c# - AvalonDock FloatingWindow 填充内容
- sql - 使用 Soundex 创建一个函数以获取类似的发音词
- python - 如何在 BeautifulSoup 中删除以前的兄弟姐妹
- c# - 如何在 Ninject 的继承树中使用继承的 DbContext 的相同实例?
- spring - 为什么当restTemplate存在两个@Bean方法时Spring Boot应用程序启动
- c# - C# WinForms 基础用户控件