使用Java编写一个与图灵机器人进行对话的工具。

但图灵机器人只支持文本对话,并不支持语音交互,所以本程序运用了第三方库百度语音识别/合成,来实现语音交互。

我们还可以将下载好的音乐放入指定文件夹,进行简单的配置后还能点歌。

1.登录图灵机器人官网:http://www.turingapi.com/,完成注册、身份认证、创建机器人。一般一到两天后就能身份审核成功,创建好机器人之后就会生成自己的apikey:

2.登录百度智能云官网:http://ai.baidu.com/,右上角“控制台”进入登录界面。登录后点击“语音技术”、“创建应用”,应用创建好后就是呢过程自己的AppID,API Key,Secret Key:

3.完成以上准备工作就可以开始编码了。我们可以参考相关文档,以及下载SDK。不同语言有不同的SDK,根据自己所需来下载。我这里使用的Java语言编写,所以下载的是“识别、合成 RESTful API JAVA SDK”。

4.以下就是相关代码:

Record类完成录音、播放音频文件:

package com.feng.robot;import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;import java.io.*;
import java.util.Scanner;
import javax.sound.sampled.*;/*** 录音record(),录音完毕后将音频文件保存到指定文件夹中* 播放音频文件play(音频文件路径)*/
public class Record {private static AudioFormat audioFormat;static TargetDataLine targetDataLine;public File record() {System.out.println("想聊天,请按y。结束讲话,请按n。");Scanner input = new Scanner(System.in);String Sinput = input.next();if(Sinput.equals("y")){System.out.println("讲话中……");captureAudio();//调用录音方法}Scanner input_2 = new Scanner(System.in);String Sinput_2 = input_2.next();if(Sinput_2.equals("n")){targetDataLine.stop();targetDataLine.close();System.out.println("结束讲话。");}//结束录音后便在指定路径生成一个record.wav文件File audioFile = new File("F:\\record.wav");return audioFile;}//录音方法public void captureAudio(){try {audioFormat = new AudioFormat(16000,16,1,true,false);DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);new CaptureThread().start();//开启线程} catch (Exception e){e.printStackTrace();System.exit(0);}}class CaptureThread extends Thread {public void run() {AudioFileFormat.Type fileType = null;//指定的文件类型File audioFile = null;//设置文件类型和文件扩展名//根据选择的单选按钮。fileType = AudioFileFormat.Type.WAVE;audioFile = new File("F://record.wav");try {targetDataLine.open(audioFormat);targetDataLine.start();//当开始音频捕获或回放时,生成 START 事件。AudioSystem.write(new AudioInputStream(targetDataLine),fileType, audioFile);} catch (Exception e) {e.printStackTrace();}}}//播放音频文件public void play(String filePathName){File file = new File(filePathName);try {FileInputStream fis = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(fis);Player player = new Player(bis);player.play();} catch (FileNotFoundException e) {e.printStackTrace();} catch (JavaLayerException e) {e.printStackTrace();}}
}

SpeechRec类完成百度语音识别/合成:

package com.feng.robot;import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import org.json.JSONObject;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
/*** 百度语音识别ASR() & 合成TTS()*/
public class SpeechRec {private static final String serverURL = "http://vop.baidu.com/server_api";private static String token = "";private static final String apiKey = "自己的API Key";private static final String secretKey = "自己的Secret Key";private static final String cuid = "自己的App ID";public static void main(String[] args) throws Exception {Record record = new Record();SpeechRec s = new SpeechRec();record.record();s.getToken();String str = s.ASR(new File("F:\\record.wav"));System.out.println(str);}//语音识别:音频-->文本,并返回解析后的文本public String ASR(File recordFile) throws Exception {HttpURLConnection conn = (HttpURLConnection) new URL(serverURL+ "?cuid=" + cuid + "&token=" + token).openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "audio/wav; rate=16000");conn.setDoInput(true);conn.setDoOutput(true);// 发送请求DataOutputStream wr = new DataOutputStream(conn.getOutputStream());wr.write(loadFile(recordFile));wr.flush();wr.close();String recordText = parseJson(printResponse(conn));//解析后的识别文本return recordText;}public void getToken() throws Exception {String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +"&client_id=" + apiKey + "&client_secret=" + secretKey;HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection();token = new JSONObject(printResponse(conn)).getString("access_token");}public String printResponse(HttpURLConnection conn) throws Exception {if (conn.getResponseCode() != 200) {System.out.println("conn.getResponseCode() = " + conn.getResponseCode());return "";}InputStream is = conn.getInputStream();BufferedReader rd = new BufferedReader(new InputStreamReader(is));String line;StringBuffer response = new StringBuffer();while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');}rd.close();return response.toString();}private byte[] loadFile(File file) throws IOException {InputStream is = new FileInputStream(file);long length = file.length();byte[] bytes = new byte[(int) length];int offset = 0;int numRead = 0;while (offset < bytes.length&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {offset += numRead;}if (offset < bytes.length) {is.close();throw new IOException("Could not completely read file " + file.getName());}is.close();return bytes;}//解析后,得到百度语音识别后的文本public String parseJson(String response){if(response == null){return "";}net.sf.json.JSONObject ASRResult = net.sf.json.JSONObject.fromObject(response);String resultText = "";if(ASRResult.get("result") == null){resultText = "[\"\"]";}else{resultText = ASRResult.get("result").toString();}//resultText = ASRResult.get("result").toString();resultText = resultText.substring(2,resultText.length()-2);return resultText;}//语音合成,文本-->音频,返回得到的音频文件public File TTS(String text){AipSpeech aipSpeech = new AipSpeech(cuid,apiKey,secretKey);HashMap options = new HashMap();options.put("spd","5");options.put("pit","5");options.put("per","4");TtsResponse ttsResponse = aipSpeech.synthesis(text,"zh",1,options);byte[] aa = ttsResponse.getData();getFile(aa,"F:\\play.wav");File file = new File("F:\\play.wav");return file;}//合成的二进制数据放入文件中private void getFile(byte[] binData, String filePathName) {BufferedOutputStream bos = null;FileOutputStream fos = null;File file = new File(filePathName);try {fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(binData);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//关闭流if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
}

TulingRobot类完成与图灵机器人进行对话的操作:

package com.feng.robot;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;/*** 图灵机器人*/
public class TulingRobot {private static final String url = "http://www.tuling123.com/openapi/";private static final String api_key = "自己的apiKey";//获取响应,得到响应的json字符串public String getResult( String message ) {//图灵api接口String apiUrl = url+"api?key="+api_key+"&info=";try {//设置编码格式message = URLEncoder.encode(message, "utf-8");//拼接urlapiUrl = apiUrl + message;} catch (UnsupportedEncodingException e) {e.printStackTrace();}//封装请求头HttpPost request = new HttpPost(apiUrl);String result = "";try {//发送http请求HttpResponse response = HttpClients.createDefault().execute(request);//获取响应码int code = response.getStatusLine().getStatusCode();if (code == 200) {//获取返回的结果result = EntityUtils.toString(response.getEntity());} else {System.out.println("code=" + code);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return result; //返回结果{"code":xxx,"text":xxx}}//解析Json字符串public String parseJson(String jsonStirng){if(jsonStirng == null){return "";}//json字符串 --> json对象JSONObject results = JSONObject.fromObject(jsonStirng);String result = results.getString("text");return result;}
}

RobotYuanYuan类中包含了要运行的主方法,也可以在主方法添加音乐,进行点歌:

package com.feng.robot;import java.io.File;/*** 机器人圆圆*/public class YuanYuan {private static File recordFile = new File("F:\\record.wav");private static String playFileName = "F:\\play.wav";public static void main(String[] args){/*** 录音-->百度语音识别,获取到文本-->文本传入到图灵机器人与之对话-->拿到图灵机器人的回话* -->百度语音合成,将回话转为音频文件-->播放音频文件* 以上完成一次完整的对话* 若我说:退出-->百度语音识别为“退出”-->百度语音合成文本“好的,那下次再见了”-->播放音频文件-->退出程序* 若我说的是歌名-->播放音乐(前提是音乐已经下载好了)*/Record record = new Record();SpeechRec speechRec = new SpeechRec();TulingRobot tulingRobot = new TulingRobot();while(true){record.record();//录音try {speechRec.getToken();String recordText = speechRec.ASR(recordFile);//百度语音识别if(recordText.equals("退出。")){speechRec.TTS("好的,那下次再见了");System.out.println("(我):退出");System.out.println("(圆圆):"+"好的,那下次再见了");record.play(playFileName);//播放合成的音频文件System.exit(0);//退出程序}if(recordText.equals("")){speechRec.TTS("你啥也不说,我如何作答呀");System.out.println("(我):"+recordText);System.out.println("(圆圆):"+"你啥也不说,我如何作答呀");record.play(playFileName);//播放合成的音频文件continue;}/***  播放音乐(可配置)*  配置过程:在指定文件夹下添加下载好的音乐-->如果识别的是音乐名,播放音频文件即可*/if(recordText.equals("意外。")){System.out.println("音乐播放中...");record.play("F:\\music\\意外.mp3");continue;}if(recordText.equals("告白之夜。")){System.out.println("音乐播放中...");record.play("F:\\music\\告白之夜.mp3");continue;}if(recordText.equals("工资向北走。")){//百度语音识别总是将“公子”识别成“工资”,可能是我发音不对吧哈哈                   record.play("F:\\music\\公子向北走.mp3");System.out.println("音乐播放中...");record.play("F:\\music\\公子向北走.mp3");continue;}//按以上格式添加配置即可String result = tulingRobot.parseJson(tulingRobot.getResult(recordText));//得到图灵机器人的回话speechRec.TTS(result);//百度语音合成System.out.println("(我):"+recordText);System.out.println("(圆圆):"+result);record.play(playFileName);} catch (Exception e) {e.printStackTrace();}}}
}

我们简单的运行一下:

对机器人说歌名,就可以播放音乐了:

如果想退出聊天了,说“退出”:

程序还存在缺点:播放音乐时,必须整首歌播放完之后才能继续聊天,不能中途打断。

后期可以进行扩展:不想听歌了输入相关指令停止音乐播放。

也可以进行以下扩展:让机器人打开网站进行关键词搜索等命令。

与图灵机器人进行语音对话(Java)(百度语音识别/合成)相关推荐

  1. python3基于百度开放平台和图灵机器人的语音助手

    python3基于百度开放平台和图灵机器人的语音助手 简介:刚刚接触python,作为一个小白,想搞一个方便自己日常生活的助手,功能肯定不多,但是满足我的基本要求. (我用的是pycharm) 本de ...

  2. 魔镜魔镜告诉我谁是世界上最美的人 语音唤醒,百度语音识别。从装系统开始

    魔镜魔镜告诉我谁是世界上最美的人 语音唤醒,百度语音识别.从装系统开始 准备原料 安装树莓系统 准备: 硬件: 软件: 步骤 很高兴,您来到我的小屋,本人diy爱好者,小白,刚刚开始玩树莓派,希望前辈 ...

  3. 百度语音识别合成案例

    百度语音识别合成案例 本文截取之前项目里涉及到的语音识别和合成部分进行代码展示.其中读取语音部分需要优化,这里是demo读取的是存在本地的音频文件,建议走音频流. 1 控制器示例代码 package ...

  4. java+百度语音识别(语音助手)

    文章目录 项目实施图 java实时录音 java文字转语音 百度AI账号注册 百度AI接口调用 项目中应用 具体实现效果 程序缺点 因为小项目是基于:java学生管理系统(百度人脸识别 + Swing ...

  5. 百度语音输入 html5,百度语音识别(采集麦克风声音 并自动转为文字)

    资源下载此资源下载价格为2D币,请先登录 资源文件列表 c#实现百度语音识别/NAudio.dll , 506880 c#实现百度语音识别/Newtonsoft.Json.dll , 653824 c ...

  6. Python免费快速接入图灵机器人接口

    图灵机器人相信大家并不陌生.通过图灵机器人,开发者和厂商能够以高效的方式创建专属的聊天机器人.客服机器人.领域对话问答机器人.儿童/服务机器人等.下面给大家见到那介绍如何通过Python(2.7版本) ...

  7. 基于 QT5 百度语音API 图灵机器人API 的智能语音聊天机器人

    基于 QT5 百度语音API 图灵机器人API 的智能语音聊天机器人 程序简介 代码一共分为以下几个模块 伪代码形式为 部分代码 源代码下载地址 程序简介 程序界面包含录音和发送两个按钮 点录音将开始 ...

  8. 有没有python与机械结合的工作-Python3从零开始搭建一个语音对话机器人的实现...

    01-初心缘由 最近在研究语音识别方向,看了很多的语音识别的资料和文章,了解了一下语音识别的前世今生,其中包含了很多算法的演变,目前来说最流行的语音识别算法主要是依赖于深度学习的神经网络算法,其中RN ...

  9. python发音机器人_Python3从零开始搭建一个语音对话机器人

    目录 01-初心缘由 最近在研究语音识别方向,看了很多的语音识别的资料和文章,了解了一下语音识别的前世今生,其中包含了很多算法的演变,目前来说最流行的语音识别算法主要是依赖于深度学习的神经网络算法,其 ...

最新文章

  1. 【青春须早为,岂能长少年】一个初入职场程序员的阶段总结
  2. c语言宿舍管理查询软件,宿舍管理查询软件--数据结构,c语言
  3. 论文工具 | 翻译神器
  4. python遍历获取一个类的所有子类
  5. 测试人员该学习哪些Linux知识
  6. 工作那些事(四)大公司VS小公司
  7. 爱迪德CA系统与NDS CA系统对比分析(一)
  8. 自动化之RPA工具之UiPath
  9. 2022-2028全球与中国废物转化能源市场现状及未来发展趋势
  10. Python 自然语言处理笔记(五)——信息检索系统,基于Lucene实现
  11. 大学四年,我做过哪些兼职
  12. 对首次认定为虹口区四新示范企业给予20万元奖励
  13. IOS 开发 iPhone屏幕尺寸、分辨率及适配
  14. matlab不同版本之间编码出错问题20200825
  15. 集合之六:Map接口
  16. C语言实现简易扫雷游戏
  17. 被尤雨溪diss的Native CSS Modules是什么
  18. Forter宣布推出Smart Payments解决方案 帮助企业提升数字商务转化率和营收
  19. xp系统无法连接到指定计算机名,winxp系统提示windows无法访问指定设备路径或文件如何解决...
  20. IDEA集成微软TFS插件

热门文章

  1. 2023考研常识之在职人员考研需要注意哪些问题?
  2. 8月一次阿里云的Java面试凉经(止步三面)
  3. 集成计算器,日期差,绘制函数图像功能的Matlab App Designer 开发
  4. angular5+动态设置页面标题title
  5. web怎么将dwg转换图片_怎么把CAD图转换成高清jpg图?
  6. Dark Knowledge by Hinton
  7. 大三SE计组II开火车问题答案整理(第六章 自用
  8. 只能输入字母的c语言程序设计教程课后答案,c语言程序设计基础教程_习题答案解析.doc...
  9. 用计算机打印出1000,一台HP1000型激光打印机用5米USB延长线接到另一台做主机的电脑上频繁出现打印故障!...
  10. CSS 让鼠标呈现手型,鼠标悬浮变小手