gpt4 book ai didi

java - 如何将 AIResponse gson 转换为可在 Text to Speech 上使用的文本?

转载 作者:行者123 更新时间:2023-12-02 10:46:46 27 4
gpt4 key购买 nike

您好,我正在使用 Dialog Flow Example 中的代码我正在尝试自定义 AIButtonSampleActivity 类。

当我说话并且应用程序调用 Dialog 的 Flow API 时,AI 响应将显示为弹出窗口和 JSON。我正在尝试将该 JSON 或 GS​​ON 转换为文本,然后创建一个文本到语音的函数。

我怎样才能在这个类上做到这一点?

    /**
* 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);
}
}

这是我收到的回复 Response from the API我想获取参数 {"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);

关于java - 如何将 AIResponse gson 转换为可在 Text to Speech 上使用的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52497417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com