如何将 AIResponse gson 转换为可用于 Text to Speech 的文本?
How to convert AIResponse gson to text that can be used on Text to Speech?
您好,我正在使用 Dialog Flow Example 中的代码,我正在尝试自定义 class AIButtonSampleActivity。
当我说话并且应用程序调用 Dialog 的 Flow API 时,AI 响应显示为弹出窗口和 JSON。我正在尝试将 JSON 或 GSON 转换为文本,然后创建一个文本到语音的函数。
我如何在这个 class 中做到这一点?
/**
* 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
- simple usage tutorial
此处将 onResult
中的 resultTextView.setText(gson.toJson(response));
替换为
JSONObject base = new JSONObject(gson.toJson(response));
String speechText = base.getJSONObject("results").getJSONObject("fulfillment").getString("speech");
resultTextView.setText(speechText);
您好,我正在使用 Dialog Flow Example 中的代码,我正在尝试自定义 class AIButtonSampleActivity。
当我说话并且应用程序调用 Dialog 的 Flow API 时,AI 响应显示为弹出窗口和 JSON。我正在尝试将 JSON 或 GSON 转换为文本,然后创建一个文本到语音的函数。
我如何在这个 class 中做到这一点?
/**
* 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);
}
}
这是我收到的回复
您需要使用 JSONObject
- simple usage tutorial
此处将 onResult
中的 resultTextView.setText(gson.toJson(response));
替换为
JSONObject base = new JSONObject(gson.toJson(response));
String speechText = base.getJSONObject("results").getJSONObject("fulfillment").getString("speech");
resultTextView.setText(speechText);